This repository has been archived by the owner on Nov 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Frontend.java
72 lines (60 loc) · 2.47 KB
/
Frontend.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
package finagle;
import com.twitter.app.Flags;
import com.twitter.finagle.Http;
import com.twitter.finagle.ListeningServer;
import com.twitter.finagle.Service;
import com.twitter.finagle.http.Request;
import com.twitter.finagle.http.Response;
import com.twitter.finagle.tracing.traceId128Bit$;
import com.twitter.finagle.zipkin.core.SamplingTracer;
import com.twitter.util.Await;
import com.twitter.util.Future;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import zipkin2.finagle.http.HttpZipkinTracer;
public class Frontend extends Service<Request, Response> {
final Service<Request, Response> backendClient;
Frontend(Service<Request, Response> backendClient) {
this.backendClient = backendClient;
}
@Override
public Future<Response> apply(Request request) {
if (request.uri().equals("/")) {
return backendClient.apply(Request.apply("/api"));
}
Response response = Response.apply();
response.statusCode(404);
return Future.value(response);
}
public static void main(String[] args) throws Exception{
if (args == null || args.length == 0) {
args = new String[] {
// The frontend makes a sampling decision (via Trace.letTracerAndId) and propagates it downstream.
// This property says sample 100% of traces.
"-zipkin.initialSampleRate", "1.0",
// All servers need to point to the same zipkin transport (note this is default)
"-zipkin.http.host", "localhost:9411",
// Originate 128-bit trace IDs
"-com.twitter.finagle.tracing.traceId128Bit", "true"
};
}
// parse any commandline arguments
new Flags("frontend", true, true).parseOrExit1(args, false);
System.out.println(traceId128Bit$.MODULE$.get());
// It is unreliable to rely on implicit tracer config (Ex sometimes NullTracer is used).
// Always set the tracer explicitly. The default constructor reads from system properties.
SamplingTracer tracer = new HttpZipkinTracer();
Service<Request, Response> backendClient = Http.client()
.withTracer(tracer)
.withLabel("frontend") // this assigns the local service name
.newService("localhost:9000");
ListeningServer server = Http.server()
.withTracer(tracer)
.withLabel("frontend") // this assigns the local service name
.serve(
new InetSocketAddress(InetAddress.getLoopbackAddress(), 8081),
new Frontend(backendClient)
);
Await.ready(server);
}
}