1
1
package io .reactivex .lab .gateway ;
2
2
3
+ import com .netflix .eureka2 .client .Eureka ;
4
+ import com .netflix .eureka2 .client .EurekaClient ;
5
+ import com .netflix .eureka2 .client .resolver .ServerResolver ;
6
+ import com .netflix .eureka2 .client .resolver .ServerResolvers ;
7
+ import com .netflix .eureka2 .interests .Interests ;
8
+ import com .netflix .eureka2 .transport .EurekaTransports ;
3
9
import com .netflix .hystrix .HystrixRequestLog ;
4
10
import com .netflix .hystrix .contrib .metrics .eventstream .HystrixMetricsPoller ;
5
11
import com .netflix .hystrix .strategy .concurrency .HystrixRequestContext ;
6
12
import io .netty .buffer .ByteBuf ;
7
13
import io .netty .handler .codec .http .HttpResponseStatus ;
14
+ import io .netty .handler .logging .LogLevel ;
15
+ import io .reactivex .lab .gateway .clients .LoadBalancerFactory ;
8
16
import io .reactivex .lab .gateway .routes .RouteForDeviceHome ;
9
17
import io .reactivex .lab .gateway .routes .mock .TestRouteBasic ;
10
18
import io .reactivex .lab .gateway .routes .mock .TestRouteWithHystrix ;
13
21
import io .reactivex .netty .pipeline .PipelineConfigurators ;
14
22
import io .reactivex .netty .protocol .http .server .HttpServerRequest ;
15
23
import io .reactivex .netty .protocol .http .server .HttpServerResponse ;
24
+ import io .reactivex .netty .protocol .http .sse .ServerSentEvent ;
25
+ import netflix .ocelli .Host ;
26
+ import netflix .ocelli .eureka .EurekaMembershipSource ;
27
+ import netflix .ocelli .rxnetty .HttpClientPool ;
16
28
import rx .Observable ;
17
29
import rx .Subscriber ;
18
30
import rx .subscriptions .Subscriptions ;
19
31
20
32
import java .util .concurrent .TimeUnit ;
21
33
34
+ import static io .reactivex .netty .pipeline .PipelineConfigurators .clientSseConfigurator ;
35
+
22
36
public class StartGatewayServer {
23
37
38
+ private static RouteForDeviceHome routeForDeviceHome ;
39
+
24
40
public static void main (String ... args ) {
25
41
// hystrix stream => http://localhost:9999
26
42
startHystrixMetricsStream ();
27
43
28
- RouteForDeviceHome .getInstance ();
44
+ ServerResolver .Server discoveryServer = new ServerResolver .Server ("127.0.0.1" , 7001 );
45
+ ServerResolver .Server registrationServer = new ServerResolver .Server ("127.0.0.1" , 7001 );
46
+ EurekaClient client = Eureka .newClientBuilder (ServerResolvers .from (discoveryServer ),
47
+ ServerResolvers .from (registrationServer ))
48
+ .withCodec (EurekaTransports .Codec .Json )
49
+ .build ();
50
+ EurekaMembershipSource membershipSource = new EurekaMembershipSource (client );
51
+ LoadBalancerFactory loadBalancerFactory = new LoadBalancerFactory (membershipSource ,
52
+ new HttpClientPool <>((Host host ) -> RxNetty .<ByteBuf , ServerSentEvent >newHttpClientBuilder (host .getHostName (), host .getPort ())
53
+ .pipelineConfigurator (clientSseConfigurator ())
54
+ .enableWireLogging (LogLevel .ERROR )
55
+ .build ()));
56
+
57
+ /**
58
+ * This is making sure that eureka's client registry is warmed up.
59
+ */
60
+ client .forInterest (Interests .forFullRegistry ()).take (1 ).toBlocking ().single ();
61
+
62
+ routeForDeviceHome = new RouteForDeviceHome (loadBalancerFactory );
29
63
30
64
System .out .println ("Server => Starting at http://localhost:8080/" );
31
65
System .out .println (" Sample URLs: " );
@@ -41,7 +75,7 @@ public static void main(String... args) {
41
75
return Observable .defer (() -> {
42
76
HystrixRequestContext .initializeContext ();
43
77
try {
44
- return handleRoutes (request , response ). doOnCompleted ( response :: close ) ;
78
+ return handleRoutes (request , response );
45
79
} catch (Throwable e ) {
46
80
System .err .println ("Server => Error [" + request .getPath () + "] => " + e );
47
81
response .setStatus (HttpResponseStatus .BAD_REQUEST );
@@ -58,6 +92,7 @@ public static void main(String... args) {
58
92
} else {
59
93
System .err .println ("HystrixRequestContext not initialized for thread: " + Thread .currentThread ());
60
94
}
95
+ response .close ();
61
96
});
62
97
}).startAndWait ();
63
98
}
@@ -67,7 +102,7 @@ public static void main(String... args) {
67
102
*/
68
103
private static Observable <Void > handleRoutes (HttpServerRequest <ByteBuf > request , HttpServerResponse <ByteBuf > response ) {
69
104
if (request .getPath ().equals ("/device/home" )) {
70
- return RouteForDeviceHome . getInstance () .handle (request , response );
105
+ return routeForDeviceHome .handle (request , response );
71
106
} else if (request .getPath ().equals ("/testBasic" )) {
72
107
return TestRouteBasic .handle (request , response );
73
108
} else if (request .getPath ().equals ("/testWithSimpleFaultTolerance" )) {
0 commit comments