There is currently no official starter supporting gRPC in Spring Boot, but you can also use the starters developed by the community in the GitHub repository https://github.com/grpc-ecosystem/grpc-spring. In this tutorial, I will guide you on how to build a gRPC server with Spring Boot using grpc-server-spring-boot-starter in this GitHub repository!
First, I will create a new Spring Boot project as an example:
We will declare the grpc-server-spring-boot-starter as follows:
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>net.devh</groupId> <artifactId>grpc-server-spring-boot-starter</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency> |
Here, I will use Protocol Buffers Maven Plugin to generate Java code for the gRPC service contract, so as you can see, I also declared the javax.annotation-api dependency to avoid compilation errors after generating source code.
The .proto file in the src/main/proto folder defines the service contract, I will define it simply as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
syntax = "proto3"; option java_multiple_files = true; package com.huongdanjava.springboot.grpc; message HelloRequest { string name = 1; } message HelloResponse { string message = 1; } service HelloService { rpc hello(HelloRequest) returns (HelloResponse); } |
Protocol Buffers Maven Plugin I declare as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.7.1</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.25.4:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.66.0:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
You can read more in the tutorial Generate Java code for service contract in gRPC using Protocol Buffers Maven Plugin to understand how to use Protocol Buffers Maven Plugin to generate Java code for service contract!
Run Maven Compile, you will see the following result:
To implement HelloService with grpc-server-spring-boot-starter, you just need to add a new class that extends the HelloServiceGrpc.HelloServiceImplBase class and annotate this class with the annotation @GrpcService as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava.springboot.grpc; import io.grpc.stub.StreamObserver; import net.devh.boot.grpc.server.service.GrpcService; @GrpcService public class HelloController extends HelloServiceGrpc.HelloServiceImplBase { @Override public void hello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { String greeting = "Hello " + request.getName() + " from Huong Dan Java"; HelloResponse response = HelloResponse.newBuilder() .setMessage(greeting) .build(); responseObserver.onNext(response); responseObserver.onCompleted(); } } |
The implementation content of the hello() method is similar to the previous tutorial!
grpc-server-spring-boot-starter with Spring Boot’s auto-configuration mechanism will automatically start a gRPC server with default configurations, adding the implementation of HelloService to expose it to the outside.
By default, grpc-server-spring-boot-starter will run the gRPC server at port 9090, you can change this default port by declaring the grpc.server.port property with the value you want! In this example, I will leave the default port.
If I now run this example application and use Postman to request the hello operation, I will see the following result:
Very quick and simple, right?