Using Spring Boot together with gRPC and Protobuf
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Spring Boot works well with gRPC when you treat HTTP and RPC as separate concerns: Spring Boot manages application wiring, configuration, and lifecycle, while gRPC and Protobuf define the contract and transport for service-to-service calls. The basic integration pattern is to generate Java classes from a .proto file, implement the service in a Spring-managed bean, and run a gRPC server alongside the rest of the Boot application.
Define the Contract in Protobuf First
With gRPC, the API contract starts in a .proto file rather than a controller interface.
This file does two jobs:
- defines the request and response schema
- defines the gRPC service method signatures
The generated Java code becomes the common contract between server and client.
Generate Java Stubs from the .proto File
A common Maven setup uses the protobuf and gRPC code generation plugins.
The exact plugin block can vary by project, but the goal is always the same: compile .proto definitions into Java message classes and service stubs during the build.
Implement the gRPC Service as a Spring Bean
Once the stubs are generated, implement the server side. The generated base class gives you strongly typed request and response objects.
This example uses a Spring Boot gRPC starter annotation to register the service. That keeps the implementation aligned with the rest of the Boot app instead of wiring the gRPC server manually.
Configure Spring Boot and gRPC Side by Side
Spring Boot still manages configuration through application.yml or application.properties. A simple setup might look like this:
This lets the Boot application own environment-specific settings while gRPC listens on its own port.
If the same app also exposes REST endpoints, it is common to run Spring MVC on one port and gRPC on another so the protocols stay clearly separated.
Call the Service from a Client
The generated blocking or async stubs make client calls straightforward.
That is one of the main benefits of Protobuf contracts: the client and server stay type-safe without handwritten JSON parsing.
Keep Responsibilities Clean
Spring Boot and gRPC complement each other best when their responsibilities stay clear:
- Spring Boot handles dependency injection, configuration, metrics, and application lifecycle
- Protobuf defines schemas and contracts
- gRPC handles RPC transport and generated client or server APIs
Trying to force gRPC into a controller-like REST mental model usually makes the design harder, not easier.
Common Pitfalls
The biggest mistake is treating .proto files as a side detail instead of the source of truth for the service contract. Another common issue is forgetting that gRPC uses its own transport and port, so a Spring MVC setup does not automatically expose those services. Developers also sometimes mix generated code into source control awkwardly instead of letting the build create it reliably. Finally, if REST and gRPC live in the same app, keep the interfaces clearly separated so clients do not have to guess which protocol a given endpoint uses.
Summary
- Use Protobuf to define the service contract first.
- Generate Java stubs during the build rather than writing request or response classes by hand.
- Implement the generated gRPC base class in a Spring-managed service.
- Let Spring Boot handle application wiring and configuration while gRPC handles RPC transport.
- Keep REST and gRPC concerns separate even if they live inside the same Boot application.

