diff --git a/02nio/nio01/pom.xml b/02nio/nio01/pom.xml
index e42a62f2..2465c0a4 100644
--- a/02nio/nio01/pom.xml
+++ b/02nio/nio01/pom.xml
@@ -59,6 +59,28 @@
netty-all
4.1.51.Final
+
+
+ commons-logging
+ commons-logging
+ 1.2
+
+
+ org.slf4j
+ slf4j-api
+ 1.7.25
+
+
+ org.slf4j
+ slf4j-log4j12
+ 1.7.25
+
+
+ org.apache.httpcomponents
+ httpasyncclient
+ 4.1.4
+
+
diff --git a/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java b/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java
index 58c6e0a8..61f9ce32 100644
--- a/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java
+++ b/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java
@@ -9,6 +9,7 @@
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.util.ReferenceCountUtil;
+import java0.nio01.util.HttpClientUtil;
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
@@ -44,11 +45,9 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
FullHttpResponse response = null;
try {
- String value = null; // "hello,kimmking"; // 对接上次作业的httpclient或者okhttp请求另一个url的响应数据
+// String value = null; // "hello,kimmking"; // 对接上次作业的httpclient或者okhttp请求另一个url的响应数据
-// httpGet ... http://localhost:8801
-// 返回的响应,"hello,nio";
-// value = reponse....
+ String value = HttpClientUtil.doGet("http://localhost:8801");
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
response.headers().set("Content-Type", "application/json");
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java
index e67b7961..45dca8cb 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java
@@ -22,11 +22,11 @@ public static void main(String[] args) {
// 这是多个后端url走随机路由的例子
- String proxyServers = System.getProperty("proxyServers","http://localhost:8801,http://localhost:8802");
+ String proxyServers = System.getProperty("proxyServers","http://127.0.0.1:8808,http://127.0.0.1:8801");
int port = Integer.parseInt(proxyPort);
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" starting...");
HttpInboundServer server = new HttpInboundServer(port, Arrays.asList(proxyServers.split(",")));
- System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" started at http://localhost:" + port + " for server:" + server.toString());
+ System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" started at http://127.0.0.1:" + port + " for server:" + server.toString());
try {
server.run();
}catch (Exception ex){
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java
index 69b40fde..d39d56fc 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java
@@ -1,8 +1,7 @@
package io.github.kimmking.gateway.inbound;
-import io.github.kimmking.gateway.filter.HeaderHttpRequestFilter;
-import io.github.kimmking.gateway.filter.HttpRequestFilter;
import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
+import io.github.ningtianjing.homework03.filter.MyHttpRequestFilter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
@@ -17,8 +16,9 @@ public class HttpInboundHandler extends ChannelInboundHandlerAdapter {
private static Logger logger = LoggerFactory.getLogger(HttpInboundHandler.class);
private final List proxyServer;
private HttpOutboundHandler handler;
- private HttpRequestFilter filter = new HeaderHttpRequestFilter();
-
+// private HttpRequestFilter filter = new HeaderHttpRequestFilter();
+
+ private MyHttpRequestFilter requestFilter = new MyHttpRequestFilter();
public HttpInboundHandler(List proxyServer) {
this.proxyServer = proxyServer;
this.handler = new HttpOutboundHandler(this.proxyServer);
@@ -40,7 +40,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
// handlerTest(fullRequest, ctx);
// }
- handler.handle(fullRequest, ctx, filter);
+ handler.handle(fullRequest, ctx, requestFilter);
} catch(Exception e) {
e.printStackTrace();
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java
index c20c9be5..9813025e 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java
@@ -1,11 +1,11 @@
package io.github.kimmking.gateway.outbound.httpclient4;
-import io.github.kimmking.gateway.filter.HeaderHttpResponseFilter;
import io.github.kimmking.gateway.filter.HttpRequestFilter;
import io.github.kimmking.gateway.filter.HttpResponseFilter;
import io.github.kimmking.gateway.router.HttpEndpointRouter;
import io.github.kimmking.gateway.router.RandomHttpEndpointRouter;
+import io.github.ningtianjing.homework03.filter.MyHttpResponseFilter;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
@@ -23,9 +23,7 @@
import org.apache.http.util.EntityUtils;
import java.util.List;
-import java.util.Random;
import java.util.concurrent.*;
-import java.util.logging.Filter;
import java.util.stream.Collectors;
import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
@@ -33,14 +31,16 @@
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
public class HttpOutboundHandler {
-
+
private CloseableHttpAsyncClient httpclient;
private ExecutorService proxyService;
private List backendUrls;
- HttpResponseFilter filter = new HeaderHttpResponseFilter();
+ // HttpResponseFilter filter = new HeaderHttpResponseFilter();
HttpEndpointRouter router = new RandomHttpEndpointRouter();
+ HttpResponseFilter responseFilter = new MyHttpResponseFilter();
+
public HttpOutboundHandler(List backends) {
this.backendUrls = backends.stream().map(this::formatUrl).collect(Collectors.toList());
@@ -52,14 +52,14 @@ public HttpOutboundHandler(List backends) {
proxyService = new ThreadPoolExecutor(cores, cores,
keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize),
new NamedThreadFactory("proxyService"), handler);
-
+
IOReactorConfig ioConfig = IOReactorConfig.custom()
.setConnectTimeout(1000)
.setSoTimeout(1000)
.setIoThreadCount(cores)
.setRcvBufSize(32 * 1024)
.build();
-
+
httpclient = HttpAsyncClients.custom().setMaxConnTotal(40)
.setMaxConnPerRoute(8)
.setDefaultIOReactorConfig(ioConfig)
@@ -71,19 +71,18 @@ public HttpOutboundHandler(List backends) {
private String formatUrl(String backend) {
return backend.endsWith("/")?backend.substring(0,backend.length()-1):backend;
}
-
+
public void handle(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx, HttpRequestFilter filter) {
String backendUrl = router.route(this.backendUrls);
final String url = backendUrl + fullRequest.uri();
filter.filter(fullRequest, ctx);
proxyService.submit(()->fetchGet(fullRequest, ctx, url));
}
-
+
private void fetchGet(final FullHttpRequest inbound, final ChannelHandlerContext ctx, final String url) {
final HttpGet httpGet = new HttpGet(url);
- //httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
- httpGet.setHeader("mao", inbound.headers().get("mao"));
+ httpGet.setHeader("ntj_request_header", inbound.headers().get("ntj_request_header"));
httpclient.execute(httpGet, new FutureCallback() {
@Override
@@ -93,48 +92,36 @@ public void completed(final HttpResponse endpointResponse) {
} catch (Exception e) {
e.printStackTrace();
} finally {
-
+
}
}
-
+
@Override
public void failed(final Exception ex) {
httpGet.abort();
ex.printStackTrace();
}
-
+
@Override
public void cancelled() {
httpGet.abort();
}
});
}
-
+
private void handleResponse(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx, final HttpResponse endpointResponse) throws Exception {
FullHttpResponse response = null;
try {
-// String value = "hello,kimmking";
-// response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
-// response.headers().set("Content-Type", "application/json");
-// response.headers().setInt("Content-Length", response.content().readableBytes());
-
-
byte[] body = EntityUtils.toByteArray(endpointResponse.getEntity());
-// System.out.println(new String(body));
-// System.out.println(body.length);
-
+
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(body));
response.headers().set("Content-Type", "application/json");
response.headers().setInt("Content-Length", Integer.parseInt(endpointResponse.getFirstHeader("Content-Length").getValue()));
+ printHeaders(response);
+ responseFilter.filter(response);
- filter.filter(response);
-
-// for (Header e : endpointResponse.getAllHeaders()) {
-// //response.headers().set(e.getName(),e.getValue());
-// System.out.println(e.getName() + " => " + e.getValue());
-// }
-
+ printHeaders(response);
} catch (Exception e) {
e.printStackTrace();
response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
@@ -144,20 +131,23 @@ private void handleResponse(final FullHttpRequest fullRequest, final ChannelHand
if (!HttpUtil.isKeepAlive(fullRequest)) {
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
- //response.headers().set(CONNECTION, KEEP_ALIVE);
ctx.write(response);
}
}
ctx.flush();
- //ctx.close();
}
-
+
}
-
+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
-
-
+
+ private void printHeaders(FullHttpResponse response) {
+ for (String name : response.headers().names()) {
+ System.out.println(String.format("%s => %s", name, response.headers().get(name)));
+ }
+ }
+
}
diff --git a/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/HttpClientUtil.java b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/HttpClientUtil.java
new file mode 100644
index 00000000..5463d9b5
--- /dev/null
+++ b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/HttpClientUtil.java
@@ -0,0 +1,58 @@
+package io.github.ningtianjing.homework02;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+/**
+ * description:
+ * author:ningtianjing
+ * date: 2021/1/27 16:00
+ */
+
+public class HttpClientUtil {
+
+ private static Logger log = LoggerFactory.getLogger("nio-test");
+
+ public static String doGet(String url ) throws Exception{
+ CloseableHttpClient httpClient = null;
+ HttpGet httpGet = null;
+ String result = "no response.";
+ try {
+ httpClient = HttpClients.createDefault();
+ httpGet = new HttpGet(url);
+ HttpResponse response = httpClient.execute(httpGet);
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+ result = "Get Response Success, body : " + EntityUtils.toString(response.getEntity());
+ }
+ } catch (Exception e) {
+ log.error("httpClient doGet got exception: {}", e.getStackTrace());
+ throw e;
+ } finally {
+ try {
+ if (httpGet != null) {
+ httpGet.releaseConnection();
+ }
+ if (httpClient != null){
+ httpClient.close();
+ }
+ } catch (IOException e) {
+ log.error("httpClient doGet got exception: {}", e.getStackTrace());
+ }
+ }
+ return result;
+ }
+
+ public static void main(String[] args) throws Exception{
+ String response = HttpClientUtil.doGet("http://127.0.0.1:8808");
+ log.info("Response : {} ", response);
+ }
+
+}
diff --git a/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/HttpServer01.java b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/HttpServer01.java
new file mode 100644
index 00000000..330e747d
--- /dev/null
+++ b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/HttpServer01.java
@@ -0,0 +1,38 @@
+package io.github.ningtianjing.homework02;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+// 单线程的socket程序
+public class HttpServer01 {
+ public static void main(String[] args) throws IOException{
+ ServerSocket serverSocket = new ServerSocket(8801);
+ while (true) {
+ try {
+ Socket socket = serverSocket.accept();
+ service(socket);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void service(Socket socket) {
+ try {
+ Thread.sleep(5);
+ PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
+ printWriter.println("HTTP/1.1 200 OK");
+ printWriter.println("Content-Type:text/html;charset=utf-8");
+ String body = "hello,nio1";
+ printWriter.println("Content-Length:" + body.getBytes().length);
+ printWriter.println();
+ printWriter.write(body);
+ printWriter.close();
+ socket.close();
+ } catch (IOException | InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
diff --git a/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/netty/HttpHandler.java b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/netty/HttpHandler.java
new file mode 100644
index 00000000..eeab8fcc
--- /dev/null
+++ b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/netty/HttpHandler.java
@@ -0,0 +1,76 @@
+package io.github.ningtianjing.homework02.netty;
+
+import io.github.ningtianjing.homework02.HttpClientUtil;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.handler.codec.http.DefaultFullHttpResponse;
+import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.handler.codec.http.FullHttpResponse;
+import io.netty.handler.codec.http.HttpUtil;
+import io.netty.util.ReferenceCountUtil;
+
+import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
+import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
+import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
+import static io.netty.handler.codec.http.HttpResponseStatus.OK;
+import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
+
+public class HttpHandler extends ChannelInboundHandlerAdapter {
+
+ @Override
+ public void channelReadComplete(ChannelHandlerContext ctx) {
+ ctx.flush();
+ }
+
+ @Override
+ public void channelRead(ChannelHandlerContext ctx, Object msg) {
+ try {
+ //logger.info("channelRead流量接口请求开始,时间为{}", startTime);
+ FullHttpRequest fullRequest = (FullHttpRequest) msg;
+ String uri = fullRequest.uri();
+ //logger.info("接收到的请求url为{}", uri);
+ if (uri.contains("/test")) {
+ handlerTest(fullRequest, ctx);
+ }
+
+ } catch(Exception e) {
+ e.printStackTrace();
+ } finally {
+ ReferenceCountUtil.release(msg);
+ }
+ }
+
+ private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
+ FullHttpResponse response = null;
+ try {
+ //modify by ningtianjing 2021-01-27
+ String value = HttpClientUtil.doGet("http://localhost:8801");
+
+ response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
+ response.headers().set("Content-Type", "application/json");
+ response.headers().setInt("Content-Length", response.content().readableBytes());
+
+ } catch (Exception e) {
+ System.out.println("处理出错:"+e.getMessage());
+ response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
+ } finally {
+ if (fullRequest != null) {
+ if (!HttpUtil.isKeepAlive(fullRequest)) {
+ ctx.write(response).addListener(ChannelFutureListener.CLOSE);
+ } else {
+ response.headers().set(CONNECTION, KEEP_ALIVE);
+ ctx.write(response);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+ cause.printStackTrace();
+ ctx.close();
+ }
+
+}
diff --git a/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/netty/HttpInitializer.java b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/netty/HttpInitializer.java
new file mode 100644
index 00000000..a1aef5e0
--- /dev/null
+++ b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/netty/HttpInitializer.java
@@ -0,0 +1,19 @@
+package io.github.ningtianjing.homework02.netty;
+
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.handler.codec.http.HttpObjectAggregator;
+import io.netty.handler.codec.http.HttpServerCodec;
+
+public class HttpInitializer extends ChannelInitializer {
+
+ @Override
+ public void initChannel(SocketChannel ch) {
+ ChannelPipeline p = ch.pipeline();
+ p.addLast(new HttpServerCodec());
+ //p.addLast(new HttpServerExpectContinueHandler());
+ p.addLast(new HttpObjectAggregator(1024 * 1024));
+ p.addLast(new HttpHandler());
+ }
+}
diff --git a/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/netty/NettyHttpServer.java b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/netty/NettyHttpServer.java
new file mode 100644
index 00000000..7c648402
--- /dev/null
+++ b/02nio/nio02/src/main/java/io/github/ningtianjing/homework02/netty/NettyHttpServer.java
@@ -0,0 +1,48 @@
+package io.github.ningtianjing.homework02.netty;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.buffer.PooledByteBufAllocator;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.EpollChannelOption;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.logging.LogLevel;
+import io.netty.handler.logging.LoggingHandler;
+
+public class NettyHttpServer {
+ public static void main(String[] args) throws InterruptedException {
+
+ int port = 8808;
+
+ EventLoopGroup bossGroup = new NioEventLoopGroup(2);
+ EventLoopGroup workerGroup = new NioEventLoopGroup(16);
+
+ try {
+ ServerBootstrap b = new ServerBootstrap();
+ b.option(ChannelOption.SO_BACKLOG, 128)
+ .childOption(ChannelOption.TCP_NODELAY, true)
+ .childOption(ChannelOption.SO_KEEPALIVE, true)
+ .childOption(ChannelOption.SO_REUSEADDR, true)
+ .childOption(ChannelOption.SO_RCVBUF, 32 * 1024)
+ .childOption(ChannelOption.SO_SNDBUF, 32 * 1024)
+ .childOption(EpollChannelOption.SO_REUSEPORT, true)
+ .childOption(ChannelOption.SO_KEEPALIVE, true)
+ .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
+
+ b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
+ .handler(new LoggingHandler(LogLevel.INFO))
+ .childHandler(new HttpInitializer());
+
+ Channel ch = b.bind(port).sync().channel();
+ System.out.println("开启netty http服务器,监听地址和端口为 http://127.0.0.1:" + port + '/');
+ ch.closeFuture().sync();
+ } finally {
+ bossGroup.shutdownGracefully();
+ workerGroup.shutdownGracefully();
+ }
+
+
+ }
+}
diff --git a/02nio/nio02/src/main/java/io/github/ningtianjing/homework03/BackendServer.java b/02nio/nio02/src/main/java/io/github/ningtianjing/homework03/BackendServer.java
new file mode 100644
index 00000000..1184699a
--- /dev/null
+++ b/02nio/nio02/src/main/java/io/github/ningtianjing/homework03/BackendServer.java
@@ -0,0 +1,48 @@
+package io.github.ningtianjing.homework03;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+/**
+ * 创建了一个固定大小的线程池处理请求
+ */
+public class BackendServer {
+
+ public static final Integer PORT = 8808;
+
+ public static void main(String[] args) throws IOException{
+
+ ExecutorService executorService = Executors.newFixedThreadPool(
+ Runtime.getRuntime().availableProcessors() + 2);
+ final ServerSocket serverSocket = new ServerSocket(PORT);
+ while (true) {
+ try {
+ final Socket socket = serverSocket.accept();
+ executorService.execute(() -> service(socket));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private static void service(Socket socket) {
+ try {
+ Thread.sleep(10);
+ PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
+ printWriter.println("HTTP/1.1 200 OK");
+ printWriter.println("Content-Type:text/html;charset=utf-8");
+ String body = "hello,nio by ExecutorService";
+ printWriter.println("Content-Length:" + body.getBytes().length);
+ printWriter.println();
+ printWriter.write(body);
+ printWriter.close();
+ socket.close();
+ } catch (IOException | InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
diff --git a/02nio/nio02/src/main/java/io/github/ningtianjing/homework03/filter/MyHttpRequestFilter.java b/02nio/nio02/src/main/java/io/github/ningtianjing/homework03/filter/MyHttpRequestFilter.java
new file mode 100644
index 00000000..1b72e33a
--- /dev/null
+++ b/02nio/nio02/src/main/java/io/github/ningtianjing/homework03/filter/MyHttpRequestFilter.java
@@ -0,0 +1,19 @@
+package io.github.ningtianjing.homework03.filter;
+
+import io.github.kimmking.gateway.filter.HttpRequestFilter;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.http.FullHttpRequest;
+
+/**
+ * description:
+ * author:ningtianjing
+ * date: 22:29 2021/1/29
+ */
+public class MyHttpRequestFilter implements HttpRequestFilter {
+
+
+ @Override
+ public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
+ fullRequest.headers().set("ntj_request_header", "ntjRequestHeaderFilter");
+ }
+}
diff --git a/02nio/nio02/src/main/java/io/github/ningtianjing/homework03/filter/MyHttpResponseFilter.java b/02nio/nio02/src/main/java/io/github/ningtianjing/homework03/filter/MyHttpResponseFilter.java
new file mode 100644
index 00000000..9f2bf52f
--- /dev/null
+++ b/02nio/nio02/src/main/java/io/github/ningtianjing/homework03/filter/MyHttpResponseFilter.java
@@ -0,0 +1,16 @@
+package io.github.ningtianjing.homework03.filter;
+
+import io.github.kimmking.gateway.filter.HttpResponseFilter;
+import io.netty.handler.codec.http.FullHttpResponse;
+
+/**
+ * description:
+ * author:ningtianjing
+ * date: 22:32 2021/1/29
+ */
+public class MyHttpResponseFilter implements HttpResponseFilter {
+ @Override
+ public void filter(FullHttpResponse response) {
+ response.headers().set("ntj_response_header", "ntjResponseHeaderFilter");
+ }
+}