Anyone having any examples or thoughts using gRPC together with Spring Boot?
gRPC-spring-boot-starter combines google's open-source high performance RPC-framework with spring boot's ease of setup. This project simplifies the gRPC-server/client setup to adding one dependency to your project and adding a single annotation to your service class / client (stub) field.
Protobuf is the most commonly used IDL (Interface Definition Language) for gRPC. It's where you basically store your data and function contracts in the form of a proto file. As this is in the form of a contract, both the client and server need to have the same proto file.
gRPC comes with three Transport implementations: The Netty-based transport is the main transport implementation based on Netty. It is for both the client and the server. The OkHttp-based transport is a lightweight transport based on OkHttp.
gRPC manages the way a client and a server can interact (just like a web client/server with a REST API) protobuf is just a serialization/deserialization tool (just like JSON)
If it's still relevant for you, I've created gRPC spring-boot-starter here.
grpc-spring-boot-starter auto-configures and runs the embedded gRPC server with @GRpcService-enabled beans.
The simplest example :
@GRpcService(grpcServiceOuterClass = GreeterGrpc.class) public static class GreeterService implements GreeterGrpc.Greeter { @Override public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) { // omitted } }
There is also an example of how to integrate the starter with Eureka in project's README file.
https://github.com/yidongnan/grpc-spring-boot-starter
In server
@GrpcService(GreeterGrpc.class) public class GrpcServerService extends GreeterGrpc.GreeterImplBase { @Override public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { HelloReply reply = HelloReply.newBuilder().setMessage("Hello =============> " + req.getName()).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); } }
In client
@GrpcClient("gRPC server name") private Channel serverChannel; GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverChannel); HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With