Skip to content

Commit 1b359ba

Browse files
author
bl03615
committed
2.(选做)使用 netty 实现后端 http 访问(代替上一步骤)
1 parent 533ab82 commit 1b359ba

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.kimmking.gateway.client;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.ChannelInboundHandlerAdapter;
6+
import io.netty.handler.codec.http.*;
7+
import io.netty.util.CharsetUtil;
8+
9+
import java.net.URI;
10+
11+
public class HttpClientHandler extends ChannelInboundHandlerAdapter {
12+
13+
@Override
14+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
15+
URI uri = new URI("/user/get");
16+
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, uri.toASCIIString());
17+
request.headers().add(HttpHeaderNames.CONNECTION,HttpHeaderValues.KEEP_ALIVE);
18+
request.headers().add(HttpHeaderNames.CONTENT_LENGTH,request.content().readableBytes());
19+
ctx.writeAndFlush(request);
20+
}
21+
22+
@Override
23+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
24+
System.out.println("msg ->" + msg);
25+
26+
if (msg instanceof FullHttpRequest) {
27+
FullHttpResponse response = (FullHttpResponse) msg;
28+
ByteBuf buf = response.content();
29+
String result = buf.toString(CharsetUtil.UTF_8);
30+
System.out.println("response -> " + result);
31+
32+
}
33+
}
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.github.kimmking.gateway.client;
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.nio.NioSocketChannel;
7+
import io.netty.handler.codec.http.HttpClientCodec;
8+
import io.netty.handler.codec.http.HttpContentDecompressor;
9+
import io.netty.handler.codec.http.HttpObjectAggregator;
10+
11+
public class NettyClientTest {
12+
13+
public static void start(String host,int port){
14+
15+
EventLoopGroup group = new NioEventLoopGroup();
16+
17+
try {
18+
Bootstrap b = new Bootstrap();
19+
b.group(group)
20+
.channel(NioSocketChannel.class)
21+
.option(ChannelOption.SO_KEEPALIVE,true)
22+
.handler(new ChannelInitializer<Channel>() {
23+
@Override
24+
protected void initChannel(Channel ch) throws Exception {
25+
ch.pipeline().addLast(new HttpClientCodec());
26+
ch.pipeline().addLast(new HttpObjectAggregator(65536));
27+
ch.pipeline().addLast(new HttpContentDecompressor());
28+
ch.pipeline().addLast(new HttpClientHandler());
29+
}
30+
});
31+
32+
ChannelFuture future = b.connect(host, port).sync();
33+
ChannelFuture channelFuture = future.channel().closeFuture().sync();
34+
35+
} catch (InterruptedException e) {
36+
e.printStackTrace();
37+
} finally {
38+
System.out.println("关闭线程组");
39+
group.shutdownGracefully();
40+
}
41+
42+
}
43+
44+
public static void main(String[] args) {
45+
start("localhost",8888);
46+
}
47+
48+
}

02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ public void handle(final FullHttpRequest fullRequest, final ChannelHandlerContex
7979

8080
String backendUrl = router.route(this.backendUrls,weights);
8181
final String url = backendUrl + fullRequest.uri();
82+
83+
System.out.println("被访问的url:"+url);
84+
8285
filter.filter(fullRequest, ctx);
8386

84-
//TODO ExcutorService now,use netty instead
8587
proxyService.submit(()->fetchGet(fullRequest, ctx, url));
8688
}
8789

0 commit comments

Comments
 (0)