gRPC for Spring-Boot

Abhinav Tripathi
3 min readAug 17, 2020

After spending countless time on searching for a perfect solution to setup gRPC with Spring-Boot application, i finally came up with my own way. This was achieved by taking hints from multiple sources on stackOverFlow and then writing the integration which covered all my requirements.

Before you read any further, i want to mention that knowledge of spring-boot, protobuf and gRPC is a prerequisite for this article.

Following are the questions/requirements:
1. Where should i define my proto files?
2. Where should my protobuf generated classes be present?
3. When i build my project (command: ./gradlew clean build), it should. generate the java classes at the defined location
4. I should be able to create a jar of these generated files and publish it to public or private maven repository (to be continued in next part)

Let’s get started

Define all the proto files at src/main/proto directory
Protobuf generated classes will be generated at src/main/proto_gen directory

Let’s define the dependency

compile “io.github.lognet:grpc-spring-boot-starter:3.4.3”

This library contains all the dependencies of gRPC required for a java application. This runs an embedded gRPC server with all@GRpcService annotated services as part of a Spring Boot application. So, you need not register any grpc service inside grpc server during bootRun.

Add the following protobuf plugin to build.gradle file

id "com.google.protobuf" version "0.8.8"

Create a new grpc.gradle file at project level and add the following code snippet

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.7.1"
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.19.0"
}
}
generateProtoTasks {
ofSourceSet('main').each { task ->
task.builtins {
java {
outputSubDir = 'proto_gen'
}
}
task.plugins {
grpc {
outputSubDir = 'proto_gen'
}
}
}
}
generatedFilesBaseDir = "$projectDir/src/"
}
task cleanProtoGen {
doFirst {
delete("$projectDir/src/main/proto_gen")
}
}

clean.dependsOn cleanProtoGen

This new gradle file has been created to keep code clean and to not mix everything in build.gradle

Add the following line after plugins in build.gradle to import grpc.gradle

apply from: ‘grpc.gradle’

This is it. All work is done to integrate gRPC with Spring-Boot.

Now, let’s write a gRPC service and run gRPC server to check if everything works fine.

Create pingPong.proto file in src/main/proto directory and add the following code

Now, let’s create PingPongGrpcController and implement ping rpc

Finally, define port for grpc server in application.properties

grpc.port=8008

Voila, we are done with everything. We have integrated gRPC with Spring-Boot application and implemented a gRPC service which receives a PING request and returns PONG response

To verify we can either use the stub generated by protobuf or we can use BloomRPC (this is the client application for gRPC services)
We’ll be using BloomRPC for now.
Following is the screen-shot of rpc call

We have sent request PING and received the response PONG

You can find the source code here.

Feel free to ask any doubts or questions in the comments below.

Cheers!

--

--