11package io .github .kimmking .gateway .inbound ;
22
3+ import io .github .kimmking .gateway .filter .HttpRequestFilter ;
34import io .github .kimmking .gateway .outbound .httpclient4 .HttpOutboundHandler ;
5+ import io .github .kimmking .gateway .outbound .myselfhttpclient .MyHttpOutboundHandler ;
6+ import io .netty .buffer .Unpooled ;
7+ import io .netty .channel .ChannelFutureListener ;
48import io .netty .channel .ChannelHandlerContext ;
59import io .netty .channel .ChannelInboundHandlerAdapter ;
6- import io .netty .handler .codec .http .FullHttpRequest ;
10+ import io .netty .handler .codec .http .* ;
711import io .netty .util .ReferenceCountUtil ;
812import org .slf4j .Logger ;
913import org .slf4j .LoggerFactory ;
1014
11- public class HttpInboundHandler extends ChannelInboundHandlerAdapter {
15+ import static io .netty .handler .codec .http .HttpHeaderValues .KEEP_ALIVE ;
16+ import static io .netty .handler .codec .http .HttpResponseStatus .NO_CONTENT ;
17+ import static io .netty .handler .codec .http .HttpResponseStatus .OK ;
18+ import static io .netty .handler .codec .http .HttpVersion .HTTP_1_1 ;
19+
20+ public class HttpInboundHandler extends ChannelInboundHandlerAdapter implements HttpRequestFilter {
1221
1322 private static Logger logger = LoggerFactory .getLogger (HttpInboundHandler .class );
1423 private final String proxyServer ;
1524 private HttpOutboundHandler handler ;
16-
25+ // 自己写的Httpclient组件
26+ private MyHttpOutboundHandler myHandler ;
27+
1728 public HttpInboundHandler (String proxyServer ) {
1829 this .proxyServer = proxyServer ;
30+ // 老师
1931 handler = new HttpOutboundHandler (this .proxyServer );
32+ // 自己写的Httpclient组件
33+ myHandler = new MyHttpOutboundHandler (this .proxyServer );
2034 }
21-
35+
2236 @ Override
2337 public void channelReadComplete (ChannelHandlerContext ctx ) {
2438 ctx .flush ();
@@ -27,50 +41,55 @@ public void channelReadComplete(ChannelHandlerContext ctx) {
2741 @ Override
2842 public void channelRead (ChannelHandlerContext ctx , Object msg ) {
2943 try {
30- //logger.info("channelRead流量接口请求开始,时间为{}", startTime);
44+ long startTime = System .currentTimeMillis ();
45+ logger .info ("channelRead流量接口请求开始,时间为{}" , startTime );
3146 FullHttpRequest fullRequest = (FullHttpRequest ) msg ;
32- // String uri = fullRequest.uri();
33- // //logger.info("接收到的请求url为{}", uri);
34- // if (uri.contains("/test")) {
35- // handlerTest(fullRequest, ctx);
36- // }
37-
38- handler .handle (fullRequest , ctx );
39-
40- } catch (Exception e ) {
47+ // 自定义过滤器
48+ filter (fullRequest ,ctx );
49+ // 自己写的HttpClient
50+ myHandler .handler (fullRequest ,ctx );
51+ // 老师写的
52+ // handler.handle(fullRequest, ctx);
53+
54+ } catch (Exception e ) {
4155 e .printStackTrace ();
4256 } finally {
4357 ReferenceCountUtil .release (msg );
4458 }
4559 }
4660
47- // private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
48- // FullHttpResponse response = null;
49- // try {
50- // String value = "hello,kimmking";
51- // response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
52- // response.headers().set("Content-Type", "application/json");
53- // response.headers().setInt("Content-Length", response.content().readableBytes());
54- //
55- // } catch (Exception e) {
56- // logger.error("处理测试接口出错", e);
57- // response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
58- // } finally {
59- // if (fullRequest != null) {
60- // if (!HttpUtil.isKeepAlive(fullRequest)) {
61- // ctx.write(response).addListener(ChannelFutureListener.CLOSE);
62- // } else {
63- // response.headers().set(CONNECTION, KEEP_ALIVE);
64- // ctx.write(response);
65- // }
66- // }
67- // }
68- // }
69- //
70- // @Override
71- // public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
72- // cause.printStackTrace();
73- // ctx.close();
74- // }
61+ @ Override
62+ public void filter (FullHttpRequest fullRequest , ChannelHandlerContext ctx ) {
63+ HttpHeaders headers = fullRequest .headers ();
64+ headers .set ("nio" ,"BAIFUKUAN" );
65+ }
66+ private void handlerTest (FullHttpRequest fullRequest , ChannelHandlerContext ctx ) {
67+ FullHttpResponse response = null ;
68+ try {
69+ String value = "hello,kimmking" ;
70+ response = new DefaultFullHttpResponse (HTTP_1_1 , OK , Unpooled .wrappedBuffer (value .getBytes ("UTF-8" )));
71+ response .headers ().set ("Content-Type" , "application/json" );
72+ response .headers ().setInt ("Content-Length" , response .content ().readableBytes ());
73+
74+ } catch (Exception e ) {
75+ logger .error ("处理测试接口出错" , e );
76+ response = new DefaultFullHttpResponse (HTTP_1_1 , NO_CONTENT );
77+ } finally {
78+ if (fullRequest != null ) {
79+ if (!HttpUtil .isKeepAlive (fullRequest )) {
80+ ctx .write (response ).addListener (ChannelFutureListener .CLOSE );
81+ } else {
82+ response .headers ().set ("CONNECTION" , KEEP_ALIVE );
83+ ctx .write (response );
84+ }
85+ }
86+ }
87+ }
88+
89+ @ Override
90+ public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause ) {
91+ cause .printStackTrace ();
92+ ctx .close ();
93+ }
7594
7695}
0 commit comments