-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathServer.java
More file actions
59 lines (47 loc) · 1.67 KB
/
Server.java
File metadata and controls
59 lines (47 loc) · 1.67 KB
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
package io.vertx.example.grpc.health;
import io.grpc.examples.helloworld.GreeterGrpcService;
import io.grpc.examples.helloworld.HelloReply;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.grpc.health.HealthService;
import io.vertx.grpc.server.GrpcServer;
import io.vertx.launcher.application.VertxApplication;
public class Server extends VerticleBase {
public static void main(String[] args) {
VertxApplication.main(new String[] { Server.class.getName() });
System.out.println("Server started");
}
private final int port;
public Server(int port) {
this.port = port;
}
public Server() {
this(8080);
}
@Override
public Future<?> start() {
// Create the server
GrpcServer rpcServer = GrpcServer.server(vertx);
// The rpc service
rpcServer.callHandler(GreeterGrpcService.SayHello, request -> {
request
.last()
.onSuccess(msg -> {
System.out.println("Hello " + msg.getName());
request.response().end(HelloReply.newBuilder().setMessage(msg.getName()).build());
});
});
// Create the health service
HealthService healthService = HealthService.create(vertx);
// Bind the health service
rpcServer.addService(healthService);
// By default, the health service always returns SERVING status for all services if they were registered through addService.
// but you can register a specific health check for a specific service.
healthService.register(GreeterGrpcService.SERVICE_NAME, () -> Future.succeededFuture(false));
// start the server
return vertx
.createHttpServer()
.requestHandler(rpcServer)
.listen(port);
}
}