1- //package io.github.kimmking.gateway.outbound;
2- //
3- //import io.netty.bootstrap.Bootstrap;
4- //import io.netty.channel.ChannelFuture;
5- //import io.netty.channel.ChannelInitializer;
6- //import io.netty.channel.ChannelOption;
7- //import io.netty.channel.EventLoopGroup;
8- //import io.netty.channel.nio.NioEventLoopGroup;
9- //import io.netty.channel.socket.SocketChannel;
10- //import io.netty.channel.socket.nio.NioSocketChannel;
11- //import io.netty.handler.codec.http.HttpRequestEncoder;
12- //import io.netty.handler.codec.http.HttpResponseDecoder;
13- //
14- //public class NettyHttpClient {
15- // public void connect(String host, int port) throws Exception {
16- // EventLoopGroup workerGroup = new NioEventLoopGroup();
17- //
18- // try {
19- // Bootstrap b = new Bootstrap();
20- // b.group(workerGroup);
21- // b.channel(NioSocketChannel.class);
22- // b.option(ChannelOption.SO_KEEPALIVE, true);
23- // b.handler(new ChannelInitializer<SocketChannel>() {
24- // @Override
25- // public void initChannel(SocketChannel ch) throws Exception {
26- // // 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
27- // ch.pipeline().addLast(new HttpResponseDecoder());
28- // 客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
29- // ch.pipeline().addLast(new HttpRequestEncoder());
30- // ch.pipeline().addLast(new HttpClientOutboundHandler());
31- // }
32- // });
33- //
34- // // Start the client.
35- // ChannelFuture f = b.connect(host, port).sync();
36- //
37- //
38- // f.channel().write(request);
39- // f.channel().flush();
40- // f.channel().closeFuture().sync();
41- // } finally {
42- // workerGroup.shutdownGracefully();
43- // }
44- //
45- // }
46- //
47- // public static void main(String[] args) throws Exception {
48- // NettyHttpClient client = new NettyHttpClient();
49- // client.connect("127.0.0.1", 8844);
50- // }
51- //}
1+ package io .github .kimmking .gateway .outbound .netty4 ;
2+
3+ import io .netty .bootstrap .Bootstrap ;
4+ import io .netty .channel .ChannelFuture ;
5+ import io .netty .channel .ChannelInitializer ;
6+ import io .netty .channel .ChannelOption ;
7+ import io .netty .channel .EventLoopGroup ;
8+ import io .netty .channel .nio .NioEventLoopGroup ;
9+ import io .netty .channel .socket .SocketChannel ;
10+ import io .netty .channel .socket .nio .NioSocketChannel ;
11+ import io .netty .handler .codec .http .HttpRequestEncoder ;
12+ import io .netty .handler .codec .http .HttpResponseDecoder ;
13+
14+ public class NettyHttpClient {
15+ public void connect (String host , int port ) throws Exception {
16+ EventLoopGroup workerGroup = new NioEventLoopGroup ();
17+
18+ try {
19+ Bootstrap b = new Bootstrap ();
20+ b .group (workerGroup );
21+ b .channel (NioSocketChannel .class );
22+ b .option (ChannelOption .SO_KEEPALIVE , true );
23+ b .handler (new ChannelInitializer <SocketChannel >() {
24+ @ Override
25+ public void initChannel (SocketChannel ch ) throws Exception {
26+ // 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
27+ ch .pipeline ().addLast (new HttpResponseDecoder ());
28+ // 客户端发送的是http request,所以要使用HttpRequestEncoder进行编码
29+ ch .pipeline ().addLast (new HttpRequestEncoder ());
30+ ch .pipeline ().addLast (new NettyHttpClientOutboundHandler ());
31+ }
32+ });
33+
34+ // Start the client.
35+ ChannelFuture f = b .connect (host , port ).sync ();
36+
37+ f .channel ().write ("" );
38+ f .channel ().flush ();
39+ f .channel ().closeFuture ().sync ();
40+ } finally {
41+ workerGroup .shutdownGracefully ();
42+ }
43+
44+ }
45+
46+ public static void main (String [] args ) throws Exception {
47+ NettyHttpClient client = new NettyHttpClient ();
48+ client .connect ("127.0.0.1" , 8801 );
49+ }
50+ }
0 commit comments