Thursday, July 3, 2025

Building a Server-Side Streaming gRPC Service in Spring Boot: A Stock Ticker Demo

🧠What is gRPC and Why Use It?

This project demonstrates how to implementΒ server-side streamingΒ usingΒ gRPCΒ in aΒ Spring BootΒ application withΒ Java 11. The use case simulates a stock ticker that streams real-time stock prices to the client.


πŸ”§ Technologies Used

  • Java 11
  • Spring Boot 2.7.x
  • gRPC (1.54.0)
  • Protocol Buffers (proto3)
  • Maven
  • WebFlux + Server-Sent Events (SSE) for REST integration

βœ…Code Repository LinkΒ 

Link -Β grpc-stock-streaming-rest

Generate gRPC Java Code -Β Β mvn clean compile

Run the Application -Β Β mvn spring-boot:run

REST to gRPC Streaming via SSE GET -Β http://localhost:8080/api/stream?symbol=AAPL

βœ… Project Structure

  • grpc-stock-streaming/ β”œβ”€β”€ pom.xml β”œβ”€β”€ src/ β”‚ └── main/ β”‚ β”œβ”€β”€ java/ β”‚ β”‚ └── com/ β”‚ β”‚ └── example/ β”‚ β”‚ └── grpc/ β”‚ β”‚ β”œβ”€β”€ GrpcApplication.java # Bootstraps Spring Boot β”‚ β”‚ β”œβ”€β”€ StockService.java # gRPC service implementation β”‚ β”‚ β”œβ”€β”€ StockClient.java # gRPC client stub (non-blocking) β”‚ β”‚ └── StockController.java # REST API exposing SSE endpoint β”‚ β”œβ”€β”€ proto/ β”‚ β”‚ └── stock.proto # gRPC service + messages β”‚ └── resources/ β”‚ └── application.properties # gRPC & Spring server config

βœ… Class Explanations














βœ…stock.proto Explained in Detail

  • syntax = "proto3"; option java_multiple_files = true; option java_package = "com.example.grpc"; option java_outer_classname = "StockProto"; service StockTicker { rpc StreamStockPrices (StockRequest) returns (stream StockResponse); } message StockRequest { string symbol = 1; } message StockResponse { string symbol = 1; double price = 2; }

βœ… REST β†’ gRPC Call Flow Diagram


  • Browser / Postman / curl β”‚ β–Ό GET /api/stream?symbol=AAPL (SSE endpoint) β”‚ β–Ό StockController β”‚ β–Ό StockClient (gRPC Stub) β”‚ β–Ό [Non-blocking StreamObserver] β”‚ β–Ό gRPC Network Call (localhost:9090) β”‚ β–Ό StockService (on Server) β”‚ [5x response] β”‚ β–Ό StockClient β†’ REST Stream (Flux/SSE) β†’ Browser

🧠 What is gRPC and Why Use It?

❌ gRPC Server-Side Streaming Anti-Patterns

βš™οΈ gRPC Streaming Performance vs Other Streaming Protocols

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS