Skip to content

Commit 1e78fd4

Browse files
author
tangrong
committed
路由
1 parent 570794b commit 1e78fd4

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package io.github.kimmking.gateway;
22

33

4+
import cn.hutool.core.collection.CollUtil;
45
import io.github.kimmking.gateway.inbound.HttpInboundServer;
56

7+
import java.util.Arrays;
8+
69
public class NettyServerApplication {
710

811
public final static String GATEWAY_NAME = "NIOGateway";
912
public final static String GATEWAY_VERSION = "1.0.0";
1013

1114
public static void main(String[] args) {
12-
String proxyServer = System.getProperty("proxyServer","http://localhost:8080");
15+
String proxyServer = CollUtil.join(Arrays.asList("http://locallhost:8080","http://locallhost:8081"),",");
1316
String proxyPort = System.getProperty("proxyPort","8888");
1417

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

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
44
import io.github.kimmking.gateway.outbound.okhttp.HutoolhttpOutboundHandler;
5+
import io.github.kimmking.gateway.router.HttpEndpointRouter;
56
import io.netty.channel.ChannelHandlerContext;
67
import io.netty.channel.ChannelInboundHandlerAdapter;
78
import io.netty.handler.codec.http.FullHttpRequest;
89
import io.netty.util.ReferenceCountUtil;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

13+
import java.util.Arrays;
14+
import java.util.List;
15+
1216
public class HttpInboundHandler extends ChannelInboundHandlerAdapter {
1317

1418
private static Logger logger = LoggerFactory.getLogger(HttpInboundHandler.class);
@@ -28,14 +32,23 @@ public void channelReadComplete(ChannelHandlerContext ctx) {
2832
@Override
2933
public void channelRead(ChannelHandlerContext ctx, Object msg) {
3034
try {
31-
//logger.info("channelRead流量接口请求开始,时间为{}", startTime);
35+
3236
FullHttpRequest fullRequest = (FullHttpRequest) msg;
33-
// String uri = fullRequest.uri();
34-
// //logger.info("接收到的请求url为{}", uri);
35-
// if (uri.contains("/test")) {
36-
// handlerTest(fullRequest, ctx);
37-
// }
38-
37+
38+
String url = fullRequest.uri();
39+
HttpEndpointRouter router = new HttpEndpointRouter() {
40+
@Override
41+
public String route(List<String> endpoints, List<String> proxyServers) {
42+
if (endpoints.stream().anyMatch(e->e.startsWith("/user"))) {
43+
return proxyServers.get(0);
44+
} else if (endpoints.stream().anyMatch(e->e.startsWith("/order"))) {
45+
return proxyServers.get(1);
46+
}
47+
return null;
48+
}
49+
};
50+
String proxy = router.route(Arrays.asList(url), Arrays.asList(this.proxyServer.split(",").clone()));
51+
HutoolhttpOutboundHandler handler = new HutoolhttpOutboundHandler(proxy);
3952
handler.handle(fullRequest, ctx);
4053

4154
} catch(Exception e) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import java.util.List;
44

5+
@FunctionalInterface
56
public interface HttpEndpointRouter {
6-
7-
String route(List<String> endpoints);
8-
7+
String route(List<String> endpoints,List<String> proxyServers);
98
}

0 commit comments

Comments
 (0)