Using Spring Boot together with gRPC and Protobuf

SpringSpring BootProtocol BuffersGrpc

Spring Problem Overview


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

Spring Solutions


Solution 1 - Spring

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.

Solution 2 - Spring

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());

Solution 3 - Spring

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.

Solution 4 - Spring

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.

Solution 5 - Spring

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>

Solution 6 - Spring

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<br> [2] https://github.com/Senthuran100/grpc-User/blob/master/src/main/proto/user.proto

Solution 7 - Spring

Created a simple Springboot App with GRPC. This GitHub repo has both Server and Client examples. Repo has separate Maven module(grpc-interface) where we declare the Proto files and generate the Java source code then can be used as lib in both Server and client apps.

https://github.com/vali7394/grpc-springboot-repo

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkusView Question on Stackoverflow
Solution 1 - SpringAlexander.FurerView Answer on Stackoverflow
Solution 2 - SpringMichael ChenView Answer on Stackoverflow
Solution 3 - SpringSemyon FishmanView Answer on Stackoverflow
Solution 4 - SpringPaul VerestView Answer on Stackoverflow
Solution 5 - SpringwthamiraView Answer on Stackoverflow
Solution 6 - SpringSenthuranView Answer on Stackoverflow
Solution 7 - SpringVali7394View Answer on Stackoverflow