-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathStartGatewayServer.java
101 lines (90 loc) · 4.86 KB
/
StartGatewayServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package io.reactivex.lab.gateway;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.reactivex.lab.gateway.hystrix.HystrixMetricsStreamHandler;
import io.reactivex.lab.gateway.loadbalancer.DiscoveryAndLoadBalancer;
import io.reactivex.lab.gateway.routes.RouteForDeviceHome;
import io.reactivex.lab.gateway.routes.mock.TestRouteBasic;
import io.reactivex.lab.gateway.routes.mock.TestRouteWithHystrix;
import io.reactivex.lab.gateway.routes.mock.TestRouteWithSimpleFaultTolerance;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
import io.reactivex.netty.protocol.http.server.HttpServerResponse;
import rx.Observable;
import com.netflix.hystrix.HystrixRequestLog;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
public class StartGatewayServer {
static {
// discovery config
System.setProperty("reactivelab.eureka.server.host", "127.0.0.1");
System.setProperty("reactivelab.eureka.server.read.port", "7001");
System.setProperty("reactivelab.eureka.server.write.port", "7002");
}
public static void main(String... args) {
// initialize
DiscoveryAndLoadBalancer.getFactory();
// hystrix stream => http://localhost:9999
startHystrixMetricsStream();
System.out.println("Server => Starting at http://localhost:8080/");
System.out.println(" Sample URLs: ");
System.out.println(" - http://localhost:8080/device/home?userId=123");
System.out.println("----------------------------------------------------------------");
// start web services => http://localhost:8080
RxNetty.createHttpServer(8080, (request, response) -> {
if (request.getPath().contains("favicon.ico")) {
return Observable.empty();
}
// System.out.println("Server => Request: " + request.getPath());
return Observable.defer(() -> {
HystrixRequestContext.initializeContext();
try {
return handleRoutes(request, response);
} catch (Throwable e) {
System.err.println("Server => Error [" + request.getPath() + "] => " + e);
response.setStatus(HttpResponseStatus.BAD_REQUEST);
return response.writeStringAndFlush("Error 500: Bad Request\n" + e.getMessage() + "\n");
}
}).onErrorResumeNext(error -> {
System.err.println("Server => Error: " + error.getMessage());
error.printStackTrace();
return writeError(request, response, "Failed: " + error.getMessage());
}).doOnTerminate(() -> {
if (HystrixRequestContext.isCurrentThreadInitialized()) {
System.out.println("Server => Request [" + request.getPath() + "] => " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
HystrixRequestContext.getContextForCurrentThread().shutdown();
} else {
System.err.println("HystrixRequestContext not initialized for thread: " + Thread.currentThread());
}
response.close();
});
}).startAndWait();
}
/**
* Hard-coded route handling.
*/
private static Observable<Void> handleRoutes(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
if (request.getPath().equals("/device/home")) {
return new RouteForDeviceHome().handle(request, response);
} else if (request.getPath().equals("/testBasic")) {
return TestRouteBasic.handle(request, response);
} else if (request.getPath().equals("/testWithSimpleFaultTolerance")) {
return TestRouteWithSimpleFaultTolerance.handle(request, response);
} else if (request.getPath().equals("/testWithHystrix")) {
return TestRouteWithHystrix.handle(request, response);
} else {
return writeError(request, response, "Unknown path: " + request.getPath());
}
}
private static void startHystrixMetricsStream() {
HystrixMetricsStreamHandler<ByteBuf, ByteBuf> hystrixMetricsStreamHandler = new HystrixMetricsStreamHandler<>("/", 1000);
RxNetty.createHttpServer(9999, (request, response) -> {
System.out.println("Server => Start Hystrix Stream at http://localhost:9999");
return hystrixMetricsStreamHandler.handle(request, response);
}).start();
}
public static Observable<Void> writeError(HttpServerRequest<?> request, HttpServerResponse<ByteBuf> response, String message) {
System.err.println("Server => Error [" + request.getPath() + "] => " + message);
response.setStatus(HttpResponseStatus.BAD_REQUEST);
return response.writeStringAndFlush("Error 500: " + message);
}
}