Spring boot GRPC + Maven + Java Starter.
GRPC Basic
gRPC is a modern open-source high-performance RPC framework that can run in any environment. By default, it uses Protocol Buffers to define exposed services.
Why GRPC?
gRPC can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in the last mile of distributed computing to connect devices, mobile applications, and browsers to backend services.
gRPC was initially developed at Google and is now licensed under Apache 2.0.
To show how gRPC works let’s build a client and corresponding server that exposes a simple gRPC service.
So now Let’s Defining a Service Using Protocol Buffers
gRPC services are defined using protocol buffers. These are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data.
For this example, we define a message containing information about a person's name and receive a hello person name message. Both are then used in a sayHello() RPC method that takes the name from the client and returns a greeting from the server.
We also define the version of the protocol buffers language that is used (proto3) in addition to the package name and an option that enables the generation of separate files for different classes.
For more information check the protocol buffers language guide.
The below protocol buffer file is stored in src/main/proto/hello.proto
Spring Boot Setup
To facilitate the management of the different Spring dependencies, Spring Boot Starters are used. These are a set of convenient dependency descriptors that you can include in your application.
We include the spring-boot-starter-web
dependency which automatically sets up an embedded Apache Tomcat that will host our gRPC service endpoint.
The Spring boot starter for gRPC framework auto-configures and runs an embedded gRPC server with @GRpcService
enabled Beans as part of a Spring Boot application. The starter supports both Spring Boot version 1.5.X and 2.X.X. We enable it by including the grpc-spring-boot-starter
dependency.
Protocol buffers support generated code in a number of programming languages. This tutorial focuses on Java.
There are multiple ways to generate the protobuf-based code and in this example, we will use the protobuf-maven-plugin as documented on the grpc-java GitHub page.
We also include the os-maven-plugin extension that generates various useful platform-dependent project properties. This information is needed as the Protocol Buffer compiler is native code. In other words, the protobuf-maven-plugin
needs to fetch the correct compiler for the platform it is running on.
Finally, the plugins section includes the spring-boot-maven-plugin
. This allows us to build a single, runnable uber-jar. This is a convenient way to execute and transport our code. Also, the plugin allows us to start the example via a Maven command.
So now our Pom.xml file looks like
Creating the Server
The service implementation is defined in the GreeterIMPL POJO that implements the GreeterImplBase class that was generated from the hello.proto file.
We override the sayHello()
method and generate a HelloReponse
response based on the message of the HelloRequest passed in the request.
The GreeterIMPL POJO is annotated with @GRpcService
which auto-configures the specified gRPC service to be exposed on port 6565.
So now you are done,
Testing
Use BloomRPC ( GUI Client for gRPC services) to test gRPC API:
That’s great, isn’t it?
If you would like to run the above code sample you can get the full source code here.
In this tutorial, you learned how to implement a gRPC spring-boot maven Java service.
If you liked this example or have a question you would like to ask:
Leave a comment below.
Thanks!