helidon-io / helidon

Java libraries for writing microservices
https://helidon.io
Apache License 2.0
3.44k stars 562 forks source link

gRPC MP Implementation #8878

Open spericas opened 2 weeks ago

spericas commented 2 weeks ago

Description

Integration of H3 gRPC MP Implementation into H4. gRPC in H3 MP started its own Netty-based server on a new port, the new implementation in H4 is fully intergrated with the web server, which is now capable of detecting the type of a connection and route its data to the correct handler. As a result of this new integration, there is new gRPC server configuration (there is no gRPC server anymore) and a few other integration points have changed.

Module Changes

gRPC MP Support

Just like in H3, there is now support for gRPC annotated endpoints:

    @Grpc
    @ApplicationScoped
    public static class EchoService {

        @Unary(name = "Echo")
        public void echo(Echo.EchoRequest request, StreamObserver<Echo.EchoResponse> observer) {
            try {
                String message = request.getMessage();
                Echo.EchoResponse response = Echo.EchoResponse.newBuilder().setMessage(message).build();
                complete(observer, response);
            } catch (IllegalStateException e) {
                observer.onError(e);
            }
        }
    }

In addition to annotated endpoints, the gRPC CDI extension will scan and automatically register any beans that implement GrpcService (from SE) and BindableService (io.grpc), just like it did in H3.

Server interceptors from the io.grpc API are support only on annotated types either by direct reference using the @GrpcInterceptors or via CDI name binding.

    @Grpc
    @ApplicationScoped
    @EchoInterceptorBinding       // name binding
    @GrpcInterceptors(EchoInterceptor1.class)   // direct reference
    public static class EchoService { ... }

    @GrpcInterceptor
    @ApplicationScoped
    public static class EchoInterceptor1 implements ServerInterceptor {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
                                                                     Metadata headers,
                                                                     ServerCallHandler<ReqT, RespT> next) {
            ...
        }
    }

    @GrpcInterceptor
    @EchoInterceptorBinding
    @ApplicationScoped
    public static class EchoInterceptor2 implements ServerInterceptor {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
                                                                     Metadata headers,
                                                                     ServerCallHandler<ReqT, RespT> next) {
            ...
        }
    }

Additional Features in PR