Skip to content

Commit 62a3eae

Browse files
author
romanlu
committed
整合okhttp3实现网关
1 parent dcec340 commit 62a3eae

3 files changed

Lines changed: 84 additions & 3 deletions

File tree

02nio/nio02/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
<artifactId>httpasyncclient</artifactId>
5353
<version>4.1.4</version>
5454
</dependency>
55+
56+
<dependency>
57+
<groupId>com.squareup.okhttp3</groupId>
58+
<artifactId>okhttp</artifactId>
59+
<version>4.9.0</version>
60+
</dependency>
5561

5662
<!--
5763
<dependency>

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

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

3-
import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
3+
import io.github.kimmking.gateway.outbound.okhttp.OkhttpOutboundHandler;
44
import io.netty.channel.ChannelHandlerContext;
55
import io.netty.channel.ChannelInboundHandlerAdapter;
66
import io.netty.handler.codec.http.FullHttpRequest;
@@ -12,11 +12,11 @@ public class HttpInboundHandler extends ChannelInboundHandlerAdapter {
1212

1313
private static Logger logger = LoggerFactory.getLogger(HttpInboundHandler.class);
1414
private final String proxyServer;
15-
private HttpOutboundHandler handler;
15+
private OkhttpOutboundHandler handler;
1616

1717
public HttpInboundHandler(String proxyServer) {
1818
this.proxyServer = proxyServer;
19-
handler = new HttpOutboundHandler(this.proxyServer);
19+
handler = new OkhttpOutboundHandler(this.proxyServer);
2020
}
2121

2222
@Override
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,79 @@
11
package io.github.kimmking.gateway.outbound.okhttp;
22

3+
import io.netty.buffer.Unpooled;
4+
import io.netty.channel.ChannelFutureListener;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.handler.codec.http.DefaultFullHttpResponse;
7+
import io.netty.handler.codec.http.FullHttpRequest;
8+
import io.netty.handler.codec.http.FullHttpResponse;
9+
import io.netty.handler.codec.http.HttpUtil;
10+
import okhttp3.OkHttpClient;
11+
import okhttp3.Request;
12+
import okhttp3.Response;
13+
14+
import java.io.IOException;
15+
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+
/**
21+
* 基于OkHttp实现的网关handler
22+
* @author romanlu
23+
*/
324
public class OkhttpOutboundHandler {
25+
private String backendUrl;
26+
27+
public OkhttpOutboundHandler(String backendUrl) {
28+
this.backendUrl = backendUrl.endsWith("/") ? backendUrl.substring(0, backendUrl.length() - 1) : backendUrl;
29+
}
30+
31+
public void handle(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx) {
32+
final String url = this.backendUrl + fullRequest.uri();
33+
OkHttpClient client = new OkHttpClient();
34+
try {
35+
Request request = new Request.Builder().url(url).build();
36+
Response response = client.newCall(request).execute();
37+
//打印结果
38+
handleResponse(fullRequest, ctx, response);
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
} catch (Exception e) {
42+
e.printStackTrace();
43+
} finally {
44+
client.clone();
45+
}
46+
}
47+
48+
private void handleResponse(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx, final Response endpointResponse) throws Exception {
49+
FullHttpResponse response = null;
50+
try {
51+
52+
byte[] body = endpointResponse.body().bytes();
53+
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(body));
54+
response.headers().set("Content-Type", "application/json");
55+
response.headers().setInt("Content-Length", body.length);
56+
57+
58+
} catch (Exception e) {
59+
e.printStackTrace();
60+
response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
61+
exceptionCaught(ctx, e);
62+
} finally {
63+
if (fullRequest != null) {
64+
if (!HttpUtil.isKeepAlive(fullRequest)) {
65+
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
66+
} else {
67+
ctx.write(response);
68+
}
69+
}
70+
ctx.flush();
71+
}
72+
73+
}
74+
75+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
76+
cause.printStackTrace();
77+
ctx.close();
78+
}
479
}

0 commit comments

Comments
 (0)