From 09f3952cadd77900926886b48b620eed46975429 Mon Sep 17 00:00:00 2001 From: dongjiang Date: Wed, 4 Nov 2020 22:13:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=EF=BC=8C=E4=B8=BB=E8=A6=81=E6=98=AF=E5=91=A8=E5=85=AD=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=BF=87=E6=BB=A4=E5=99=A8=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/filter/HttpRequestFilterImpl.java | 44 +++++++ .../gateway/filter/UriHttpRequestFilter.java | 22 ++++ .../outbound/netty4/NettyHttpClient.java | 114 ++++++++++-------- .../NettyHttpClientOutboundHandler.java | 17 ++- .../router/HttpEndpointRouterImpl.java | 14 +++ 5 files changed, 162 insertions(+), 49 deletions(-) create mode 100644 02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilterImpl.java create mode 100644 02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/UriHttpRequestFilter.java create mode 100644 02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouterImpl.java diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilterImpl.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilterImpl.java new file mode 100644 index 00000000..692a06b0 --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HttpRequestFilterImpl.java @@ -0,0 +1,44 @@ +package io.github.kimmking.gateway.filter; + + +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.*; + +/** + *

+ * dongjiang + * reference: + * HJ43 + * + *

+ */ +public class HttpRequestFilterImpl implements HttpRequestFilter{ + + @Override + public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) { + HttpMethod method = fullRequest.method(); + if(method.equals(HttpMethod.POST)){ + FullHttpRequest response =null; + + try{ + response = (FullHttpRequest) new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.METHOD_NOT_ALLOWED); + }catch (Exception e){ + e.printStackTrace(); + response= (FullHttpRequest) new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.NO_CONTENT); + }finally { + if(fullRequest!=null){ + if(!HttpUtil.isKeepAlive(fullRequest)){ + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + }else { + ctx.write(response); + } + } + ctx.flush(); + ctx.close(); + } + + + } + } +} diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/UriHttpRequestFilter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/UriHttpRequestFilter.java new file mode 100644 index 00000000..58b89a29 --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/UriHttpRequestFilter.java @@ -0,0 +1,22 @@ +package io.github.kimmking.gateway.filter; + +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.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; + +public class UriHttpRequestFilter implements HttpRequestFilter{ + + @Override + public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) { + String url =fullRequest.uri(); + System.out.println("url:" + url); + if(!url.contains("api/hello")){ + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); + ctx.write(response); + ctx.flush(); + ctx.close(); + } + } +} 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..650cf6d2 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,69 @@ -//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(); -// } -// -// } -// +package io.github.kimmking.gateway.outbound.netty4;//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; + +import java.net.URI; +import java.net.URISyntaxException; + +public class NettyHttpClient { + + private String host; + private int port; + public NettyHttpClient(String proxyServer){ + try { + URI uri = new URI(proxyServer); + this.host=uri.getHost(); + this.port=uri.getPort(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + } + + 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); +//// client.connect("127.0.0.1", 8844); +// client.connect("127.0.0.1", 8088); // } -//} \ No newline at end of file +} \ 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..1396cb76 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 @@ -3,8 +3,23 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +/** + *

+ * reference : + * HJ43 + * huyanatown + + *

+ + */ public class NettyHttpClientOutboundHandler extends ChannelInboundHandlerAdapter { - + + private NettyHttpClient client; + public NettyHttpClientOutboundHandler(String proxyServer){ + this.client= new NettyHttpClient(proxyServer); + } + + @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouterImpl.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouterImpl.java new file mode 100644 index 00000000..d063feb3 --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/router/HttpEndpointRouterImpl.java @@ -0,0 +1,14 @@ +package io.github.kimmking.gateway.router; + +import java.util.List; +import java.util.Random; + +public class HttpEndpointRouterImpl implements HttpEndpointRouter{ + @Override + public String route(List endpoints) { + int size = endpoints.size(); + return endpoints.get(new Random().nextInt(size)); + } + + +}