Skip to content

Commit 570794b

Browse files
author
唐榕
committed
Week03 作业题目(周四)
1 parent 01d9794 commit 570794b

6 files changed

Lines changed: 100 additions & 9 deletions

File tree

02nio/nio02/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,19 @@
5252
<artifactId>httpasyncclient</artifactId>
5353
<version>4.1.4</version>
5454
</dependency>
55-
55+
56+
<dependency>
57+
<groupId>org.apache.httpcomponents</groupId>
58+
<artifactId>httpasyncclient</artifactId>
59+
<version>4.1.4</version>
60+
</dependency>
61+
62+
<!-- 国人开发的Java工具包-->
63+
<dependency>
64+
<groupId>cn.hutool</groupId>
65+
<artifactId>hutool-all</artifactId>
66+
<version>5.3.9</version>
67+
</dependency>
5668
<!--
5769
<dependency>
5870
<groupId>org.springframework.boot</groupId>

02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class NettyServerApplication {
99
public final static String GATEWAY_VERSION = "1.0.0";
1010

1111
public static void main(String[] args) {
12-
String proxyServer = System.getProperty("proxyServer","http://localhost:8088");
12+
String proxyServer = System.getProperty("proxyServer","http://localhost:8080");
1313
String proxyPort = System.getProperty("proxyPort","8888");
1414

1515
// http://localhost:8888/api/hello ==> gateway API

02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.kimmking.gateway.inbound;
22

33
import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
4+
import io.github.kimmking.gateway.outbound.okhttp.HutoolhttpOutboundHandler;
45
import io.netty.channel.ChannelHandlerContext;
56
import io.netty.channel.ChannelInboundHandlerAdapter;
67
import io.netty.handler.codec.http.FullHttpRequest;
@@ -12,11 +13,11 @@ public class HttpInboundHandler extends ChannelInboundHandlerAdapter {
1213

1314
private static Logger logger = LoggerFactory.getLogger(HttpInboundHandler.class);
1415
private final String proxyServer;
15-
private HttpOutboundHandler handler;
16+
private HutoolhttpOutboundHandler handler;
1617

1718
public HttpInboundHandler(String proxyServer) {
1819
this.proxyServer = proxyServer;
19-
handler = new HttpOutboundHandler(this.proxyServer);
20+
handler = new HutoolhttpOutboundHandler(this.proxyServer);
2021
}
2122

2223
@Override

02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.kimmking.gateway.outbound.httpclient4;
22

33

4+
import cn.hutool.core.convert.Convert;
45
import io.netty.buffer.Unpooled;
56
import io.netty.channel.ChannelFutureListener;
67
import io.netty.channel.ChannelHandlerContext;
@@ -103,7 +104,7 @@ private void handleResponse(final FullHttpRequest fullRequest, final ChannelHand
103104

104105
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(body));
105106
response.headers().set("Content-Type", "application/json");
106-
response.headers().setInt("Content-Length", Integer.parseInt(endpointResponse.getFirstHeader("Content-Length").getValue()));
107+
response.headers().setInt("Content-Length", Convert.toInt(body.length));
107108

108109
// for (Header e : endpointResponse.getAllHeaders()) {
109110
// //response.headers().set(e.getName(),e.getValue());
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.github.kimmking.gateway.outbound.okhttp;
2+
3+
import cn.hutool.core.convert.Convert;
4+
import cn.hutool.http.HttpRequest;
5+
import cn.hutool.http.HttpResponse;
6+
import io.github.kimmking.gateway.outbound.httpclient4.NamedThreadFactory;
7+
import io.netty.buffer.Unpooled;
8+
import io.netty.channel.ChannelFutureListener;
9+
import io.netty.channel.ChannelHandlerContext;
10+
import io.netty.handler.codec.http.DefaultFullHttpResponse;
11+
import io.netty.handler.codec.http.FullHttpRequest;
12+
import io.netty.handler.codec.http.FullHttpResponse;
13+
import io.netty.handler.codec.http.HttpUtil;
14+
15+
import java.util.concurrent.ArrayBlockingQueue;
16+
import java.util.concurrent.ExecutorService;
17+
import java.util.concurrent.ThreadPoolExecutor;
18+
import java.util.concurrent.TimeUnit;
19+
20+
import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
21+
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
22+
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
23+
24+
public class HutoolhttpOutboundHandler {
25+
private final String backendUrl;
26+
private final ExecutorService proxyService;
27+
28+
public HutoolhttpOutboundHandler(String backendUrl) {
29+
this.backendUrl = backendUrl.endsWith("/") ? backendUrl.substring(0, backendUrl.length() - 1) : backendUrl;
30+
int cores = Runtime.getRuntime().availableProcessors() * 2;
31+
long keepAliveTime = 1000;
32+
int queueSize = 2048;
33+
34+
proxyService = new ThreadPoolExecutor(cores, cores,
35+
keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize),
36+
new NamedThreadFactory("proxyService"), new ThreadPoolExecutor.CallerRunsPolicy());
37+
38+
}
39+
40+
public void handle(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx) {
41+
final String url = this.backendUrl + fullRequest.uri();
42+
proxyService.submit(() -> fetchGet(fullRequest, ctx, url));
43+
}
44+
45+
private void fetchGet(final FullHttpRequest inbound, final ChannelHandlerContext ctx, final String url) {
46+
HttpResponse response = new HttpRequest(url).keepAlive(true).executeAsync();
47+
handleResponse(inbound, ctx, response);
48+
}
49+
50+
private void handleResponse(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx, final HttpResponse endpointResponse) {
51+
FullHttpResponse response = null;
52+
try {
53+
54+
byte[] body = endpointResponse.bodyBytes();
55+
56+
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(body));
57+
response.headers().set("Content-Type", "application/json");
58+
response.headers().setInt("Content-Length", Convert.toInt(body.length));
59+
60+
} catch (Exception e) {
61+
e.printStackTrace();
62+
response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
63+
exceptionCaught(ctx, e);
64+
} finally {
65+
if (fullRequest != null) {
66+
if (!HttpUtil.isKeepAlive(fullRequest)) {
67+
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
68+
} else {
69+
ctx.write(response);
70+
}
71+
}
72+
ctx.flush();
73+
}
74+
75+
}
76+
77+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
78+
cause.printStackTrace();
79+
ctx.close();
80+
}
81+
}

02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/okhttp/OkhttpOutboundHandler.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)