Anyone having any examples or thoughts using gRPC together with Spring Boot?
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());
In here I use gRpc and eureka to communication. This project based on Spring-boot
https://github.com/WThamira/grpc-spring-boot
additionally you canuse register as consul also. full example in this repo
https://github.com/WThamira/gRpc-spring-boot-example
this maven dependency help to gRpc
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.0.1</version>
</dependency>
and need plugin show in below
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<!-- The version of protoc must match protobuf-java. If you don't depend
on protobuf-java directly, you will be transitively depending on the protobuf-java
version that grpc depends on. -->
<protocArtifact>com.google.protobuf:protoc:3.0.2:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
If you need a gRPC client library, i.e. consume stubs, check out my library https://github.com/sfcodes/grpc-client-spring-boot
This library will automatically scan your classpath, find all gRPC stub classes, instantiate them, and register them as beans with the ApplicationContext; allowing you to easily @Autowire
and inject them just like you would any other Spring bean. For example:
@RestController
public class GreeterController {
@Autowired // <===== gRPC stub is autowired!
private GreeterGrpc.GreeterBlockingStub greeterStub;
@RequestMapping(value = "/sayhello")
public String sayHello(@RequestParam String name) {
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply reply = greeterStub.sayHello(request);
return reply.getMessage();
}
}
For gRPC server library, I'd also recommend LogNet/grpc-spring-boot-starter
.
In this Github Repo[1] you will find an example of using gRPC to insert and view the users into the couchbase db. Please refer the proto file[2] to find the rpc methods.
Normally gRPC clients like bloomRPC is used to access the service. Using envoy proxy it is possible to transcode and access the service using HTTP/1.1. In the readme file the steps of creating a config file and to run the envoy proxy using docker file is shown.
[1] https://github.com/Senthuran100/grpc-User
[2] https://github.com/Senthuran100/grpc-User/blob/master/src/main/proto/user.proto
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.
Starting from https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services, then
take a look at
SPR-13589 ProtobufHttpMessageConverter support for protobuf 3.0.0-beta4 and related SPR-13203
HttpMessageConverter based on Protostuff library
That is some support for proto3 is coming in Spring 5. As it is under development one is encouraged to vote and raise what is important for their project.