Skip to content

Commit 1da8cdb

Browse files
第三周作业提交
1 parent 01d9794 commit 1da8cdb

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.kimmking.gateway.filter;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.handler.codec.http.FullHttpRequest;
5+
6+
/**
7+
* 自定义http过滤器
8+
*/
9+
public class MyHttpRequestFilter implements HttpRequestFilter {
10+
@Override
11+
public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
12+
fullRequest.headers().add("nio","huoziyang");
13+
}
14+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public HttpInboundServer(int port, String proxyServer) {
2929
public void run() throws Exception {
3030

3131
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
32-
EventLoopGroup workerGroup = new NioEventLoopGroup(16);
32+
EventLoopGroup workerGroup = new NioEventLoopGroup();
3333

3434
try {
3535
ServerBootstrap b = new ServerBootstrap();
@@ -44,7 +44,8 @@ public void run() throws Exception {
4444
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
4545

4646
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
47-
.handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpInboundInitializer(this.proxyServer));
47+
.handler(new LoggingHandler(LogLevel.INFO))
48+
.childHandler(new HttpInboundInitializer(this.proxyServer));
4849

4950
Channel ch = b.bind(port).sync().channel();
5051
logger.info("开启netty http服务器,监听地址和端口为 http://127.0.0.1:" + port + '/');

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

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

33

4+
import io.github.kimmking.gateway.filter.MyHttpRequestFilter;
45
import io.netty.buffer.Unpooled;
56
import io.netty.channel.ChannelFutureListener;
67
import io.netty.channel.ChannelHandlerContext;
7-
import io.netty.handler.codec.http.DefaultFullHttpResponse;
8-
import io.netty.handler.codec.http.FullHttpRequest;
9-
import io.netty.handler.codec.http.FullHttpResponse;
10-
import io.netty.handler.codec.http.HttpUtil;
8+
import io.netty.handler.codec.http.*;
119
import org.apache.http.HttpResponse;
1210
import org.apache.http.client.methods.HttpGet;
1311
import org.apache.http.concurrent.FutureCallback;
@@ -26,6 +24,7 @@
2624
public class HttpOutboundHandler {
2725

2826
private CloseableHttpAsyncClient httpclient;
27+
private MyHttpRequestFilter myHttpRequestFilter;
2928
private ExecutorService proxyService;
3029
private String backendUrl;
3130

@@ -62,6 +61,9 @@ public void handle(final FullHttpRequest fullRequest, final ChannelHandlerContex
6261
private void fetchGet(final FullHttpRequest inbound, final ChannelHandlerContext ctx, final String url) {
6362
final HttpGet httpGet = new HttpGet(url);
6463
//httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
64+
myHttpRequestFilter.filter(inbound,ctx);
65+
HttpRequest request = (HttpRequest)inbound;
66+
HttpHeaders headers = request.headers();
6567
httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
6668
httpclient.execute(httpGet, new FutureCallback<HttpResponse>() {
6769
@Override

0 commit comments

Comments
 (0)