60

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

Markus
  • 941
  • 2
  • 8
  • 7
  • 5
    https://spring.io/blog/2015/03/22/using-google-protocol-buffers-with-spring-mvc-based-rest-services is probably a nice read. – M. Deinum Aug 11 '15 at 10:23
  • 1
    True, already found that one. But I would like to know if anyone has tied this together with protobuf service definitions as well? – Markus Aug 12 '15 at 07:30
  • 1
    Looking for an example too – André Abreu Aug 19 '15 at 20:22
  • I recommend using [yidongnan/grpc-spring-boot-starter](https://github.com/yidongnan/grpc-spring-boot-starter). It supports both server and client as well as Spring-Security, Metrics and many more features and examples. **Disclosure**: I'm one of the core maintainers of that library. If you have any question regarding that library feel free to open an issue there. – ST-DDT Feb 18 '19 at 10:09
  • https://github.com/abhinavsinghvirsen/grpc-app simple example maven springboot – abhinavsinghvirsen Jun 26 '23 at 09:34

8 Answers8

40

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.

Alexander.Furer
  • 1,817
  • 1
  • 16
  • 24
  • 3
    If you want to use the "same" library for both client and server i recommend using [yidongnan/grpc-spring-boot-starter](https://github.com/yidongnan/grpc-spring-boot-starter). It also has Spring-Security support. **Disclosure**: I'm one of the core maintainers of that library. – ST-DDT Feb 18 '19 at 10:02
  • Isn't there any support provided by spring boot for gRPC and protobuf as a starter dependency, like the ones for data-jpa, logging, testing etc. ? – Malathi May 27 '19 at 11:46
  • The https://github.com/LogNet/grpc-spring-boot-starter is unofficial one. At this moment spring team does not provide starter for grpc. – Alexander.Furer May 27 '19 at 15:17
  • does attribute autowiring occur with instantiating via @GrpcService ? – Simon Logic Feb 04 '21 at 13:28
  • The service will be instantiated via spring's bean factory, autowiring works as-is. – Alexander.Furer Feb 04 '21 at 13:35
  • yep, just looked into annotation, and saw there @Service )) nice. – Simon Logic Feb 04 '21 at 15:54
5

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());
Michael Chen
  • 51
  • 1
  • 2
5

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.

3

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.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
0

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>
wthamira
  • 2,032
  • 2
  • 21
  • 40
0

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

Senthuran
  • 1,583
  • 2
  • 15
  • 19
0

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

Vali7394
  • 441
  • 5
  • 10
0

you can use this page. dependency and build tags was provided. 'https://www.baeldung.com/grpc-introduction'