Skip to content

Manish927/grpc-poc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 

Repository files navigation

gRPC Shopping Service POC

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.

Overview

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.

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client    │────────▢│ User Serviceβ”‚
β”‚             β”‚         β”‚  (Port 20021)β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                               β”‚ gRPC Call
                               β–Ό
                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                        β”‚Order Serviceβ”‚
                        β”‚  (Port 20022)β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Technology Stack

  • Language: Java 11
  • Build Tool: Maven
  • gRPC: 1.40.1
  • Protocol Buffers: 3.17.3
  • Database: H2 (in-memory database)
  • Framework: gRPC Java

Project Structure

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

Protocol Buffer Definitions

User Service (user.proto)

  • Service: UserService
  • RPC Method: getUserDetails(UserRequest) returns (UserResponse)
  • Retrieves user information including ID, username, name, age, gender, and number of orders

Order Service (order.proto)

  • Service: OrderService
  • RPC Method: getOrdersForUser(OrderRequest) returns (OrderResponse)
  • Retrieves all orders for a given user ID

Gateway Service (gateway.proto)

  • Service: GatewayService
  • Provides HTTP/REST gateway mapping for gRPC services (for future HTTP gateway implementation)

Features

  • βœ… 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

Prerequisites

  • Java 11 or higher
  • Maven 3.6 or higher
  • IDE (IntelliJ IDEA, Eclipse, or VS Code)

Getting Started

1. Clone the Repository

git clone <repository-url>
cd grpc-poc

2. Build the Project

cd shopping-service
mvn clean compile

This will:

  • Download all dependencies
  • Compile Protocol Buffer definitions
  • Generate Java stubs from .proto files
  • Compile Java source code

3. Initialize the Database

The H2 database is automatically initialized using initialize.sql when the application starts. The script creates:

  • user table with sample user data
  • orders table with sample order data

4. Run the Services

Start Order Service

cd shopping-service
mvn exec:java -Dexec.mainClass="com.shopping.server.OrderServer"

The Order Service will start on port 20022.

Start User Service

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.

5. Test the Services

Option 1: BloomRPC (GUI Tool)

  1. Download BloomRPC from https://appimage.github.io/BloomRPC
  2. Import the .proto files from shopping-service/src/main/proto/
  3. Connect to localhost:20021 for User Service or localhost:20022 for Order Service
  4. Test the RPC methods

Option 2: gRPC Client Code

Use the provided OrderClient.java as a reference to create your own client implementations.

Example Usage

User Service Request

UserRequest {
  username: "Manish927"
}

User Service Response

UserResponse {
  id: 1
  username: "Manish927"
  name: "Manish Srivastava1"
  age: 28
  gender: MALE
  num_of_orders: 2
}

Order Service Request

OrderRequest {
  user_id: 5
}

Order Service Response

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"
    }
  ]
}

Service Ports

  • User Service: 20021
  • Order Service: 20022

Database Schema

User Table

  • id (INT, Primary Key, Auto Increment)
  • username (VARCHAR)
  • password (VARCHAR)
  • name (VARCHAR)
  • gender (VARCHAR)
  • age (INT)

Orders Table

  • order_id (INT, Primary Key, Auto Increment)
  • user_id (INT, Foreign Key β†’ user.id)
  • no_of_items (INT)
  • total_amount (DOUBLE)
  • order_date (DATE)

Development Notes

Generating Protocol Buffer Code

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/

Service-to-Service Communication

The UserServiceImpl demonstrates how to make gRPC calls from one service to another:

  1. Creates a ManagedChannel to the Order Service
  2. Creates a blocking stub
  3. Makes the RPC call
  4. Properly shuts down the channel

Building a Fat JAR

To create a standalone executable JAR, you would need to:

  1. Add the Maven Shade Plugin to pom.xml
  2. Configure the main class
  3. Include resources (like initialize.sql) in the JAR

Future Enhancements

  • 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

Troubleshooting

Port Already in Use

If you encounter "port already in use" errors:

  • Check if another instance is running: netstat -ano | findstr :20021 (Windows) or lsof -i :20021 (Linux/Mac)
  • Kill the process or change the port in the server classes

Protocol Buffer Compilation Errors

  • Ensure protoc is available (handled by Maven plugin)
  • Check that .proto files are in src/main/proto/
  • Run mvn clean and rebuild

Database Connection Issues

  • H2 is in-memory, so data is lost on restart
  • Ensure initialize.sql is in src/main/resources/

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

License

This is a proof-of-concept project for educational purposes.

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages