Skip to content

Commit 6c1d085

Browse files
author
hiram
committed
netty实现http客户端访问修改部分逻辑
1 parent 2cc8eca commit 6c1d085

8 files changed

Lines changed: 35 additions & 340 deletions

File tree

02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package io.github.kimmking.gateway.inbound;
22

3+
import io.github.kimmking.gateway.filter.HttpMethodRequestFilter;
34
import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
45
import io.github.kimmking.gateway.outbound.netty4.NettyHttpClient;
56
import io.github.kimmking.gateway.outbound.netty4.NettyHttpOutboundHandler;
67
import io.netty.channel.ChannelHandlerContext;
78
import io.netty.channel.ChannelInboundHandlerAdapter;
89
import io.netty.handler.codec.http.FullHttpRequest;
10+
import io.netty.handler.codec.http.HttpHeaderNames;
911
import io.netty.util.ReferenceCountUtil;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
@@ -18,7 +20,7 @@ public class HttpInboundHandler extends ChannelInboundHandlerAdapter {
1820

1921
public HttpInboundHandler(String proxyServer) {
2022
this.proxyServer = proxyServer;
21-
handler = new NettyHttpClient();
23+
handler = new NettyHttpClient(proxyServer);
2224
}
2325

2426
@Override
@@ -32,14 +34,17 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
3234

3335
//logger.info("channelRead流量接口请求开始,时间为{}", startTime);
3436
FullHttpRequest fullRequest = (FullHttpRequest) msg;
35-
// String uri = fullRequest.uri();
36-
// //logger.info("接收到的请求url为{}", uri);
37+
String uri = fullRequest.uri();
38+
fullRequest.headers().get(HttpHeaderNames.CONTENT_TYPE);
39+
//System.out.println("接收到的请求url为" + uri);
3740
// if (uri.contains("/test")) {
3841
// handlerTest(fullRequest, ctx);
3942
// }
4043

4144
//handler.handle(fullRequest, ctx);
42-
handler.connect(proxyServer, ctx);
45+
HttpMethodRequestFilter myFirstHttpRequestFilter = new HttpMethodRequestFilter();
46+
myFirstHttpRequestFilter.filter(fullRequest, ctx);
47+
handler.connect(fullRequest, ctx);
4348
} catch(Exception e) {
4449
e.printStackTrace();
4550
} finally {

02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
import io.netty.channel.nio.NioEventLoopGroup;
66
import io.netty.channel.socket.SocketChannel;
77
import io.netty.channel.socket.nio.NioSocketChannel;
8+
import io.netty.handler.codec.http.FullHttpRequest;
9+
import io.netty.handler.codec.http.HttpHeaderNames;
810
import io.netty.handler.codec.http.HttpRequestEncoder;
911
import io.netty.handler.codec.http.HttpResponseDecoder;
1012

1113
public class NettyHttpClient {
12-
public void connect(String proxyServer, ChannelHandlerContext ctx) throws Exception {
14+
private String backendUrl;
15+
16+
public NettyHttpClient(String backendUrl) {
17+
this.backendUrl = backendUrl;
18+
}
19+
20+
public void connect(FullHttpRequest fullHttpRequest, ChannelHandlerContext ctx) throws Exception {
1321
EventLoopGroup workerGroup = new NioEventLoopGroup();
1422

1523
try {
@@ -25,7 +33,7 @@ public void initChannel(SocketChannel ch) throws Exception {
2533
ch.pipeline().addLast(new HttpResponseDecoder());
2634
// 客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
2735
ch.pipeline().addLast(new HttpRequestEncoder());
28-
ch.pipeline().addLast(new NettyHttpOutboundHandler(ctx, proxyServer));
36+
ch.pipeline().addLast(new NettyHttpOutboundHandler(ctx, fullHttpRequest, backendUrl));
2937
}
3038
});
3139

@@ -39,8 +47,8 @@ public void initChannel(SocketChannel ch) throws Exception {
3947
request.headers().set(HttpHeaderNames.CONTENT_LENGTH,
4048
request.content().readableBytes());*/
4149
// Start the client.
42-
String host = proxyServer.replaceAll("/", "").split(":")[1];
43-
int port = Integer.parseInt(proxyServer.replaceAll("/", "").split(":")[2]);
50+
String host = backendUrl.replaceAll("/", "").split(":")[1];
51+
int port = Integer.parseInt(backendUrl.replaceAll("/", "").split(":")[2]);
4452
ChannelFuture f = b.connect(host, port).sync();
4553
/*f.channel().write(request);
4654
f.channel().flush();*/
@@ -52,7 +60,7 @@ public void initChannel(SocketChannel ch) throws Exception {
5260
}
5361

5462
public static void main(String[] args) throws Exception {
55-
NettyHttpClient client = new NettyHttpClient();
63+
// NettyHttpClient client = new NettyHttpClient();
5664
//client.connect("127.0.0.1", 8088);
5765
}
5866
}

02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpOutboundHandler.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,25 @@ public class NettyHttpOutboundHandler extends ChannelInboundHandlerAdapter {
1717
private ByteBufToBytes reader;
1818
private ChannelHandlerContext parentCtx;
1919
private int contentLength = 0;
20-
private String proxyServer = null;
21-
public NettyHttpOutboundHandler(ChannelHandlerContext ctx, String proxyServer) {
20+
private FullHttpRequest fullHttpRequest = null;
21+
private String backendUrl;
22+
public NettyHttpOutboundHandler(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest, String backendUrl) {
2223
this.parentCtx = ctx;
23-
this.proxyServer = proxyServer;
24+
this.fullHttpRequest = fullHttpRequest;
25+
this.backendUrl = backendUrl;
2426
}
2527
@Override
2628
public void channelActive(ChannelHandlerContext ctx)
2729
throws Exception {
28-
System.out.println("channelActive");
30+
//System.out.println("channelActive");
2931
/* URI uri = new URI("/api/hello");
3032
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString());
3133
request.headers().add(HttpHeaderNames.CONNECTION,HttpHeaderValues.KEEP_ALIVE);
3234
request.headers().add(HttpHeaderNames.CONTENT_LENGTH,request.content().readableBytes());
3335
ctx.writeAndFlush(request);*/
34-
String host = proxyServer.replaceAll("/", "").split(":")[1];
35-
DefaultFullHttpRequest request = new DefaultFullHttpRequest(
36-
HttpVersion.HTTP_1_1, HttpMethod.GET, new URI("/api/hello").toASCIIString());
36+
String host = backendUrl.replaceAll("/", "").split(":")[1];
37+
DefaultFullHttpRequest request = new DefaultFullHttpRequest(
38+
HttpVersion.HTTP_1_1, fullHttpRequest.method(), fullHttpRequest.uri());
3739
/*DefaultFullHttpRequest request = new DefaultFullHttpRequest(
3840
HttpVersion.HTTP_1_1, HttpMethod.GET, new URI("/").toASCIIString());*/
3941

@@ -49,12 +51,12 @@ public void channelActive(ChannelHandlerContext ctx)
4951
@Override
5052
public void channelRead(ChannelHandlerContext ctx, Object msg)
5153
throws Exception {
52-
System.out.println("channelRead");
54+
//System.out.println("channelRead");
5355

5456
if (msg instanceof HttpResponse) {
5557
HttpResponse response = (HttpResponse) msg;
56-
System.out.println("CONTENT_TYPE:"
57-
+ response.headers().get(HttpHeaderNames.CONTENT_TYPE));
58+
/*System.out.println("CONTENT_TYPE:"
59+
+ response.headers().get(HttpHeaderNames.CONTENT_TYPE));*/
5860
if (HttpUtil.isContentLengthSet(response)) {
5961
contentLength = (int) HttpUtil.getContentLength(response);
6062
reader = new ByteBufToBytes(contentLength);
@@ -66,7 +68,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg)
6668
reader.reading(content);
6769
content.release();
6870
byte[] bytes = reader.readFull();
69-
System.out.println(new String(bytes));
71+
//System.out.println(new String(bytes));
7072
if (reader.isEnd()) {
7173
FullHttpResponse response = null;
7274
try {

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

Lines changed: 0 additions & 138 deletions
This file was deleted.

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

Lines changed: 0 additions & 32 deletions
This file was deleted.

02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/okhttp/netty4/NettyHttpClient.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)