Return to site

🍃🏹 SPRING BOOT gRPC IN 5 STEPS: A BEGINNER-FRIENDLY PATH

· spring

Spring Boot 4.1 introduces native gRPC support, reducing much of the manual infrastructure configuration previously required.

But gRPC is not “REST with another annotation.” You still need to understand Protocol Buffers, generated code and RPC communication.

See also https://github.com/SimonVerhoeven/spring-boot-grpc-demo from Simon Verhoeven (Java renowned consultant)

🔸 TLDR

Define a Protobuf contract, generate the Java API, implement the service, configure the client and make your first remote call.

Here is a simplified stock-ticker example. 📈

Section image

🔸 STEP 1 — ADD THE STARTER

Dependencies

<dependency>
  <artifactId>spring-boot-starter-grpc-server</artifactId>
</dependency>

The starter configures the gRPC server and uses port 9090 by default.

🔸 STEP 2 — DEFINE THE CONTRACT

ticker.proto

service Ticker {
  rpc GetQuote(QuoteRequest) returns (Quote);
  rpc StreamQuotes(StreamRequest) returns (stream Quote);
}

Protobuf defines one unary call and one server-streaming call.

🔸 STEP 3 — GENERATE THE JAVA API

Compile the project

./mvnw compile

Maven turns files from src/main/proto into messages, base services and client stubs.

🔸 STEP 4 — IMPLEMENT THE SERVICE

@Service
class TickerService extends TickerGrpc.TickerImplBase {
  @Override
  public void getQuote(QuoteRequest r, StreamObserver<Quote> o) {
    o.onNext(findQuote(r.getSymbol()));
    o.onCompleted();
  }

  @Override
  public void streamQuotes(StreamQuotesRequest r,
      StreamObserver<Quote> o) {
    quotes(r.getSymbol()).forEach(o::onNext);
    o.onCompleted();
  }
}

TickerGrpc is generated from service Ticker in ticker.proto during compilation. TickerImplBase provides the RPC signatures. @Service registers the implementation, while findQuote() and quotes() represent your business logic.

🔸 STEP 5 — IMPORT THE CLIENT

Register the generated stub

@ImportGrpcClients(
  types = TickerGrpc.TickerBlockingStub.class)
class ClientConfig { }

Spring manages the stub. Inject it and call getQuote() like a regular Spring dependency (like a Spring component).

You can then test the service using IntelliJ’s HTTP client, Postman or grpcurl:

grpcurl -plaintext -d '{"symbol":"AAPL"}' \
localhost:9090 package.Ticker/GetQuote

💡NB: AAPL is the stock-market ticker symbol for Apple Inc.

🔸 TAKEAWAYS

▪️ gRPC uses strongly typed Protobuf contracts.

▪️ Unary RPCs work well for request-response operations.

▪️ Streaming RPCs support live and continuous data flows.

▪️ Spring Boot handles much of the server and client wiring.

▪️ For production, add TLS or mTLS, deadlines, retries, message limits, tracing and health checks.

▪️ gRPC is especially useful for internal service communication; but REST may remain simpler for public or browser-first APIs.

Would you use REST publicly and gRPC between your microservices? 🤔

#SpringBoot #gRPC #Java #Microservices #Protobuf #CloudNative #Spring #SoftwareEngineering

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaFullstackBook👇