gRPC Server Routing
Unlike Webserver, which allows you to route requests based on path expression and the HTTP verb, gRPC server always routes requests based on the service and method name. This makes routing configuration somewhat simpler — all you need to do is register your services:
private static GrpcRouting createRouting(Config config) {
return GrpcRouting.builder()
.register(new GreetService(config))
.register(new EchoService())
.register(new MathService())
.build();
}- Register
GreetServiceinstance. - Register
EchoServiceinstance. - Register
MathServiceinstance.
Both "standard" gRPC services that implement io.grpc.BindableService interface (typically implemented by extending generated server-side stub and overriding its methods), and Helidon gRPC services that implement io.helidon.grpc.server.GrpcService interface can be registered.
The difference is that Helidon gRPC services allow you to customize behavior down to the method level, and provide a number of useful helper methods that make service implementation easier, as we’ll see in a moment.
Customizing Service Definitions
When registering a service, regardless of its type, you can customize its descriptor by providing configuration consumer as a second argument to the register method.
This is particularly useful when registering standard BindableService instances, as it allows you to add certain Helidon-specific behaviors, such as health checks and metrics to them:
private static GrpcRouting createRouting(Config config) {
return GrpcRouting.builder()
.register(new GreetService(config))
.register(new EchoService(), service -> {
service.healthCheck(CustomHealthChecks::echoHealthCheck)
.metered();
})
.build();
}- Add custom health check to the service.
- Specify that all the calls to service methods should be metered.
Specifying Global Interceptors
GrpcRouting also allows you to specify custom interceptors that will be applied to all registered services.
This is useful to configure features such as tracing, security and metrics collection, and we provide built-in interceptors for those purposes that you can simply register with the routing definition:
private static GrpcRouting createRouting(Config config) {
return GrpcRouting.builder()
.intercept(GrpcMetrics.timed())
.register(new GreetService(config))
.register(new EchoService())
.register(new MathService())
.build();
}- Register
GrpcMetricsinterceptor that will collect timers for all methods of all services (but can be overridden at the individual service or even method level).