Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring Boot together with gRPC and Protobuf

Anyone having any examples or thoughts using gRPC together with Spring Boot?

like image 669
Markus Avatar asked Aug 11 '15 09:08

Markus


People also ask

Does Springboot support gRPC?

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.

Does gRPC require Protobuf?

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.

Does gRPC use Netty?

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.

What is gRPC and Protobuf?

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)


2 Answers

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.

like image 184
Alexander.Furer Avatar answered Sep 19 '22 16:09

Alexander.Furer


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()); 
like image 30
Michael Chen Avatar answered Sep 20 '22 16:09

Michael Chen