diff --git a/02nio/nio02/pom.xml b/02nio/nio02/pom.xml
index 005de90a..f6b818ca 100644
--- a/02nio/nio02/pom.xml
+++ b/02nio/nio02/pom.xml
@@ -1,86 +1,115 @@
-
-
- 4.0.0
-
- io.github.kimmking
- netty-gateway
- 0.0.1-SNAPSHOT
- jar
-
- netty-gateway
- Demo project for Spring Boot
-
-
- org.springframework.boot
- spring-boot-starter-parent
- 2.0.4.RELEASE
-
-
-
-
- UTF-8
- UTF-8
- 1.8
-
-
-
-
-
- io.netty
- netty-all
- 4.1.45.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
-
-
-
- org.projectlombok
- lombok
-
-
-
-
-
-
-
-
-
- org.springframework.boot
- spring-boot-maven-plugin
-
-
-
-
-
-
+
+
+ 4.0.0
+
+ io.github.kimmking
+ netty-gateway
+ 0.0.1-SNAPSHOT
+ jar
+
+ netty-gateway
+ Demo project for Spring Boot
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.0.4.RELEASE
+
+
+
+
+ UTF-8
+ UTF-8
+ 1.8
+
+
+
+
+
+ io.netty
+ netty-all
+ 4.1.45.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
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+
+ com.alibaba
+ fastjson
+ 1.2.58
+
+
+ org.apache.httpcomponents
+ httpcore
+ 4.4.9
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.5.6
+
+
+ log4j
+ log4j
+ 1.2.17
+
+
+ org.slf4j
+ slf4j-api
+ 1.7.7
+
+
+ org.slf4j
+ slf4j-nop
+ 1.7.2
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
+
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..05c27540 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
@@ -1,36 +1,36 @@
-package io.github.kimmking.gateway;
-
-
-import io.github.kimmking.gateway.inbound.HttpInboundServer;
-
-import java.util.Arrays;
-
-public class NettyServerApplication {
-
- public final static String GATEWAY_NAME = "NIOGateway";
- public final static String GATEWAY_VERSION = "3.0.0";
-
- public static void main(String[] args) {
-
- String proxyPort = System.getProperty("proxyPort","8888");
-
- // 这是之前的单个后端url的例子
-// String proxyServer = System.getProperty("proxyServer","http://localhost:8088");
-// // http://localhost:8888/api/hello ==> gateway API
-// // http://localhost:8088/api/hello ==> backend service
- // java -Xmx512m gateway-server-0.0.1-SNAPSHOT.jar #作为后端服务
-
-
- // 这是多个后端url走随机路由的例子
- String proxyServers = System.getProperty("proxyServers","http://localhost:8801,http://localhost:8802");
- 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());
- try {
- server.run();
- }catch (Exception ex){
- ex.printStackTrace();
- }
- }
-}
+package io.github.kimmking.gateway;
+
+
+import io.github.kimmking.gateway.inbound.HttpInboundServer;
+
+import java.util.Arrays;
+
+public class NettyServerApplication {
+
+ public final static String GATEWAY_NAME = "NIOGateway";
+ public final static String GATEWAY_VERSION = "3.0.0";
+
+ public static void main(String[] args) {
+
+ String proxyPort = System.getProperty("proxyPort","8888");
+
+ // 这是之前的单个后端url的例子
+// String proxyServer = System.getProperty("proxyServer","http://localhost:8088");
+// // http://localhost:8888/api/hello ==> gateway API
+// // http://localhost:8088/api/hello ==> backend service
+ // java -Xmx512m gateway-server-0.0.1-SNAPSHOT.jar #作为后端服务
+
+
+ // 这是多个后端url走随机路由的例子
+ String proxyServers = System.getProperty("proxyServers","http://localhost:8801,http://localhost:8802");
+ 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());
+ try {
+ server.run();
+ }catch (Exception ex){
+ ex.printStackTrace();
+ }
+ }
+}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpRequestFilter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpRequestFilter.java
index af5c37fb..ee02a3c3 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpRequestFilter.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpRequestFilter.java
@@ -1,12 +1,31 @@
-package io.github.kimmking.gateway.filter;
-
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.http.FullHttpRequest;
-
-public class HeaderHttpRequestFilter implements HttpRequestFilter {
-
- @Override
- public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
- fullRequest.headers().set("mao", "soul");
- }
-}
+package io.github.kimmking.gateway.filter;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.handler.codec.http.HttpRequest;
+
+import java.net.InetSocketAddress;
+
+public class HeaderHttpRequestFilter implements HttpRequestFilter {
+
+ @Override
+ public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
+ String uri = fullRequest.uri();
+ String ip = HeaderHttpRequestFilter.getIpAddr(ctx);
+ System.out.println("ip="+ip+"/uri="+uri);
+ fullRequest.headers().set("mao", "soul");
+ }
+ public static String getIpAddr(ChannelHandlerContext ctx) {
+ String ip = "";
+ try{
+ InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
+ ip = insocket.getAddress().getHostAddress();
+ System.out.println(ip);
+ }catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return ip;
+ }
+
+}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpResponseFilter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpResponseFilter.java
index 53493fb4..858df95a 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpResponseFilter.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpResponseFilter.java
@@ -1,10 +1,10 @@
-package io.github.kimmking.gateway.filter;
-
-import io.netty.handler.codec.http.FullHttpResponse;
-
-public class HeaderHttpResponseFilter implements HttpResponseFilter {
- @Override
- public void filter(FullHttpResponse response) {
- response.headers().set("kk", "java-1-nio");
- }
-}
+package io.github.kimmking.gateway.filter;
+
+import io.netty.handler.codec.http.FullHttpResponse;
+
+public class HeaderHttpResponseFilter implements HttpResponseFilter {
+ @Override
+ public void filter(FullHttpResponse response) {
+ response.headers().set("kk", "java-1-nio");
+ }
+}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilter.java
index 31253b40..8c0e2050 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilter.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilter.java
@@ -1,10 +1,10 @@
-package io.github.kimmking.gateway.filter;
-
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.http.FullHttpRequest;
-
-public interface HttpRequestFilter {
-
- void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx);
-
-}
+package io.github.kimmking.gateway.filter;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.http.FullHttpRequest;
+
+public interface HttpRequestFilter {
+
+ void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx);
+
+}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpResponseFilter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpResponseFilter.java
index c169e430..90d8fc6a 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpResponseFilter.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpResponseFilter.java
@@ -1,9 +1,9 @@
-package io.github.kimmking.gateway.filter;
-
-import io.netty.handler.codec.http.FullHttpResponse;
-
-public interface HttpResponseFilter {
-
- void filter(FullHttpResponse response);
-
-}
+package io.github.kimmking.gateway.filter;
+
+import io.netty.handler.codec.http.FullHttpResponse;
+
+public interface HttpResponseFilter {
+
+ void filter(FullHttpResponse response);
+
+}
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..1579298d 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,81 +1,82 @@
-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.netty.channel.ChannelHandlerContext;
-import io.netty.channel.ChannelInboundHandlerAdapter;
-import io.netty.handler.codec.http.FullHttpRequest;
-import io.netty.util.ReferenceCountUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.List;
-
-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();
-
- public HttpInboundHandler(List proxyServer) {
- this.proxyServer = proxyServer;
- this.handler = new HttpOutboundHandler(this.proxyServer);
- }
-
- @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);
-// }
-
- handler.handle(fullRequest, ctx, filter);
-
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- ReferenceCountUtil.release(msg);
- }
- }
-
-// private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
-// 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());
-//
-// } catch (Exception e) {
-// logger.error("处理测试接口出错", e);
-// 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();
-// }
-
-}
+package io.github.kimmking.gateway.inbound;
+
+import com.alibaba.fastjson.JSONObject;
+import io.github.kimmking.gateway.filter.HeaderHttpRequestFilter;
+import io.github.kimmking.gateway.filter.HttpRequestFilter;
+import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.util.ReferenceCountUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+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();
+
+ public HttpInboundHandler(List proxyServer) {
+ this.proxyServer = proxyServer;
+ this.handler = new HttpOutboundHandler(this.proxyServer);
+ }
+
+ @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);
+// }
+
+ handler.handle(fullRequest, ctx, filter);
+
+ } catch(Exception e) {
+ e.printStackTrace();
+ } finally {
+ ReferenceCountUtil.release(msg);
+ }
+ }
+
+// private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
+// 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());
+//
+// } catch (Exception e) {
+// logger.error("处理测试接口出错", e);
+// 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/kimmking/gateway/inbound/HttpInboundInitializer.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundInitializer.java
index 1d651fb1..713de470 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundInitializer.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundInitializer.java
@@ -1,33 +1,33 @@
-package io.github.kimmking.gateway.inbound;
-
-import io.github.kimmking.gateway.filter.HttpRequestFilter;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.ChannelInitializer;
-import io.netty.channel.ChannelPipeline;
-import io.netty.channel.socket.SocketChannel;
-import io.netty.handler.codec.http.FullHttpRequest;
-import io.netty.handler.codec.http.HttpObjectAggregator;
-import io.netty.handler.codec.http.HttpServerCodec;
-
-import java.util.List;
-
-public class HttpInboundInitializer extends ChannelInitializer {
-
- private List proxyServer;
-
- public HttpInboundInitializer(List proxyServer) {
- this.proxyServer = proxyServer;
- }
-
- @Override
- public void initChannel(SocketChannel ch) {
- ChannelPipeline p = ch.pipeline();
-// if (sslCtx != null) {
-// p.addLast(sslCtx.newHandler(ch.alloc()));
-// }
- p.addLast(new HttpServerCodec());
- //p.addLast(new HttpServerExpectContinueHandler());
- p.addLast(new HttpObjectAggregator(1024 * 1024));
- p.addLast(new HttpInboundHandler(this.proxyServer));
- }
-}
+package io.github.kimmking.gateway.inbound;
+
+import io.github.kimmking.gateway.filter.HttpRequestFilter;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.handler.codec.http.HttpObjectAggregator;
+import io.netty.handler.codec.http.HttpServerCodec;
+
+import java.util.List;
+
+public class HttpInboundInitializer extends ChannelInitializer {
+
+ private List proxyServer;
+
+ public HttpInboundInitializer(List proxyServer) {
+ this.proxyServer = proxyServer;
+ }
+
+ @Override
+ public void initChannel(SocketChannel ch) {
+ ChannelPipeline p = ch.pipeline();
+// if (sslCtx != null) {
+// p.addLast(sslCtx.newHandler(ch.alloc()));
+// }
+ p.addLast(new HttpServerCodec());
+ //p.addLast(new HttpServerExpectContinueHandler());
+ p.addLast(new HttpObjectAggregator(1024 * 1024));
+ p.addLast(new HttpInboundHandler(this.proxyServer));
+ }
+}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundServer.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundServer.java
index 97d54ccd..92745140 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundServer.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundServer.java
@@ -1,60 +1,60 @@
-package io.github.kimmking.gateway.inbound;
-
-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;
-import lombok.Data;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.List;
-
-@Data
-public class HttpInboundServer {
-
- private int port;
-
- private List proxyServers;
-
- public HttpInboundServer(int port, List proxyServers) {
- this.port=port;
- this.proxyServers = proxyServers;
- }
-
- public void run() throws Exception {
-
- EventLoopGroup bossGroup = new NioEventLoopGroup(1);
- 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.DEBUG))
- .childHandler(new HttpInboundInitializer(this.proxyServers));
-
- 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();
- }
- }
-}
+package io.github.kimmking.gateway.inbound;
+
+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;
+import lombok.Data;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+@Data
+public class HttpInboundServer {
+
+ private int port;
+
+ private List proxyServers;
+
+ public HttpInboundServer(int port, List proxyServers) {
+ this.port=port;
+ this.proxyServers = proxyServers;
+ }
+
+ public void run() throws Exception {
+
+ EventLoopGroup bossGroup = new NioEventLoopGroup(1);
+ 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.DEBUG))
+ .childHandler(new HttpInboundInitializer(this.proxyServers));
+
+ 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/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java
index c20c9be5..13639be9 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,163 +1,176 @@
-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.netty.buffer.Unpooled;
-import io.netty.channel.ChannelFutureListener;
-import io.netty.channel.ChannelHandlerContext;
-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 org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.concurrent.FutureCallback;
-import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
-import org.apache.http.impl.nio.reactor.IOReactorConfig;
-import org.apache.http.protocol.HTTP;
-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;
-import static io.netty.handler.codec.http.HttpResponseStatus.OK;
-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();
- HttpEndpointRouter router = new RandomHttpEndpointRouter();
-
- public HttpOutboundHandler(List backends) {
-
- this.backendUrls = backends.stream().map(this::formatUrl).collect(Collectors.toList());
-
- int cores = Runtime.getRuntime().availableProcessors();
- long keepAliveTime = 1000;
- int queueSize = 2048;
- RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();//.DiscardPolicy();
- 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)
- .setKeepAliveStrategy((response,context) -> 6000)
- .build();
- httpclient.start();
- }
-
- 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"));
-
- httpclient.execute(httpGet, new FutureCallback() {
- @Override
- public void completed(final HttpResponse endpointResponse) {
- try {
- handleResponse(inbound, ctx, 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()));
-
- filter.filter(response);
-
-// for (Header e : endpointResponse.getAllHeaders()) {
-// //response.headers().set(e.getName(),e.getValue());
-// System.out.println(e.getName() + " => " + e.getValue());
-// }
-
- } catch (Exception e) {
- e.printStackTrace();
- response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
- exceptionCaught(ctx, e);
- } finally {
- if (fullRequest != null) {
- 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();
- }
-
-
-}
+package io.github.kimmking.gateway.outbound.httpclient4;
+
+
+import com.alibaba.fastjson.JSONObject;
+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.outbound.utils.Httputils;
+import io.github.kimmking.gateway.router.HttpEndpointRouter;
+import io.github.kimmking.gateway.router.RandomHttpEndpointRouter;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+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 org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.concurrent.FutureCallback;
+import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
+import org.apache.http.impl.nio.client.HttpAsyncClients;
+import org.apache.http.impl.nio.reactor.IOReactorConfig;
+import org.apache.http.protocol.HTTP;
+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;
+import static io.netty.handler.codec.http.HttpResponseStatus.OK;
+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();
+ HttpEndpointRouter router = new RandomHttpEndpointRouter();
+
+ public HttpOutboundHandler(List backends) {
+ System.out.println("backendsList = "+ JSONObject.toJSONString(backends));
+ this.backendUrls = backends.stream().map(this::formatUrl).collect(Collectors.toList());
+ System.out.println("backendUrls After = "+ JSONObject.toJSONString(backendUrls));
+ int cores = Runtime.getRuntime().availableProcessors();
+ long keepAliveTime = 1000;
+ int queueSize = 2048;
+ RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();//.DiscardPolicy();
+ 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)
+ .setKeepAliveStrategy((response,context) -> 6000)
+ .build();
+ httpclient.start();
+ }
+
+ 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(()->httpUtile(fullRequest, ctx, url));
+ }
+ private void httpUtile(final FullHttpRequest inbound, final ChannelHandlerContext ctx, final String url) {
+ try {
+ HttpResponse result = Httputils.doGetJson(url,new JSONObject());
+ System.out.println("result = "+ result);
+ handleResponse(inbound, ctx, result);
+ }catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+
+ 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"));
+
+ httpclient.execute(httpGet, new FutureCallback() {
+ @Override
+ public void completed(final HttpResponse endpointResponse) {
+ try {
+ handleResponse(inbound, ctx, 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()));
+
+ filter.filter(response);
+
+// for (Header e : endpointResponse.getAllHeaders()) {
+// //response.headers().set(e.getName(),e.getValue());
+// System.out.println(e.getName() + " => " + e.getValue());
+// }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
+ exceptionCaught(ctx, e);
+ } finally {
+ if (fullRequest != null) {
+ 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();
+ }
+
+
+}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/NamedThreadFactory.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/NamedThreadFactory.java
index 726beb9d..bd2f3aee 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/NamedThreadFactory.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/NamedThreadFactory.java
@@ -1,32 +1,32 @@
-package io.github.kimmking.gateway.outbound.httpclient4;
-
-import java.util.concurrent.ThreadFactory;
-import java.util.concurrent.atomic.AtomicInteger;
-
-public class NamedThreadFactory implements ThreadFactory {
-
- private final ThreadGroup group;
- private final AtomicInteger threadNumber = new AtomicInteger(1);
-
- private final String namePrefix;
- private final boolean daemon;
-
- public NamedThreadFactory(String namePrefix, boolean daemon) {
- this.daemon = daemon;
- SecurityManager s = System.getSecurityManager();
- group = (s != null) ? s.getThreadGroup() :
- Thread.currentThread().getThreadGroup();
- this.namePrefix = namePrefix;
- }
-
- public NamedThreadFactory(String namePrefix) {
- this(namePrefix, false);
- }
-
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(group, r, namePrefix + "-thread-" + threadNumber.getAndIncrement(), 0);
- t.setDaemon(daemon);
- return t;
- }
+package io.github.kimmking.gateway.outbound.httpclient4;
+
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class NamedThreadFactory implements ThreadFactory {
+
+ private final ThreadGroup group;
+ private final AtomicInteger threadNumber = new AtomicInteger(1);
+
+ private final String namePrefix;
+ private final boolean daemon;
+
+ public NamedThreadFactory(String namePrefix, boolean daemon) {
+ this.daemon = daemon;
+ SecurityManager s = System.getSecurityManager();
+ group = (s != null) ? s.getThreadGroup() :
+ Thread.currentThread().getThreadGroup();
+ this.namePrefix = namePrefix;
+ }
+
+ public NamedThreadFactory(String namePrefix) {
+ this(namePrefix, false);
+ }
+
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(group, r, namePrefix + "-thread-" + threadNumber.getAndIncrement(), 0);
+ t.setDaemon(daemon);
+ return t;
+ }
}
\ No newline at end of file
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java
index 79aeb148..0d7ef210 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java
@@ -1,51 +1,51 @@
-//package io.github.kimmking.gateway.outbound;
-//
-//import io.netty.bootstrap.Bootstrap;
-//import io.netty.channel.ChannelFuture;
-//import io.netty.channel.ChannelInitializer;
-//import io.netty.channel.ChannelOption;
-//import io.netty.channel.EventLoopGroup;
-//import io.netty.channel.nio.NioEventLoopGroup;
-//import io.netty.channel.socket.SocketChannel;
-//import io.netty.channel.socket.nio.NioSocketChannel;
-//import io.netty.handler.codec.http.HttpRequestEncoder;
-//import io.netty.handler.codec.http.HttpResponseDecoder;
-//
-//public class NettyHttpClient {
-// public void connect(String host, int port) throws Exception {
-// EventLoopGroup workerGroup = new NioEventLoopGroup();
-//
-// try {
-// Bootstrap b = new Bootstrap();
-// b.group(workerGroup);
-// b.channel(NioSocketChannel.class);
-// b.option(ChannelOption.SO_KEEPALIVE, true);
-// b.handler(new ChannelInitializer() {
-// @Override
-// public void initChannel(SocketChannel ch) throws Exception {
-// // 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
-// ch.pipeline().addLast(new HttpResponseDecoder());
-// 客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
-// ch.pipeline().addLast(new HttpRequestEncoder());
-// ch.pipeline().addLast(new HttpClientOutboundHandler());
-// }
-// });
-//
-// // Start the client.
-// ChannelFuture f = b.connect(host, port).sync();
-//
-//
-// f.channel().write(request);
-// f.channel().flush();
-// f.channel().closeFuture().sync();
-// } finally {
-// workerGroup.shutdownGracefully();
-// }
-//
-// }
-//
-// public static void main(String[] args) throws Exception {
-// NettyHttpClient client = new NettyHttpClient();
-// client.connect("127.0.0.1", 8844);
-// }
+//package io.github.kimmking.gateway.outbound;
+//
+//import io.netty.bootstrap.Bootstrap;
+//import io.netty.channel.ChannelFuture;
+//import io.netty.channel.ChannelInitializer;
+//import io.netty.channel.ChannelOption;
+//import io.netty.channel.EventLoopGroup;
+//import io.netty.channel.nio.NioEventLoopGroup;
+//import io.netty.channel.socket.SocketChannel;
+//import io.netty.channel.socket.nio.NioSocketChannel;
+//import io.netty.handler.codec.http.HttpRequestEncoder;
+//import io.netty.handler.codec.http.HttpResponseDecoder;
+//
+//public class NettyHttpClient {
+// public void connect(String host, int port) throws Exception {
+// EventLoopGroup workerGroup = new NioEventLoopGroup();
+//
+// try {
+// Bootstrap b = new Bootstrap();
+// b.group(workerGroup);
+// b.channel(NioSocketChannel.class);
+// b.option(ChannelOption.SO_KEEPALIVE, true);
+// b.handler(new ChannelInitializer() {
+// @Override
+// public void initChannel(SocketChannel ch) throws Exception {
+// // 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
+// ch.pipeline().addLast(new HttpResponseDecoder());
+// 客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
+// ch.pipeline().addLast(new HttpRequestEncoder());
+// ch.pipeline().addLast(new HttpClientOutboundHandler());
+// }
+// });
+//
+// // Start the client.
+// ChannelFuture f = b.connect(host, port).sync();
+//
+//
+// f.channel().write(request);
+// f.channel().flush();
+// f.channel().closeFuture().sync();
+// } finally {
+// workerGroup.shutdownGracefully();
+// }
+//
+// }
+//
+// public static void main(String[] args) throws Exception {
+// NettyHttpClient client = new NettyHttpClient();
+// client.connect("127.0.0.1", 8844);
+// }
//}
\ No newline at end of file
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClientOutboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClientOutboundHandler.java
index 6730cd5a..d06cd0d0 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClientOutboundHandler.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClientOutboundHandler.java
@@ -1,22 +1,22 @@
-package io.github.kimmking.gateway.outbound.netty4;
-
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.ChannelInboundHandlerAdapter;
-
-public class NettyHttpClientOutboundHandler extends ChannelInboundHandlerAdapter {
-
- @Override
- public void channelActive(ChannelHandlerContext ctx)
- throws Exception {
-
-
- }
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg)
- throws Exception {
-
-
-
- }
+package io.github.kimmking.gateway.outbound.netty4;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+
+public class NettyHttpClientOutboundHandler extends ChannelInboundHandlerAdapter {
+
+ @Override
+ public void channelActive(ChannelHandlerContext ctx)
+ throws Exception {
+
+
+ }
+
+ @Override
+ public void channelRead(ChannelHandlerContext ctx, Object msg)
+ throws Exception {
+
+
+
+ }
}
\ No newline at end of file
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/okhttp/OkhttpOutboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/okhttp/OkhttpOutboundHandler.java
index 5f194588..0e805ff5 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/okhttp/OkhttpOutboundHandler.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/okhttp/OkhttpOutboundHandler.java
@@ -1,4 +1,4 @@
-package io.github.kimmking.gateway.outbound.okhttp;
-
-public class OkhttpOutboundHandler {
-}
+package io.github.kimmking.gateway.outbound.okhttp;
+
+public class OkhttpOutboundHandler {
+}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/utils/Httputils.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/utils/Httputils.java
new file mode 100644
index 00000000..68e6dcea
--- /dev/null
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/utils/Httputils.java
@@ -0,0 +1,91 @@
+package io.github.kimmking.gateway.outbound.utils;
+
+import com.alibaba.fastjson.JSONObject;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.StatusLine;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+
+public class Httputils {
+ public static String doPostJson(String url, JSONObject json) {
+ // 创建Httpclient对象
+ CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse response = null;
+ String resultString = "";
+ try {
+ // 创建Http Post请求
+ HttpPost httpPost = new HttpPost(url);
+ // 创建请求内容
+ if(json == null ) {
+ json = new JSONObject();
+ }
+ HttpEntity postEntity = new StringEntity(json.toString(), "UTF-8");
+ httpPost.setEntity(postEntity);
+ httpPost.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
+ httpPost.setHeader("mao", "mao");
+ // 执行http请求
+ response = httpClient.execute(httpPost);
+ StatusLine statusLine = response.getStatusLine();
+ if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
+ HttpEntity entity = response.getEntity();
+ resultString = EntityUtils.toString(entity);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ response.close();
+ httpClient.close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+ System.out.println("返回的数据"+resultString);
+ return resultString;
+ }
+
+ public static HttpResponse doGetJson(String url, JSONObject json) {
+ // 创建Httpclient对象
+ CloseableHttpClient httpClient = HttpClients.createDefault();
+ HttpResponse response = null;
+ String resultString = "";
+ try {
+ // 创建Http Post请求
+ HttpGet httpPost = new HttpGet(url);
+ // 创建请求内容
+ if(json == null ) {
+ json = new JSONObject();
+ }
+
+ httpPost.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
+ httpPost.setHeader("mao", "mao");
+ // 执行http请求
+ response = httpClient.execute(httpPost);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return response;
+ }
+
+ public static void main(String[] args) {
+ JSONObject json1=new JSONObject();
+ String url="http://localhost:8801";
+ System.out.println(doPostJson(url,json1));
+ }
+
+}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouter.java
index 8e307ab7..1622c11d 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouter.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouter.java
@@ -1,17 +1,17 @@
-package io.github.kimmking.gateway.router;
-
-import java.util.List;
-
-public interface HttpEndpointRouter {
-
- String route(List endpoints);
-
- // Load Balance
- // Random
- // RoundRibbon
- // Weight
- // - server01,20
- // - server02,30
- // - server03,50
-
-}
+package io.github.kimmking.gateway.router;
+
+import java.util.List;
+
+public interface HttpEndpointRouter {
+
+ String route(List endpoints);
+
+ // Load Balance
+ // Random
+ // RoundRibbon
+ // Weight
+ // - server01,20
+ // - server02,30
+ // - server03,50
+
+}
diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/RandomHttpEndpointRouter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/RandomHttpEndpointRouter.java
index 684d1ba5..8357422c 100644
--- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/RandomHttpEndpointRouter.java
+++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/RandomHttpEndpointRouter.java
@@ -1,13 +1,17 @@
-package io.github.kimmking.gateway.router;
-
-import java.util.List;
-import java.util.Random;
-
-public class RandomHttpEndpointRouter implements HttpEndpointRouter {
- @Override
- public String route(List urls) {
- int size = urls.size();
- Random random = new Random(System.currentTimeMillis());
- return urls.get(random.nextInt(size));
- }
-}
+package io.github.kimmking.gateway.router;
+
+import com.alibaba.fastjson.JSONObject;
+
+import java.util.List;
+import java.util.Random;
+
+public class RandomHttpEndpointRouter implements HttpEndpointRouter {
+ @Override
+ public String route(List urls) {
+ int size = urls.size();
+ Random random = new Random(System.currentTimeMillis());
+ System.out.println("urls After = "+ JSONObject.toJSONString(urls));
+ return urls.get(random.nextInt(size));
+
+ }
+}