Skip to content

Commit 8afb809

Browse files
authored
http工具类
1 parent f3f6632 commit 8afb809

17 files changed

Lines changed: 774 additions & 636 deletions

02nio/nio02/pom.xml

Lines changed: 115 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,115 @@
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"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
6-
<groupId>io.github.kimmking</groupId>
7-
<artifactId>netty-gateway</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
9-
<packaging>jar</packaging>
10-
11-
<name>netty-gateway</name>
12-
<description>Demo project for Spring Boot</description>
13-
14-
<parent>
15-
<groupId>org.springframework.boot</groupId>
16-
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>2.0.4.RELEASE</version>
18-
<relativePath/>
19-
</parent>
20-
21-
<properties>
22-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24-
<java.version>1.8</java.version>
25-
</properties>
26-
27-
<dependencies>
28-
29-
<dependency>
30-
<groupId>io.netty</groupId>
31-
<artifactId>netty-all</artifactId>
32-
<version>4.1.45.Final</version>
33-
</dependency>
34-
35-
<dependency>
36-
<groupId>commons-logging</groupId>
37-
<artifactId>commons-logging</artifactId>
38-
<version>1.2</version>
39-
</dependency>
40-
<dependency>
41-
<groupId>org.slf4j</groupId>
42-
<artifactId>slf4j-api</artifactId>
43-
<version>1.7.25</version>
44-
</dependency>
45-
<dependency>
46-
<groupId>org.slf4j</groupId>
47-
<artifactId>slf4j-log4j12</artifactId>
48-
<version>1.7.25</version>
49-
</dependency>
50-
<dependency>
51-
<groupId>org.apache.httpcomponents</groupId>
52-
<artifactId>httpasyncclient</artifactId>
53-
<version>4.1.4</version>
54-
</dependency>
55-
56-
<dependency>
57-
<groupId>org.projectlombok</groupId>
58-
<artifactId>lombok</artifactId>
59-
</dependency>
60-
61-
<!--
62-
<dependency>
63-
<groupId>org.springframework.boot</groupId>
64-
<artifactId>spring-boot-starter-web</artifactId>
65-
</dependency>
66-
67-
<dependency>
68-
<groupId>org.springframework.boot</groupId>
69-
<artifactId>spring-boot-starter-test</artifactId>
70-
<scope>test</scope>
71-
</dependency>
72-
-->
73-
74-
</dependencies>
75-
76-
<build>
77-
<plugins>
78-
<plugin>
79-
<groupId>org.springframework.boot</groupId>
80-
<artifactId>spring-boot-maven-plugin</artifactId>
81-
</plugin>
82-
</plugins>
83-
</build>
84-
85-
86-
</project>
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"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>io.github.kimmking</groupId>
7+
<artifactId>netty-gateway</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>netty-gateway</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.4.RELEASE</version>
18+
<relativePath/>
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
29+
<dependency>
30+
<groupId>io.netty</groupId>
31+
<artifactId>netty-all</artifactId>
32+
<version>4.1.45.Final</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>commons-logging</groupId>
37+
<artifactId>commons-logging</artifactId>
38+
<version>1.2</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.slf4j</groupId>
42+
<artifactId>slf4j-api</artifactId>
43+
<version>1.7.25</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.slf4j</groupId>
47+
<artifactId>slf4j-log4j12</artifactId>
48+
<version>1.7.25</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.apache.httpcomponents</groupId>
52+
<artifactId>httpasyncclient</artifactId>
53+
<version>4.1.4</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.projectlombok</groupId>
58+
<artifactId>lombok</artifactId>
59+
</dependency>
60+
61+
<!--
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-web</artifactId>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-test</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
-->
73+
<dependency>
74+
<groupId>com.alibaba</groupId>
75+
<artifactId>fastjson</artifactId>
76+
<version>1.2.58</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.apache.httpcomponents</groupId>
80+
<artifactId>httpcore</artifactId>
81+
<version>4.4.9</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.apache.httpcomponents</groupId>
85+
<artifactId>httpclient</artifactId>
86+
<version>4.5.6</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>log4j</groupId>
90+
<artifactId>log4j</artifactId>
91+
<version>1.2.17</version>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.slf4j</groupId>
95+
<artifactId>slf4j-api</artifactId>
96+
<version>1.7.7</version>
97+
</dependency>
98+
<dependency>
99+
<groupId>org.slf4j</groupId>
100+
<artifactId>slf4j-nop</artifactId>
101+
<version>1.7.2</version>
102+
</dependency>
103+
</dependencies>
104+
105+
<build>
106+
<plugins>
107+
<plugin>
108+
<groupId>org.springframework.boot</groupId>
109+
<artifactId>spring-boot-maven-plugin</artifactId>
110+
</plugin>
111+
</plugins>
112+
</build>
113+
114+
115+
</project>
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
package io.github.kimmking.gateway;
2-
3-
4-
import io.github.kimmking.gateway.inbound.HttpInboundServer;
5-
6-
import java.util.Arrays;
7-
8-
public class NettyServerApplication {
9-
10-
public final static String GATEWAY_NAME = "NIOGateway";
11-
public final static String GATEWAY_VERSION = "3.0.0";
12-
13-
public static void main(String[] args) {
14-
15-
String proxyPort = System.getProperty("proxyPort","8888");
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");
26-
int port = Integer.parseInt(proxyPort);
27-
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" starting...");
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());
30-
try {
31-
server.run();
32-
}catch (Exception ex){
33-
ex.printStackTrace();
34-
}
35-
}
36-
}
1+
package io.github.kimmking.gateway;
2+
3+
4+
import io.github.kimmking.gateway.inbound.HttpInboundServer;
5+
6+
import java.util.Arrays;
7+
8+
public class NettyServerApplication {
9+
10+
public final static String GATEWAY_NAME = "NIOGateway";
11+
public final static String GATEWAY_VERSION = "3.0.0";
12+
13+
public static void main(String[] args) {
14+
15+
String proxyPort = System.getProperty("proxyPort","8888");
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");
26+
int port = Integer.parseInt(proxyPort);
27+
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" starting...");
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());
30+
try {
31+
server.run();
32+
}catch (Exception ex){
33+
ex.printStackTrace();
34+
}
35+
}
36+
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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-
}
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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +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-
}
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+
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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 interface HttpRequestFilter {
7-
8-
void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx);
9-
10-
}
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 interface HttpRequestFilter {
7+
8+
void filter(FullHttpRequest fullRequest, ChannelHandlerContext ctx);
9+
10+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package io.github.kimmking.gateway.filter;
2-
3-
import io.netty.handler.codec.http.FullHttpResponse;
4-
5-
public interface HttpResponseFilter {
6-
7-
void filter(FullHttpResponse response);
8-
9-
}
1+
package io.github.kimmking.gateway.filter;
2+
3+
import io.netty.handler.codec.http.FullHttpResponse;
4+
5+
public interface HttpResponseFilter {
6+
7+
void filter(FullHttpResponse response);
8+
9+
}

0 commit comments

Comments
 (0)