Skip to content

Commit 4f4ed28

Browse files
committed
update
1 parent b63161e commit 4f4ed28

File tree

8 files changed

+39
-18
lines changed

8 files changed

+39
-18
lines changed

‎02nio/nio02/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
<artifactId>httpasyncclient</artifactId>
5353
<version>4.1.4</version>
5454
</dependency>
55+
56+
<dependency>
57+
<groupId>org.projectlombok</groupId>
58+
<artifactId>lombok</artifactId>
59+
</dependency>
5560

5661
<!--
5762
<dependency>

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
publicclassNettyServerApplication{
99

1010
publicfinalstaticStringGATEWAY_NAME = "NIOGateway";
11-
publicfinalstaticStringGATEWAY_VERSION = "1.0.0";
11+
publicfinalstaticStringGATEWAY_VERSION = "3.0.0";
1212

1313
publicstaticvoidmain(String[] args){
14-
StringproxyServer = System.getProperty("proxyServer","http://localhost:8088");
14+
1515
StringproxyPort = System.getProperty("proxyPort","8888");
16-
17-
// http://localhost:8888/api/hello ==> gateway API
18-
// http://localhost:8088/api/hello ==> backend service
16+
17+
// String proxyServer = System.getProperty("proxyServer","http://localhost:8088");
18+
// // http://localhost:8888/api/hello ==> gateway API
19+
// // http://localhost:8088/api/hello ==> backend service
20+
// java -Xmx512m gateway-server-0.0.1-SNAPSHOT.jar #作为后端服务
21+
1922

2023
intport = Integer.parseInt(proxyPort);
2124
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" starting...");
2225
HttpInboundServerserver = newHttpInboundServer(port, Arrays.asList("http://localhost:8801","http://localhost:8802"));
23-
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" started at http://localhost:" + port + " for server:" + proxyServer);
26+
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" started at http://localhost:" + port + " for server:" + server.toString());
2427
try{
2528
server.run();
2629
}catch (Exceptionex){
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
packageio.github.kimmking.gateway.filter;
2+
3+
importio.netty.channel.ChannelHandlerContext;
4+
importio.netty.handler.codec.http.FullHttpRequest;
5+
6+
publicclassHeaderHttpRequestFilterimplementsHttpRequestFilter{
7+
8+
@Override
9+
publicvoidfilter(FullHttpRequestfullRequest, ChannelHandlerContextctx){
10+
fullRequest.headers().set("mao", "soul");
11+
}
12+
}

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

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

3+
importio.github.kimmking.gateway.filter.HeaderHttpRequestFilter;
34
importio.github.kimmking.gateway.filter.HttpRequestFilter;
45
importio.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
56
importio.netty.channel.ChannelHandlerContext;
@@ -16,12 +17,11 @@ public class HttpInboundHandler extends ChannelInboundHandlerAdapter{
1617
privatestaticLoggerlogger = LoggerFactory.getLogger(HttpInboundHandler.class);
1718
privatefinalList<String> proxyServer;
1819
privateHttpOutboundHandlerhandler;
19-
privateHttpRequestFilterfilter;
20+
privateHttpRequestFilterfilter = newHeaderHttpRequestFilter();
2021

21-
publicHttpInboundHandler(List<String> proxyServer, HttpRequestFilterfilter){
22+
publicHttpInboundHandler(List<String> proxyServer){
2223
this.proxyServer = proxyServer;
23-
this.filter = filter;
24-
handler = newHttpOutboundHandler(this.proxyServer);
24+
this.handler = newHttpOutboundHandler(this.proxyServer);
2525
}
2626

2727
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public void initChannel(SocketChannel ch){
2828
p.addLast(newHttpServerCodec());
2929
//p.addLast(new HttpServerExpectContinueHandler());
3030
p.addLast(newHttpObjectAggregator(1024 * 1024));
31-
p.addLast(newHttpInboundHandler(this.proxyServer,
32-
(fullRequest, ctx) -> fullRequest.headers().set("mao", "soul")));
31+
p.addLast(newHttpInboundHandler(this.proxyServer));
3332
}
3433
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
importio.netty.channel.socket.nio.NioServerSocketChannel;
1111
importio.netty.handler.logging.LogLevel;
1212
importio.netty.handler.logging.LoggingHandler;
13+
importlombok.Data;
1314
importorg.slf4j.Logger;
1415
importorg.slf4j.LoggerFactory;
1516

1617
importjava.util.List;
1718

18-
19+
@Data
1920
publicclassHttpInboundServer{
2021

2122
privateintport;
@@ -45,7 +46,8 @@ public void run() throws Exception{
4546
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
4647

4748
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
48-
.handler(newLoggingHandler(LogLevel.INFO)).childHandler(newHttpInboundInitializer(this.proxyServers));
49+
.handler(newLoggingHandler(LogLevel.DEBUG))
50+
.childHandler(newHttpInboundInitializer(this.proxyServers));
4951

5052
Channelch = b.bind(port).sync().channel();
5153
System.out.println("开启netty http服务器,监听地址和端口为 http://127.0.0.1:" + port + '/');

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public class HttpOutboundHandler{
4040

4141
publicHttpOutboundHandler(List<String> backends){
4242

43-
this.backendUrls = backendUrls.stream().map(this::formatUrl).collect(Collectors.toList());
43+
this.backendUrls = backends.stream().map(this::formatUrl).collect(Collectors.toList());
4444

45-
intcores = Runtime.getRuntime().availableProcessors() * 2;
45+
intcores = Runtime.getRuntime().availableProcessors();
4646
longkeepAliveTime = 1000;
4747
intqueueSize = 2048;
4848
RejectedExecutionHandlerhandler = newThreadPoolExecutor.CallerRunsPolicy();//.DiscardPolicy();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class RandomHttpEndpointRouter implements HttpEndpointRouter{
77
@Override
88
publicStringroute(List<String> urls){
99
intsize = urls.size();
10-
Randomrandom = newRandom(size);
11-
returnurls.get(random.nextInt());
10+
Randomrandom = newRandom(System.currentTimeMillis());
11+
returnurls.get(random.nextInt(size));
1212
}
1313
}

0 commit comments

Comments
(0)