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
public class NettyServerApplication {
99

1010
public final static String GATEWAY_NAME = "NIOGateway";
11-
public final static String GATEWAY_VERSION = "1.0.0";
11+
public final static String GATEWAY_VERSION = "3.0.0";
1212

1313
public static void main(String[] args) {
14-
String proxyServer = System.getProperty("proxyServer","http://localhost:8088");
14+
1515
String proxyPort = 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
int port = Integer.parseInt(proxyPort);
2124
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" starting...");
2225
HttpInboundServer server = new HttpInboundServer(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 (Exception ex){
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.kimmking.gateway.filter;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.handler.codec.http.FullHttpRequest;
5+
6+
public class HeaderHttpRequestFilter implements HttpRequestFilter {
7+
8+
@Override
9+
public void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
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
package io.github.kimmking.gateway.inbound;
22

3+
import io.github.kimmking.gateway.filter.HeaderHttpRequestFilter;
34
import io.github.kimmking.gateway.filter.HttpRequestFilter;
45
import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
56
import io.netty.channel.ChannelHandlerContext;
@@ -16,12 +17,11 @@ public class HttpInboundHandler extends ChannelInboundHandlerAdapter {
1617
private static Logger logger = LoggerFactory.getLogger(HttpInboundHandler.class);
1718
private final List<String> proxyServer;
1819
private HttpOutboundHandler handler;
19-
private HttpRequestFilter filter;
20+
private HttpRequestFilter filter = new HeaderHttpRequestFilter();
2021

21-
public HttpInboundHandler(List<String> proxyServer, HttpRequestFilter filter) {
22+
public HttpInboundHandler(List<String> proxyServer) {
2223
this.proxyServer = proxyServer;
23-
this.filter = filter;
24-
handler = new HttpOutboundHandler(this.proxyServer);
24+
this.handler = new HttpOutboundHandler(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(new HttpServerCodec());
2929
//p.addLast(new HttpServerExpectContinueHandler());
3030
p.addLast(new HttpObjectAggregator(1024 * 1024));
31-
p.addLast(new HttpInboundHandler(this.proxyServer,
32-
(fullRequest, ctx) -> fullRequest.headers().set("mao", "soul")));
31+
p.addLast(new HttpInboundHandler(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
import io.netty.channel.socket.nio.NioServerSocketChannel;
1111
import io.netty.handler.logging.LogLevel;
1212
import io.netty.handler.logging.LoggingHandler;
13+
import lombok.Data;
1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
1516

1617
import java.util.List;
1718

18-
19+
@Data
1920
public class HttpInboundServer {
2021

2122
private int port;
@@ -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(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpInboundInitializer(this.proxyServers));
49+
.handler(new LoggingHandler(LogLevel.DEBUG))
50+
.childHandler(new HttpInboundInitializer(this.proxyServers));
4951

5052
Channel ch = 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
public HttpOutboundHandler(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-
int cores = Runtime.getRuntime().availableProcessors() * 2;
45+
int cores = Runtime.getRuntime().availableProcessors();
4646
long keepAliveTime = 1000;
4747
int queueSize = 2048;
4848
RejectedExecutionHandler handler = new ThreadPoolExecutor.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
public String route(List<String> urls) {
99
int size = urls.size();
10-
Random random = new Random(size);
11-
return urls.get(random.nextInt());
10+
Random random = new Random(System.currentTimeMillis());
11+
return urls.get(random.nextInt(size));
1212
}
1313
}

0 commit comments

Comments
 (0)