Spring boot starter for gRPC.
Basic codes are forked from grpc-spring-boot-starter
- Scan for gRPC service implement annotated with @GRpcService
- global and customer gRPC interceptors support
- gRPC service daemon runner
- TLS Configuration
- etcd & consul service register plugin
- HAProxy sample
- git clone the project
- run
mvn clean package install
to install jar to local - then check the example project under
spring-boot-starter-grpc-example
folder
- Define gRPC service with protobuf 3 format
service Greeter {
rpc SayHello ( HelloRequest) returns ( HelloReply) {}
}
- Generate gRPC-java code from the service defination files(*.proto)
mvn generate-sources
- implement the gRPC service and annatation the service with
@GRpcService
@GRpcService
public static class GreeterService extends GreeterGrpc.GreeterImplBase{
@Override
public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
final GreeterOuterClass.HelloReply.Builder replyBuilder = GreeterOuterClass.HelloReply.newBuilder().setMessage("Hello " + request.getName());
responseObserver.onNext(replyBuilder.build());
responseObserver.onCompleted();
}
}
- spring application context now will auto scan and run the gRPC service
The starter supports the registration of two kinds of interceptors: Global and Per Service. +
In both cases the interceptor has to implement io.grpc.ServerInterceptor
interface.
- Per service
@GRpcService(interceptors = { LogInterceptor.class })
public class GreeterService extends GreeterGrpc.GreeterImplBase{
// ommited
}
LogInterceptor
will be instantiated via spring factory if there is bean of type LogInterceptor
, or via no-args constructor otherwise.
- Global
@GRpcGlobalInterceptor
public class MyInterceptor implements ServerInterceptor{
// ommited
}
The annotation on java config factory method is also supported :
@Configuration
public class MyConfig{
@Bean
@GRpcGlobalInterceptor
public ServerInterceptor globalInterceptor(){
return new ServerInterceptor(){
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
// your logic here
return next.startCall(call, headers);
}
};
}
}
The particular service also has the opportunity to disable the global interceptors :
@GRpcService(applyGlobalInterceptors = false)
public class GreeterService extends GreeterGrpc.GreeterImplBase{
// ommited
}