22
33import io .github .kimmking .gateway .outbound .httpclient4 .HttpOutboundHandler ;
44import io .github .kimmking .gateway .outbound .okhttp .HutoolhttpOutboundHandler ;
5+ import io .github .kimmking .gateway .router .HttpEndpointRouter ;
56import io .netty .channel .ChannelHandlerContext ;
67import io .netty .channel .ChannelInboundHandlerAdapter ;
78import io .netty .handler .codec .http .FullHttpRequest ;
89import io .netty .util .ReferenceCountUtil ;
910import org .slf4j .Logger ;
1011import org .slf4j .LoggerFactory ;
1112
13+ import java .util .Arrays ;
14+ import java .util .List ;
15+
1216public 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 ) {
0 commit comments