A proof-of-concept implementation of a microservices-based shopping application using gRPC (gRPC Remote Procedure Calls). This project demonstrates inter-service communication, Protocol Buffers, and service-oriented architecture patterns.
This project implements a shopping service system with two main microservices:
- User Service: Manages user information and retrieves user details
- Order Service: Manages order information and retrieves orders for users
The User Service demonstrates service-to-service communication by internally calling the Order Service to fetch order counts for users.
βββββββββββββββ βββββββββββββββ
β Client ββββββββββΆβ User Serviceβ
β β β (Port 20021)β
βββββββββββββββ ββββββββ¬ββββββββ
β
β gRPC Call
βΌ
βββββββββββββββ
βOrder Serviceβ
β (Port 20022)β
βββββββββββββββ
- Language: Java 11
- Build Tool: Maven
- gRPC: 1.40.1
- Protocol Buffers: 3.17.3
- Database: H2 (in-memory database)
- Framework: gRPC Java
grpc-poc/
βββ shopping-service/
β βββ src/
β β βββ main/
β β β βββ java/
β β β β βββ com/shopping/
β β β β βββ client/ # gRPC client implementations
β β β β β βββ OrderClient.java
β β β β βββ db/ # Database models and DAOs
β β β β β βββ H2DatabaseConnection.java
β β β β β βββ Order.java
β β β β β βββ OrderDao.java
β β β β β βββ User.java
β β β β β βββ UserDao.java
β β β β βββ server/ # gRPC server implementations
β β β β β βββ OrderServer.java
β β β β β βββ UserServer.java
β β β β βββ service/ # Service implementations
β β β β βββ OrderServiceImpl.java
β β β β βββ UserServiceImpl.java
β β β βββ proto/ # Protocol Buffer definitions
β β β β βββ order.proto
β β β β βββ user.proto
β β β β βββ gateway.proto
β β β βββ resources/
β β β βββ initialize.sql # Database initialization script
β β βββ test/
β βββ pom.xml
βββ README.md
- Service:
UserService - RPC Method:
getUserDetails(UserRequest) returns (UserResponse) - Retrieves user information including ID, username, name, age, gender, and number of orders
- Service:
OrderService - RPC Method:
getOrdersForUser(OrderRequest) returns (OrderResponse) - Retrieves all orders for a given user ID
- Service:
GatewayService - Provides HTTP/REST gateway mapping for gRPC services (for future HTTP gateway implementation)
- β gRPC service implementation with Protocol Buffers
- β Inter-service communication (User Service β Order Service)
- β H2 in-memory database with pre-populated test data
- β Service-to-service gRPC client calls
- β Clean shutdown handling
- β Logging and error handling
- Java 11 or higher
- Maven 3.6 or higher
- IDE (IntelliJ IDEA, Eclipse, or VS Code)
git clone <repository-url>
cd grpc-poccd shopping-service
mvn clean compileThis will:
- Download all dependencies
- Compile Protocol Buffer definitions
- Generate Java stubs from
.protofiles - Compile Java source code
The H2 database is automatically initialized using initialize.sql when the application starts. The script creates:
usertable with sample user dataorderstable with sample order data
cd shopping-service
mvn exec:java -Dexec.mainClass="com.shopping.server.OrderServer"The Order Service will start on port 20022.
In a separate terminal:
cd shopping-service
mvn exec:java -Dexec.mainClass="com.shopping.server.UserServer"The User Service will start on port 20021.
Note: Start the Order Service first, as the User Service depends on it.
- Download BloomRPC from https://appimage.github.io/BloomRPC
- Import the
.protofiles fromshopping-service/src/main/proto/ - Connect to
localhost:20021for User Service orlocalhost:20022for Order Service - Test the RPC methods
Use the provided OrderClient.java as a reference to create your own client implementations.
UserRequest {
username: "Manish927"
}UserResponse {
id: 1
username: "Manish927"
name: "Manish Srivastava1"
age: 28
gender: MALE
num_of_orders: 2
}OrderRequest {
user_id: 5
}OrderResponse {
order: [
{
user_id: 5
order_d: 1
num_of_items: 3
total_amount: 635.0
order_date: "2021-09-09T00:00:00Z"
},
{
user_id: 5
order_d: 3
num_of_items: 5
total_amount: 700.0
order_date: "2021-09-09T00:00:00Z"
}
]
}- User Service:
20021 - Order Service:
20022
id(INT, Primary Key, Auto Increment)username(VARCHAR)password(VARCHAR)name(VARCHAR)gender(VARCHAR)age(INT)
order_id(INT, Primary Key, Auto Increment)user_id(INT, Foreign Key β user.id)no_of_items(INT)total_amount(DOUBLE)order_date(DATE)
The Protocol Buffer code is automatically generated during the Maven build process using the protobuf-maven-plugin. The generated code is located in:
target/generated-sources/protobuf/java/com/shopping/stubs/
The UserServiceImpl demonstrates how to make gRPC calls from one service to another:
- Creates a
ManagedChannelto the Order Service - Creates a blocking stub
- Makes the RPC call
- Properly shuts down the channel
To create a standalone executable JAR, you would need to:
- Add the Maven Shade Plugin to
pom.xml - Configure the main class
- Include resources (like
initialize.sql) in the JAR
- Implement Gateway Service with HTTP/REST endpoints
- Add authentication and authorization
- Add service discovery (e.g., Consul, Eureka)
- Add load balancing
- Add monitoring and observability (Prometheus, Grafana)
- Add unit and integration tests
- Dockerize the services
- Add Kubernetes deployment configurations
If you encounter "port already in use" errors:
- Check if another instance is running:
netstat -ano | findstr :20021(Windows) orlsof -i :20021(Linux/Mac) - Kill the process or change the port in the server classes
- Ensure
protocis available (handled by Maven plugin) - Check that
.protofiles are insrc/main/proto/ - Run
mvn cleanand rebuild
- H2 is in-memory, so data is lost on restart
- Ensure
initialize.sqlis insrc/main/resources/
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This is a proof-of-concept project for educational purposes.