π§ 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.
- 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?