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- //
1+ package io .github .kimmking .gateway .outbound .netty4 ;
2+
3+ import io .netty .bootstrap .Bootstrap ;
4+ import io .netty .channel .*;
5+ import io .netty .channel .nio .NioEventLoopGroup ;
6+ import io .netty .channel .socket .SocketChannel ;
7+ import io .netty .channel .socket .nio .NioSocketChannel ;
8+ import io .netty .handler .codec .http .*;
9+ import io .netty .handler .logging .LogLevel ;
10+ import io .netty .handler .logging .LoggingHandler ;
11+
12+ import java .util .Map ;
13+
14+ public class NettyHttpClient {
15+
16+ Bootstrap b = new Bootstrap ();
17+ EventLoopGroup workerGroup = new NioEventLoopGroup ();
18+
19+ private String host ;
20+ private int port ;
21+ public NettyHttpClient (String backendUrl ){
22+ backendUrl = backendUrl .endsWith ("/" )?backendUrl .substring (0 ,backendUrl .length ()-1 ):backendUrl ;
23+ String [] split = backendUrl .substring (backendUrl .indexOf ("://" ) + 3 ).split (":" );
24+ host = split [0 ];
25+ port = Integer .parseInt (split [1 ]);
26+ // connect(host,port);
27+ b .group (workerGroup );
28+ b .channel (NioSocketChannel .class );
29+ b .option (ChannelOption .SO_KEEPALIVE , true );
30+ }
31+
32+ public void connect (String host , int port ,final FullHttpRequest fullRequest , final ChannelHandlerContext ctx ) throws InterruptedException {
33+
1834// 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();
35+
36+ b .handler (new ChannelInitializer <SocketChannel >() {
37+ @ Override
38+ public void initChannel (SocketChannel ch ) throws Exception {
39+ // 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
40+ //客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
41+ ch .pipeline ().addLast (new HttpResponseDecoder ());
42+ ch .pipeline ().addLast (new HttpRequestEncoder ());
43+ ch .pipeline ().addLast (new NettyHttpClientOutboundHandler (ctx .channel ()));
44+ ch .pipeline ().addLast (new LoggingHandler (LogLevel .INFO ));
45+ }
46+ });
47+ // Start the client.
48+ Channel channel = b .connect (host , port ).sync ().channel ();
49+ channel .writeAndFlush (fullRequest );
4150// } finally {
4251// workerGroup.shutdownGracefully();
43- // }
4452//
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- //}
53+ // }
54+
55+ }
56+
57+ public void handle (final FullHttpRequest fullRequest , final ChannelHandlerContext ctx ) throws Exception {
58+ System .out .println (fullRequest .headers ().get ("nio" ));
59+
60+ connect (host ,port ,fullRequest ,ctx );
61+ }
62+ }
0 commit comments