Skip to content

Commit ee52288

Browse files
committed
add response filter
1 parent 4f4ed28 commit ee52288

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ public static void main(String[] args) {
1414

1515
String proxyPort = System.getProperty("proxyPort","8888");
1616

17+
// 这是之前的单个后端url的例子
1718
// String proxyServer = System.getProperty("proxyServer","http://localhost:8088");
1819
// // http://localhost:8888/api/hello ==> gateway API
1920
// // http://localhost:8088/api/hello ==> backend service
2021
// java -Xmx512m gateway-server-0.0.1-SNAPSHOT.jar #作为后端服务
2122

22-
23+
24+
// 这是多个后端url走随机路由的例子
25+
String proxyServers = System.getProperty("proxyServers","http://localhost:8801,http://localhost:8802");
2326
int port = Integer.parseInt(proxyPort);
2427
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" starting...");
25-
HttpInboundServer server = new HttpInboundServer(port, Arrays.asList("http://localhost:8801","http://localhost:8802"));
28+
HttpInboundServer server = new HttpInboundServer(port, Arrays.asList(proxyServers.split(",")));
2629
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" started at http://localhost:" + port + " for server:" + server.toString());
2730
try {
2831
server.run();
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.github.kimmking.gateway.outbound.httpclient4;
22

33

4+
import io.github.kimmking.gateway.filter.HeaderHttpResponseFilter;
45
import io.github.kimmking.gateway.filter.HttpRequestFilter;
6+
import io.github.kimmking.gateway.filter.HttpResponseFilter;
57
import io.github.kimmking.gateway.router.HttpEndpointRouter;
68
import io.github.kimmking.gateway.router.RandomHttpEndpointRouter;
79
import io.netty.buffer.Unpooled;
@@ -36,6 +38,7 @@ public class HttpOutboundHandler {
3638
private ExecutorService proxyService;
3739
private List<String> backendUrls;
3840

41+
HttpResponseFilter filter = new HeaderHttpResponseFilter();
3942
HttpEndpointRouter router = new RandomHttpEndpointRouter();
4043

4144
public HttpOutboundHandler(List<String> backends) {
@@ -121,9 +124,12 @@ private void handleResponse(final FullHttpRequest fullRequest, final ChannelHand
121124
// System.out.println(body.length);
122125

123126
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(body));
127+
124128
response.headers().set("Content-Type", "application/json");
125129
response.headers().setInt("Content-Length", Integer.parseInt(endpointResponse.getFirstHeader("Content-Length").getValue()));
126-
130+
131+
filter.filter(response);
132+
127133
// for (Header e : endpointResponse.getAllHeaders()) {
128134
// //response.headers().set(e.getName(),e.getValue());
129135
// System.out.println(e.getName() + " => " + e.getValue());

0 commit comments

Comments
 (0)