gRPC Set-up with Spring Boot v3.0.1 and Kotlin
gRPC is a high-performance, open-source framework for building microservices. It serializes and transmits data using Protocol Buffers, making it fast and efficient. In this article, we’ll set up a Spring Boot Application in Kotlin using gRPC as our communication framework.
Project Set-up
Protobuf Set-up
Step 1: In your project, create the following directory structure
/main
/proto
/named_greeter
/requests
/fetch_greeting_request.proto
/responses
/fetch_greeting_response.proto
/named_greeting_service.proto
Step 2: Now, add the proto files as follows:
Step 3: Add the following plugin to your build.gradle.kts
file to generate the Kotlin and Java code from your .proto
file:
Note: This build.gradle.kts has other dependencies as well, that we’ll require later to create the actual server and client.
Step 4: Run the ./gradlew clean build
command to compile your project and generate the Kotlin code from your .proto
file. The generated code will be placed in the build/generated/source/proto/main/kotlin/
directory.
Server Set-up
Step 1: Make these new directories and files
/main
/kotlin
/com
/tmk
/grpckt
/config
/grpc
/GrpcServerConfig.kt
/controller
/NamedGreetingGrpcService.kt
/GreetingApplication.kt
Step 2: Create the SpringBoot Application.
Step 3: Create the gRPC Server Config to initiate the server in the bound configuration with the spring boot server.
Step 4: Now, we need to create the actual controller that would receive and handle the gRPC request.
Client Set-up
Step 1: Create the following directory structure
/main
/kotlin
/com
/tmk
/grpckt
/client
/NamedGreetingClient.kt
Step 2: Create the client to hit the server with a request.
Final Touches
Now, there are two main parts to run this program:
Step 1: Create the config for the gRPC server port and the config to take the URL for the client.
/main
/resources
/application.yml
Step 2: Create a trigger for the client to hit the server, when the application is up.
/main
/kotlin
/com
/tmk
/grpckt
/FlowTrigger.kt
Now, just run the application and after a few logs you’ll see the following log (which indicates that your project is working)
Project Link: https://github.com/naman-gupta99/grpc-kotlin-spring-boot-sample
Final Thoughts
There are a few things to keep in mind:
- Right now, the client is basically hitting the server hosted on the same application as the one running the client. In an actual, scenario the URL for the client should point to a different application.
- On the same lines, the
FlowTrigger.kt
the file is only for this project as we need an event for the client to hit its request without external intervention. Though, this would not be required in an actual application. - One thing to keep in mind while working with protos is that it is not as flexible as a JSON contract, in the way that if you remove a particular field from the proto on the server, it will surely break the client as the client will not be able to parse the protobuf input. So, it is usually a good idea to first, maintain a shared library between the server and the clients, that has all the proto files, and second, update the protos in an add -> deploy -> deprecate -> deploy fashion.
Links and Resources Used to Learn
The resources listed below were instrumental in my journey to learn gRPC.
Kotlin and gRPC Server Set-up
Using gRPC With Kotlin for Building Microservices — This medium article provides a comprehensive guide on how to set up a gRPC server using Kotlin.
gRPC Documentation
Quick start — The gRPC quick start guide provides a comprehensive overview of how to get started with gRPC in Kotlin.
gRPC Kotlin Github Repo
https://github.com/grpc/grpc-kotlin — The gRPC Kotlin Github repository provides information and resources for anyone interested in learning gRPC in Kotlin.
gRPC Kotlin Spring Boot Example
grpc-spring-boot-starter/GlobalInterceptorConfiguration.java at master · yidongnan/grpc-spring-boot-starter — This Github repository provides a great example of how to set up a gRPC server using Kotlin and Spring Boot.
Issues
While following the tutorials, I faced an issue with the gRPC server not starting up with the Spring Boot Server. Here’s how I resolved the issue:
- Make sure to include the
spring-boot-starter-web
dependency to run a Spring Boot Server. - Add the
grpc-bom
to manage the versions of all the gRPC dependencies and to make sure that you aren’t using any conflicting dependencies - Note that Spring Boot v3.0.0 and above are not yet supported by gRPC, requiring special configurations to start up.
@ImportAutoConfiguration(
GrpcClientAutoConfiguration::class,
GrpcClientMetricAutoConfiguration::class,
GrpcClientHealthAutoConfiguration::class,
GrpcClientSecurityAutoConfiguration::class,
GrpcClientTraceAutoConfiguration::class,
GrpcDiscoveryClientAutoConfiguration::class,
GrpcCommonCodecAutoConfiguration::class,
GrpcCommonTraceAutoConfiguration::class,
GrpcAdviceAutoConfiguration::class,
GrpcHealthServiceAutoConfiguration::class,
GrpcMetadataConsulConfiguration::class,
GrpcMetadataEurekaConfiguration::class,
GrpcMetadataNacosConfiguration::class,
GrpcMetadataZookeeperConfiguration::class,
GrpcReflectionServiceAutoConfiguration::class,
GrpcServerAutoConfiguration::class,
GrpcServerFactoryAutoConfiguration::class,
GrpcServerMetricAutoConfiguration::class,
GrpcServerSecurityAutoConfiguration::class,
GrpcServerTraceAutoConfiguration::class
}