Thanks to visit codestin.com
Credit goes to github.com

Skip to content

A complete proof-of-concept implementation of an ecommerce platform using microservices architecture with gRPC communication and REST API gateway

Notifications You must be signed in to change notification settings

Agbobli5373/grpc-microservices-ecommerce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Ecommerce Microservices POC

A complete proof-of-concept implementation of an ecommerce platform using microservices architecture with gRPC communication and REST API gateway.

πŸ—οΈ Architecture Overview

This project demonstrates a modern microservices architecture with three independent services:

Services

  • product-service (gRPC, Port 50051)

    • Manages product catalog with SQLite database
    • Provides CRUD operations for products
    • Async gRPC server with SQLModel ORM
  • order-service (gRPC, Port 50052)

    • Manages customer orders with SQLite database
    • Validates products by calling product-service
    • Calculates total price automatically
    • Async gRPC server with cross-service communication
  • api-gateway (REST, Port 8000)

    • FastAPI REST API gateway
    • Translates REST requests to gRPC calls
    • Provides external API access to the microservices

Architecture Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   API Gateway   β”‚    β”‚ Product Service β”‚    β”‚  Order Service  β”‚
β”‚    (FastAPI)    │◄──►│     (gRPC)      │◄──►│     (gRPC)      β”‚
β”‚                 β”‚    β”‚                 β”‚    β”‚                 β”‚
β”‚ β€’ REST API      β”‚    β”‚ β€’ Product CRUD  β”‚    β”‚ β€’ Order CRUD    β”‚
β”‚ β€’ Request       β”‚    β”‚ β€’ SQLite DB     β”‚    β”‚ β€’ SQLite DB     β”‚
β”‚   Translation   β”‚    β”‚ β€’ Validation    β”‚    β”‚ β€’ Price Calc    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  Docker Compose     β”‚
                    β”‚  β€’ Service Network  β”‚
                    β”‚  β€’ Volume Mounts    β”‚
                    β”‚  β€’ Health Checks    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Docker & Docker Compose
  • Git

Run the Application

  1. Clone and navigate to the repository

    git clone https://github.com/Agbobli5373/grpc-microservices-ecommerce.git
    cd grpc-microservices-ecommerce
  2. Build and start all services

    docker-compose up --build
  3. Verify services are running

    docker-compose ps

The application will be available at:

  • API Gateway: http://localhost:8000
  • Product Service: localhost:50051 (gRPC)
  • Order Service: localhost:50052 (gRPC)

πŸ“‘ API Endpoints

Products API

Method Endpoint Description
GET /products List all products
POST /products Create a new product
GET /products/{id} Get product by ID

Create Product Example:

curl -X POST http://localhost:8000/products \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Wireless Headphones",
    "description": "High-quality wireless headphones with noise cancellation",
    "price": 199.99
  }'

Response:

{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "name": "Wireless Headphones",
  "description": "High-quality wireless headphones with noise cancellation",
  "price": 199.99
}

Orders API

Method Endpoint Description
GET /orders List all orders
POST /orders Create a new order
GET /orders/{id} Get order by ID

Create Order Example:

curl -X POST http://localhost:8000/orders \
  -H "Content-Type: application/json" \
  -d '{
    "product_id": "550e8400-e29b-41d4-a716-446655440000",
    "quantity": 2
  }'

Response:

{
  "id": "550e8400-e29b-41d4-a716-446655440001",
  "product_id": "550e8400-e29b-41d4-a716-446655440000",
  "quantity": 2,
  "total_price": 399.98
}

πŸ› οΈ Development Setup

Local Development

  1. Install dependencies for each service:

    # Product Service
    cd product-service
    pip install -r requirements.txt
    
    # Order Service
    cd ../order-service
    pip install -r requirements.txt
    
    # API Gateway
    cd ../api-gateway
    pip install -r requirements.txt
  2. Generate gRPC stubs:

    # Product Service
    cd product-service
    python -m grpc_tools.protoc -I./protos --python_out=./proto_gen --grpc_python_out=./proto_gen ./protos/product.proto
    
    # Order Service (needs both protos)
    cd ../order-service
    python -m grpc_tools.protoc -I./protos --python_out=./proto_gen --grpc_python_out=./proto_gen ./protos/product.proto ./protos/order.proto
    
    # API Gateway (needs both protos)
    cd ../api-gateway
    python -m grpc_tools.protoc -I./protos --python_out=./proto_gen --grpc_python_out=./proto_gen ./protos/product.proto ./protos/order.proto
  3. Run services individually:

    # Terminal 1: Product Service
    cd product-service
    python -m app.server
    
    # Terminal 2: Order Service
    cd order-service
    python -m app.server
    
    # Terminal 3: API Gateway
    cd api-gateway
    uvicorn app.main:app --host 0.0.0.0 --port 8000

πŸ“ Project Structure

ecommerce-microservices-poc/
β”œβ”€β”€ product-service/              # Product management service
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ models.py            # SQLModel product definitions
β”‚   β”‚   β”œβ”€β”€ database.py          # Database initialization
β”‚   β”‚   β”œβ”€β”€ servicer.py          # gRPC service implementation
β”‚   β”‚   └── server.py            # gRPC server startup
β”‚   β”œβ”€β”€ protos/
β”‚   β”‚   └── product.proto        # Product service protobuf definition
β”‚   β”œβ”€β”€ proto_gen/               # Generated gRPC stubs
β”‚   β”œβ”€β”€ data/                    # SQLite database files
β”‚   β”œβ”€β”€ Dockerfile               # Service containerization
β”‚   └── requirements.txt         # Python dependencies
β”œβ”€β”€ order-service/               # Order management service
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ models.py            # SQLModel order definitions
β”‚   β”‚   β”œβ”€β”€ database.py          # Database initialization
β”‚   β”‚   β”œβ”€β”€ servicer.py          # gRPC service with product validation
β”‚   β”‚   β”œβ”€β”€ client.py            # Product service client
β”‚   β”‚   └── server.py            # gRPC server startup
β”‚   β”œβ”€β”€ protos/
β”‚   β”‚   β”œβ”€β”€ product.proto        # Product service protobuf
β”‚   β”‚   └── order.proto          # Order service protobuf definition
β”‚   β”œβ”€β”€ proto_gen/               # Generated gRPC stubs
β”‚   β”œβ”€β”€ data/                    # SQLite database files
β”‚   β”œβ”€β”€ Dockerfile               # Service containerization
β”‚   └── requirements.txt         # Python dependencies
β”œβ”€β”€ api-gateway/                 # REST API gateway
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ models.py            # Pydantic REST models
β”‚   β”‚   β”œβ”€β”€ clients.py           # gRPC clients for both services
β”‚   β”‚   └── main.py              # FastAPI application
β”‚   β”œβ”€β”€ protos/
β”‚   β”‚   β”œβ”€β”€ product.proto        # Product service protobuf
β”‚   β”‚   └── order.proto          # Order service protobuf
β”‚   β”œβ”€β”€ proto_gen/               # Generated gRPC stubs
β”‚   β”œβ”€β”€ Dockerfile               # Service containerization
β”‚   └── requirements.txt         # Python dependencies
β”œβ”€β”€ docker-compose.yml           # Multi-service orchestration
β”œβ”€β”€ .gitignore                   # Git ignore rules
β”œβ”€β”€ README.md                    # This file
└── .github/
    └── copilot-instructions.md  # AI coding assistant guidelines

πŸ§ͺ Testing

End-to-End Testing

Test the complete flow:

  1. Create a product
  2. List products
  3. Create an order (validates product exists)
  4. List orders
  5. Test error handling (invalid product ID)

Example Test Script

#!/bin/bash

# Create product
PRODUCT_RESPONSE=$(curl -s -X POST http://localhost:8000/products \
  -H "Content-Type: application/json" \
  -d '{"name":"Test Product","description":"Test Description","price":29.99}')

PRODUCT_ID=$(echo $PRODUCT_RESPONSE | jq -r '.id')
echo "Created product with ID: $PRODUCT_ID"

# Create order
curl -X POST http://localhost:8000/orders \
  -H "Content-Type: application/json" \
  -d "{\"product_id\":\"$PRODUCT_ID\",\"quantity\":3}"

# List orders
curl http://localhost:8000/orders

πŸ›‘οΈ Error Handling

The system includes comprehensive error handling:

  • Product Validation: Orders validate product existence before creation
  • gRPC Error Propagation: Errors are properly mapped from gRPC to REST
  • Database Constraints: SQLModel enforces data integrity
  • Network Resilience: Services handle gRPC connection failures

πŸ”§ Technologies Used

Core Technologies

  • Python 3.11: Primary programming language
  • gRPC: Inter-service communication
  • FastAPI: REST API framework
  • SQLModel: SQL database ORM
  • SQLite: Database (POC - replace with PostgreSQL for production)

Infrastructure

  • Docker: Containerization
  • Docker Compose: Multi-service orchestration
  • Protocol Buffers: API definitions

Development Tools

  • pytest: Testing framework
  • pytest-asyncio: Async testing support
  • Git: Version control

πŸš€ Production Considerations

Database

  • Replace SQLite with PostgreSQL
  • Add database migrations (Alembic)
  • Implement connection pooling
  • Add database monitoring

Security

  • Add authentication/authorization
  • Implement TLS for gRPC communication
  • Add API rate limiting
  • Secure sensitive configuration

Observability

  • Add structured logging
  • Implement health checks
  • Add metrics collection
  • Set up monitoring dashboards

Scalability

  • Add service discovery
  • Implement load balancing
  • Add caching layer (Redis)
  • Consider Kubernetes deployment

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Built as a proof-of-concept for microservices architecture
  • Demonstrates modern Python async patterns
  • Showcases gRPC and REST API integration
  • Provides foundation for production ecommerce systems

Note: This is a proof-of-concept implementation. For production use, consider the production considerations mentioned above.

About

A complete proof-of-concept implementation of an ecommerce platform using microservices architecture with gRPC communication and REST API gateway

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published