Skip to content

Commit ac6df84

Browse files
authored
Merge pull request #1 from JavaCourse00/main
同步修改
2 parents dbd14b1 + ee52288 commit ac6df84

16 files changed

Lines changed: 245 additions & 33 deletions
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>java0.nio01</groupId>
5+
<artifactId>nio01</artifactId>
6+
<version>1.0</version>
7+
<build>
8+
<plugins>
9+
<plugin>
10+
<artifactId>maven-compiler-plugin</artifactId>
11+
<configuration>
12+
<source>${maven.compile.source}</source>
13+
<target>${maven.compile.target}</target>
14+
</configuration>
15+
</plugin>
16+
<plugin>
17+
<artifactId>maven-shade-plugin</artifactId>
18+
<version>3.2.0</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>shade</goal>
23+
</goals>
24+
<configuration>
25+
<transformers>
26+
<transformer>
27+
<manifestEntries>
28+
<Main-Class>${app.main.class}</Main-Class>
29+
<X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
30+
<X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
31+
</manifestEntries>
32+
</transformer>
33+
</transformers>
34+
</configuration>
35+
</execution>
36+
</executions>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
<properties>
41+
<app.main.class>Main</app.main.class>
42+
<maven.compile.source>8</maven.compile.source>
43+
<maven.compile.target>8</maven.compile.target>
44+
</properties>
45+
</project>

02nio/nio01/pom.xml

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,52 @@
77
<groupId>java0.nio01</groupId>
88
<artifactId>nio01</artifactId>
99
<version>1.0</version>
10+
11+
<properties>
12+
<app.main.class>Main</app.main.class>
13+
<maven.compile.source>8</maven.compile.source>
14+
<maven.compile.target>8</maven.compile.target>
15+
</properties>
16+
1017
<build>
1118
<plugins>
1219
<plugin>
1320
<groupId>org.apache.maven.plugins</groupId>
1421
<artifactId>maven-compiler-plugin</artifactId>
1522
<configuration>
16-
<source>8</source>
17-
<target>8</target>
23+
<source>${maven.compile.source}</source>
24+
<target>${maven.compile.target}</target>
1825
</configuration>
1926
</plugin>
27+
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-shade-plugin</artifactId>
31+
<version>3.2.0</version>
32+
<executions>
33+
<execution>
34+
<goals>
35+
<goal>shade</goal>
36+
</goals>
37+
<configuration>
38+
<transformers>
39+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
40+
<manifestEntries>
41+
<Main-Class>${app.main.class}</Main-Class>
42+
<X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
43+
<X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
44+
</manifestEntries>
45+
</transformer>
46+
</transformers>
47+
</configuration>
48+
</execution>
49+
</executions>
50+
</plugin>
2051
</plugins>
2152
</build>
2253

54+
55+
2356
<dependencies>
2457
<dependency>
2558
<groupId>io.netty</groupId>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java0.nio01.HttpServer01;
2+
import java0.nio01.HttpServer02;
3+
import java0.nio01.HttpServer03;
4+
import java0.nio01.netty.NettyHttpServer;
5+
6+
import java.lang.reflect.Method;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class Main {
11+
12+
public static void main(String[] args) {
13+
Map<String, Class> map = new HashMap<>();
14+
map.put("1", HttpServer01.class);
15+
map.put("2", HttpServer02.class);
16+
map.put("3", HttpServer03.class);
17+
map.put("8", NettyHttpServer.class);
18+
19+
String id = (null == args || args.length == 0) ? "1" : args[0];
20+
Class clazz = map.get(id);
21+
if( null == clazz ) {
22+
System.out.println("No class for id: " + id);
23+
}
24+
25+
try {
26+
Method method = clazz.getDeclaredMethod("main", new Class[]{String[].class});
27+
method.invoke(null, new Object[]{new String[]{}});
28+
} catch (Exception e) {
29+
e.printStackTrace();
30+
}
31+
32+
}
33+
34+
}

02nio/nio01/src/main/java/java0/nio01/HttpServer01.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private static void service(Socket socket) {
2525
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
2626
printWriter.println("HTTP/1.1 200 OK");
2727
printWriter.println("Content-Type:text/html;charset=utf-8");
28-
String body = "hello,nio";
28+
String body = "hello,nio1";
2929
printWriter.println("Content-Length:" + body.getBytes().length);
3030
printWriter.println();
3131
printWriter.write(body);

02nio/nio01/src/main/java/java0/nio01/HttpServer02.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private static void service(Socket socket) {
2727
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
2828
printWriter.println("HTTP/1.1 200 OK");
2929
printWriter.println("Content-Type:text/html;charset=utf-8");
30-
String body = "hello,nio";
30+
String body = "hello,nio2";
3131
printWriter.println("Content-Length:" + body.getBytes().length);
3232
printWriter.println();
3333
printWriter.write(body);

02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
4444
private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
4545
FullHttpResponse response = null;
4646
try {
47-
String value = "hello,kimmking";
47+
String value = null; // "hello,kimmking"; // 对接上次作业的httpclient或者okhttp请求另一个url的响应数据
48+
49+
// httpGet ... http://localhost:8801
50+
// 返回的响应,"hello,nio";
51+
// value = reponse....
52+
4853
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
4954
response.headers().set("Content-Type", "application/json");
5055
response.headers().setInt("Content-Length", response.content().readableBytes());

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: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,30 @@
33

44
import io.github.kimmking.gateway.inbound.HttpInboundServer;
55

6+
import java.util.Arrays;
7+
68
public class NettyServerApplication {
79

810
public final static String GATEWAY_NAME = "NIOGateway";
9-
public final static String GATEWAY_VERSION = "1.0.0";
11+
public final static String GATEWAY_VERSION = "3.0.0";
1012

1113
public static void main(String[] args) {
12-
String proxyServer = System.getProperty("proxyServer","http://localhost:8088");
14+
1315
String proxyPort = System.getProperty("proxyPort","8888");
14-
15-
// http://localhost:8888/api/hello ==> gateway API
16-
// http://localhost:8088/api/hello ==> backend service
17-
16+
17+
// 这是之前的单个后端url的例子
18+
// String proxyServer = System.getProperty("proxyServer","http://localhost:8088");
19+
// // http://localhost:8888/api/hello ==> gateway API
20+
// // http://localhost:8088/api/hello ==> backend service
21+
// java -Xmx512m gateway-server-0.0.1-SNAPSHOT.jar #作为后端服务
22+
23+
24+
// 这是多个后端url走随机路由的例子
25+
String proxyServers = System.getProperty("proxyServers","http://localhost:8801,http://localhost:8802");
1826
int port = Integer.parseInt(proxyPort);
1927
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" starting...");
20-
HttpInboundServer server = new HttpInboundServer(port, proxyServer);
21-
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" started at http://localhost:" + port + " for server:" + proxyServer);
28+
HttpInboundServer server = new HttpInboundServer(port, Arrays.asList(proxyServers.split(",")));
29+
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" started at http://localhost:" + port + " for server:" + server.toString());
2230
try {
2331
server.run();
2432
}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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.github.kimmking.gateway.filter;
2+
3+
import io.netty.handler.codec.http.FullHttpResponse;
4+
5+
public class HeaderHttpResponseFilter implements HttpResponseFilter {
6+
@Override
7+
public void filter(FullHttpResponse response) {
8+
response.headers().set("kk", "java-1-nio");
9+
}
10+
}

0 commit comments

Comments
 (0)