Welcome to the Stock Trading gRPC project! This repository contains a simple, high-performance client-server application demonstrating how to build and communicate using gRPC (gRPC Remote Procedure Calls) and Protocol Buffers (Protobuf) within the Spring Boot ecosystem.
This project consists of two main Spring Boot modules:
- StockTradingServer: A gRPC server that handles requests for stock prices. It uses Spring Data JPA to fetch stock details from a MySQL database and returns them to the client.
- Stock-trading-client: A gRPC client that connects to the server. It uses a blocking stub to send requests for specific stock symbols and receives the corresponding stock prices.
- gRPC Communication: Utilizes gRPC for fast and efficient binary communication between the client and server.
- Protocol Buffers: Defines the service contract (
StockTradingService) and message payloads (StockRequest,StockResponse) in an language-agnostic.protofile. - Spring Boot Integration: Leverages
spring-grpc-spring-boot-starteron the server andgrpc-client-spring-boot-starteron the client for seamless autoconfiguration. - Database Integration: The server uses Spring Data JPA and MySQL to persist and retrieve mocked stock data (
Stockentity).
- Java: Primary programming language (Java 17).
- Spring Boot: Framework for both client and server applications (Version 3.5.3).
- gRPC & Protobuf: For defining services and messaging (
grpc-netty-shaded,protobuf-java). - Database: MySQL Server (
mysql-connector-j) & Spring Data JPA. - Build Tool: Maven (with
os-maven-pluginandprotobuf-maven-pluginfor Protobuf compilation). - Lombok: To reduce boilerplate code in Data models and Entities.
StockTradingServer/src/main/proto/stock_trading.proto: Contains the service definition (getStockPrice) and message structures.StockTradingServer/src/main/java/edu/pict/StockTradingServer/service/StockTradingServiceImplementation.java: Implements the gRPC service base class generated by Protobuf and handles the business logic (fetching from DB).Stock-trading-client/src/main/java/edu/pict/Stock_trading_client/service/StockClientService.java: Sets up the gRPC client stub using@GrpcClientand makes blocking calls to the server.
- Java 17 and Maven installed.
- MySQL server running locally.
Ensure you have a MySQL database created (check the application.properties or .yml in StockTradingServer for the exact database name, username, and password).
Before running the applications, you must compile the Protobuf files to generate the necessary Java classes and gRPC stubs.
Navigate to both module directories (StockTradingServer and Stock-trading-client) and run:
mvn clean install(This triggers the protobuf-maven-plugin to generate the sources).
Navigate to the StockTradingServer directory and run the Spring Boot application:
mvn spring-boot:runThe gRPC server will start and listen for incoming connections on the configured port (default is usually 9090 for gRPC).
Navigate to the Stock-trading-client directory and run the Spring Boot application:
mvn spring-boot:runThe client will start up, establish a channel to the server, and you can test the gRPC communication (e.g., via a REST controller or CommandLineRunner setup in the client).
Built to explore the power and efficiency of gRPC in modern Java microservices!