This project is inspired by the Spring boot starter for gRPC module and backed by the Java gRPC implementation.
- Configures and runs the embedded gRPC server
- Reason: Micronaut does not support HTTP/2 at the moment
- Global and per service interceptors
- Custom ServerBuilder configuration
- Fast startup time
Artifacts are published to Maven Central.
<dependency>
<groupId>com.enegate</groupId>
<artifactId>micronaut-grpc-server</artifactId>
<version>0.0.1</version>
</dependency>
dependencies {
compile 'com.enegate:micronaut-grpc-server:0.0.1'
}
Prerequisite: Generate artifacts from your Protocol Buffer .proto
definition files
Extract from an example .proto
file:
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
Annotate your server interface implementation with @com.enegate.micronaut.grpc.server.annotation.GrpcService
@GrpcService
public class GreeterService extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + request.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
The default port for the grpc server is 8081
.
If you want to change the port add the port
property to your application.yml
file
micronaut:
grpc:
port: 9876
To enable the reflection service add the reflection
property to your application.yml
file
micronaut:
grpc:
reflection: true
To enable the health check service add the healthcheck
property to your application.yml
file
micronaut:
grpc:
healthcheck: true
Annotate your interceptor implementation with @com.enegate.micronaut.grpc.server.annotation.GrpcInterceptor
and implement Interface io.grpc.ServerInterceptor
@GrpcInterceptor
public class GreeterServiceInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
return next.startCall(call, headers);
}
}
Add the interceptor to the array parameter interceptors
of the @GrpcService
annotation of your server interface implementation
@GrpcService(interceptors = {GreeterServiceInterceptor.class})
public class GreeterService extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + request.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
Annotate your interceptor implementation with @com.enegate.micronaut.grpc.server.annotation.GrpcInterceptor
and set the global
parameter to true
.
Implement Interface io.grpc.ServerInterceptor
@GrpcInterceptor(global = true)
public class GlobalInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
return next.startCall(call, headers);
}
}
To configure TLS etc.
Annotate your interceptor implementation with @com.enegate.micronaut.grpc.server.annotation.GrpcInterceptor
and implement Interface com.enegate.micronaut.grpc.server.GrpcServerBuilderInterceptor
@GrpcInterceptor
public class ExampleServerBuilderInterceptor implements GrpcServerBuilderInterceptor {
@Override
public void intercept(ServerBuilder serverBuilder) {
// Configure TLS
serverBuilder.useTransportSecurity(...);
}
}
- micronaut-grpc-example (Java)
- micronaut-grpc-example-kotlin (Kotlin)