https://github.com/mrday2day/java-spring-grpc
Java-based gRPC server built with Spring Boot, Maven, and Protocol Buffers. It demonstrates a working gRPC service implementation using best practices for dependency management, server configuration, and reflection support.
https://github.com/mrday2day/java-spring-grpc
Last synced: about 2 months ago
JSON representation
Java-based gRPC server built with Spring Boot, Maven, and Protocol Buffers. It demonstrates a working gRPC service implementation using best practices for dependency management, server configuration, and reflection support.
- Host: GitHub
- URL: https://github.com/mrday2day/java-spring-grpc
- Owner: MrDay2Day
- License: mit
- Created: 2025-04-08T05:11:50.000Z (2 months ago)
- Default Branch: master
- Last Pushed: 2025-04-08T05:17:58.000Z (2 months ago)
- Last Synced: 2025-04-08T06:24:43.703Z (2 months ago)
- Language: Java
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Java gRPC Server (Maven + Spring Boot)
This repository showcases a **Java-based gRPC server** built with **Spring Boot**, **Maven**, and **Protocol Buffers**. It demonstrates a working gRPC service implementation using best practices for dependency management, server configuration, and reflection support.
> ๐ง **Tech Stack:** Java 21, Spring Boot, gRPC, Maven, Protobuf
---
## โจ Project Highlights
- ๐ก **gRPC Server Setup:** Lightweight and high-performance server setup using `grpc-netty`.
- ๐ **Protobuf Integration:** `.proto` definitions compiled via Maven for generating gRPC stubs.
- ๐งฉ **Spring Boot Integration:** Simplifies configuration and dependency injection.
- ๐ **Reflection Enabled:** Server supports gRPC reflection for easy introspection and debugging.
- ๐ **Bidirectional Communication Ready:** Base for implementing unary, server-streaming, client-streaming, and bidirectional gRPC methods.
- ๐ **Multiple Services:** Includes both GreetingService and OrderService implementations.---
## ๐ Project Structure
```bash
Java-Spring-gRPC/
โโโ src/
โโโ main/
โ โ โโโ java/code
โ โ โ โโโ Main.java
โ โ โ โโโ client/
โ โ โ โ โโโ GrpcClient.java
โ โ โ โโโ config/
โ โ โ โ โโโ GrpcServerConfig.java
โ โ โ โโโ services/
โ โ โ โโโ GreetingServiceImpl.java
โ โ โ โโโ OrderServiceImpl.java
โ โ โ โโโ OrderStatusResponse.java
โ โ โโโ proto/
โ โ โโโ greeting.proto
โ โ โโโ order.proto
โ โโโ resources/
โ โโโ application.yml
โโโ pom.xml
โโโ LICENSE
โโโ README.md
```---
## ๐ Getting Started
### 1. Clone the Repository
```bash
git clone https://github.com/MrDay2Day/grpc-server-java.git
cd grpc-server-java
```### 2. Build the Project
Ensure you have Maven installed. You can build the project using the following command:Make sure you have Java 21 and Maven 3.8+ installed.```bash
mvn clean install
```### 3. Run the gRPC Server
```bash
mvn spring-boot:run
```
The server will start on the default port: `3031`## ๐ Protobuf Definitions
### Greeting Service
`greeting.proto` defines the `GreetingService`:```protobuf
syntax = "proto3";option java_multiple_files = true;
option java_package = "com.example.grpc";
option java_outer_classname = "GreetingServiceProto";service GreetingService {
rpc sayHello (HelloRequest) returns (HelloResponse);
}message HelloRequest {
string name = 1;
}message HelloResponse {
string message = 1;
}
```### Order Service
`order.proto` defines the `OrderService`:```protobuf
syntax = "proto3";option java_multiple_files = true;
option java_package = "com.example.grpc";
option java_outer_classname = "OrderProto";package order;
service OrderService {
rpc getOrder (getOrderRequest) returns (getOrderResponse) {}
rpc getOrderStatus (orderStatusRequest) returns (orderStatusResponse) {}
}message getOrderRequest {
string productName = 1;
int32 productId = 2;
string productDescription = 3;
int32 units = 4;
double unitCost = 5;
}message getOrderResponse {
int32 orderId = 1;
double total = 2;
string status = 3;
}message orderStatusRequest {
int32 orderId = 1;
}message orderStatusResponse {
string status = 1;
}
```## ๐ฆ Maven Plugin for Proto Compilation
This project uses the protobuf-maven-plugin to automatically compile .proto files during build:```xml
org.xolstice.maven.plugins
protobuf-maven-plugin
...```
## ๐ง Reflection Support
The server is configured to support gRPC reflection, allowing clients to discover available services and methods dynamically. This is useful for debugging and testing.Reflection is enabled by adding the following to `GrpcServerConfig.java`:
```java
.addService(ProtoReflectionService.newInstance())
```
This allows tools like grpcurl to discover and call methods:```bash
grpcurl -plaintext localhost:3031 list
```## ๐งช Testing the gRPC Server
### Testing GreetingService
```bash
grpcurl -plaintext -d '{"name": "Day2Day"}' localhost:3031 com.example.grpc.GreetingService/sayHello
```Expected Response:
```json
{
"message": "Hello, Day2Day!"
}
```### Testing OrderService
#### Get Order
```bash
grpcurl -plaintext -d '{"productId": 123, "productName": "Widget", "productDescription": "A premium widget", "units": 5, "unitCost": 19.99}' localhost:3031 order.OrderService/getOrder
```Expected Response:
```json
{
"orderId": 100000001,
"total": 229.885,
"status": "PENDING"
}
```#### Get Order Status
```bash
grpcurl -plaintext -d '{"orderId": 100000001}' localhost:3031 order.OrderService/getOrderStatus
```Expected Response:
```json
{
"status": "SHIPPED"
}
```## ๐ฆ Dependency Management
Dependencies are centrally managed in `pom.xml`, with specific versions pulled in via `` for maintainability:```xml
net.devh
grpc-server-spring-boot-starterio.grpc
grpc-services```
## โ Skills Demonstrated
- โ๏ธ Setting up a gRPC server with Spring Boot
- ๐ Writing and compiling Protocol Buffers with Maven
- ๐ง Understanding gRPC service design patterns (Unary RPC)
- ๐งฉ Integrating Protobuf-generated code into business logic
- ๐งช Using testing/debugging tools like grpcurl with reflection
- ๐ฆ Clean dependency management using Maven
- ๐ Implementing multiple gRPC services in a single server## ๐ Future Enhancements
- โ Add support for streaming RPCs
- โ Implement client-side logic
- โ Add unit tests and integration tests
- โ Dockerize the application for containerized deployment
- โ Add observability (metrics/logging)## ๐ License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.