From c399404fc2993cec5e1cfbfc047fd9ccd2c34f13 Mon Sep 17 00:00:00 2001 From: 964692548 <964692548@qq.com> Date: Mon, 2 Nov 2020 16:19:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=A8=E5=9B=9B=E4=BD=9C=E4=B8=9A1=EF=BC=9A?= =?UTF-8?q?=E6=95=B4=E5=90=88=E8=87=AA=E5=B7=B1=E7=9A=84httpclient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/impl/HttpRequestFilterImpl.java | 16 +++++ .../gateway/inbound/HttpInboundHandler.java | 7 +- .../gateway/inbound/HttpInboundServer.java | 2 +- .../httpclient/HttpOutboundHandler.java | 68 +++++++++++++++++++ 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/impl/HttpRequestFilterImpl.java create mode 100644 02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient/HttpOutboundHandler.java diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/impl/HttpRequestFilterImpl.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/impl/HttpRequestFilterImpl.java new file mode 100644 index 00000000..d56c5f4e --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/impl/HttpRequestFilterImpl.java @@ -0,0 +1,16 @@ +package io.github.kimmking.gateway.filter.impl; + +import io.github.kimmking.gateway.filter.HttpRequestFilter; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.FullHttpRequest; + +/** + * @author think + * @date 2020/11/2 + */ +public class HttpRequestFilterImpl implements HttpRequestFilter { + @Override + public void filter( FullHttpRequest fullRequest, ChannelHandlerContext ctx ) { + + } +} 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 22fb2525..f1cd1347 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,7 +1,9 @@ package io.github.kimmking.gateway.inbound; -import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler; +import io.github.kimmking.gateway.outbound.httpclient.HttpOutboundHandler; import io.netty.channel.ChannelHandlerContext; + + import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.util.ReferenceCountUtil; @@ -13,9 +15,10 @@ public class HttpInboundHandler extends ChannelInboundHandlerAdapter { private static Logger logger = LoggerFactory.getLogger(HttpInboundHandler.class); private final String proxyServer; private HttpOutboundHandler handler; - + public HttpInboundHandler(String proxyServer) { this.proxyServer = proxyServer; + // handler = new HttpOutboundHandler(this.proxyServer); handler = new HttpOutboundHandler(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 071fa9bc..8fd07081 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 @@ -29,7 +29,7 @@ public HttpInboundServer(int port, String proxyServer) { public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); - EventLoopGroup workerGroup = new NioEventLoopGroup(16); + EventLoopGroup workerGroup = new NioEventLoopGroup(1); try { ServerBootstrap b = new ServerBootstrap(); diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient/HttpOutboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient/HttpOutboundHandler.java new file mode 100644 index 00000000..e7b47f2c --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient/HttpOutboundHandler.java @@ -0,0 +1,68 @@ +package io.github.kimmking.gateway.outbound.httpclient; + +import io.netty.buffer.Unpooled; +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 org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +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; + +/** + * @author isaac + * @date 2020/11/2 + */ +public class HttpOutboundHandler { + + private String backendUrl; + + public HttpOutboundHandler( String serverUrl) { + this.backendUrl = serverUrl.endsWith("/") ? serverUrl.substring(0,serverUrl.length()-1) : serverUrl; + } + + public void handle( final FullHttpRequest fullRequest, final ChannelHandlerContext ctx) throws IOException { + final String url = this.backendUrl + fullRequest.uri(); + fetchGet(fullRequest, ctx, url); + + } + private void fetchGet(final FullHttpRequest inbound, final ChannelHandlerContext ctx, final String url){ + final HttpGet httpGet = new HttpGet(url); + HttpClient client = HttpClients.createDefault(); + HttpResponse response = null; + try { + response = client.execute(httpGet); + } catch (IOException e) { + e.printStackTrace(); + } + handleResponse(inbound, ctx, response); + } + private void handleResponse(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx, final HttpResponse endpointResponse) { + FullHttpResponse response = null; + try { + byte[] body = EntityUtils.toByteArray(endpointResponse.getEntity()); + System.out.println(new String(body)); + + response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(body)); + } catch (Exception e) { + e.printStackTrace(); + response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); + } finally { + if (fullRequest != null) { + ctx.write(response); + } + ctx.flush(); + ctx.close(); + } + + } +} +