gRPC server with Spring Boot using grpc-server-spring-boot-starter

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:

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:

Protocol Buffers Maven Plugin I declare as follows:

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:

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?

Add Comment