Skip to content

E-commerce with microservices in .NET 9 + RabbitMQ + MySQL + Docker. Product, inventory, and sales management with JWT authentication and asynchronous communication

License

Notifications You must be signed in to change notification settings

Alekssandher/ECommerce-Microservices

Repository files navigation

E-Commerce with Microservices Architecture

E-commerce system developed with microservices architecture for product inventory management and sales, implementing asynchronous communication and JWT authentication.

πŸ—οΈ Architecture

The system consists of 4 main microservices:

1. API Gateway (Port: 5001)

  • Single entry point for all requests
  • Intelligent routing to microservices
  • Rate limiting and caching
  • Implemented with Ocelot

2. AuthService (Port: 5004)

  • User authentication and authorization
  • JWT token generation and validation
  • User management (Client and Manager)
  • Password encryption with BCrypt

3. SalesService (Port: 5002)

  • Sales order management
  • Stock validation before confirmation
  • Asynchronous communication with stock service
  • Sales status control (Pending, Confirmed, Canceled, Unauthorized)

4. StockService (Port: 5003)

  • Product and inventory management
  • Available and reserved quantity control
  • Stock reservation and release processing
  • Product CRUD operations

πŸ› οΈ Technologies Used

  • .NET 8: Main framework
  • Entity Framework Core: ORM for data access
  • MySQL: Relational database
  • RabbitMQ: Message broker for asynchronous communication
  • MassTransit: Library for RabbitMQ abstraction
  • JWT: Authentication and authorization
  • Ocelot: API Gateway
  • BCrypt.NET: Password encryption
  • Serilog: Logging system
  • Docker & Docker Compose: Containerization
  • xUnit: Unit testing framework
  • Bogus: Test data generation

πŸ”„ Microservices Communication

The system uses asynchronous events via RabbitMQ:

Sales Flow:

  1. SaleCreated β†’ StockService reserves stock
  2. SaleItemsReservedResponse β†’ SalesService confirms the sale
  3. SaleConfirmed β†’ StockService removes from stock
  4. SaleCanceled β†’ StockService releases reserved stock

Implemented Events:

  • SaleCreated: New order created
  • SaleItemsReservedResponse: Items successfully reserved
  • SaleCreationFailed: Sale creation failure
  • SaleConfirmed: Sale confirmed
  • SaleCanceled: Sale canceled
  • StockReleased: Stock released
  • StockCanceled: Stock cancellation

πŸ” Authentication System

User Types:

  • Client: Can make purchases and check their orders
  • Manager: Can manage products, inventory, and create new managers

Authentication Endpoints:

  • POST /auth/login - User login
  • POST /auth/register/user - Client registration
  • POST /auth/register/manager - Manager registration (requires Manager authentication)

πŸ“Š Main Features

Product Management (Manager):

  • New product registration
  • Product information updates
  • Product and price queries

Inventory Management (Manager):

  • Available and reserved stock queries
  • Item reservation and release
  • Stock removal

Sales Management (Client):

  • Order creation
  • Order history queries
  • Pending order cancellation

πŸš€ How to Run

Prerequisites:

  • Docker
  • Docker Compose

Running with Docker:

  1. Clone the repository:
git clone https://github.com/Alekssandher/ECommerce-Microservices/
cd ECommerce-Microservices
  1. Build and run the containers:
docker-compose up --build
  1. Wait for services to be ready:
  • MySQL: Port 3306
  • RabbitMQ: Ports 5672 (AMQP) and 15672 (Management UI)
  • API Gateway: Port 5001

Service URLs:

πŸ“ API Endpoints

Authentication:

POST /auth/login
POST /auth/register/user
POST /auth/register/manager (Manager only)

Products:

GET    /products                    # List products
GET    /products/{productId}        # Get product
POST   /products                    # Create product (Manager only)
PUT    /products                    # Update product (Manager only)

Stock:

GET    /stock/{productId}                      # Get stock (Manager only)
GET    /stock/{productId}/available            # Get available stock (Manager only)
POST   /stock/{productId}/reserve/{quantity}   # Reserve stock (Manager only)
POST   /stock/{productId}/release/{quantity}   # Release stock (Manager only)
DELETE /stock/{productId}/remove/{quantity}    # Remove stock (Manager only)

Sales:

GET    /sales/{id}      # Get sale (Client only)
PATCH  /sales/{id}      # Update sale (Client only)
DELETE /sales/{id}      # Cancel sale (Client only)

πŸ§ͺ Testing

The project includes comprehensive unit tests for:

  • Mappers
  • Models
  • Services

Run tests:

dotnet test

Test Coverage:

  • AuthService: Models, JWT services, and mappers
  • SalesService: Models, sales services, and mappers
  • StockService: Models, product/stock services, and mappers

πŸ—‚οΈ Project Structure

β”œβ”€β”€ AuthService/                 # Authentication microservice
β”œβ”€β”€ Services/
β”‚   β”œβ”€β”€ SalesService/           # Sales microservice
β”‚   └── StockService/           # Stock microservice
β”œβ”€β”€ Gateway/                    # API Gateway
β”œβ”€β”€ Shared/                     # Shared libraries
β”‚   β”œβ”€β”€ Extensions/            # Extensions for JWT, RabbitMQ, etc.
β”‚   β”œβ”€β”€ Messages/              # Communication events
β”‚   β”œβ”€β”€ Middlewares/           # Global exception middleware
β”‚   └── ModelViews/            # DTOs and standardized responses
β”œβ”€β”€ ECommerce-Microservices.Tests/ # Test projects
β”œβ”€β”€ docker-compose.yml         # Docker configuration
└── README.md

πŸ“‹ Databases

Each microservice has its own database:

  • users_auth_service: User and authentication data
  • sales_micro_service: Sales and order data
  • stock_micro_service: Product and inventory data

πŸ” Monitoring and Logs

  • Serilog: Structured logging for all services
  • Health Checks: /health endpoint in each service
  • RabbitMQ Management: Web interface for queue monitoring

πŸ›‘οΈ Security

  • JWT authentication with symmetric keys
  • Role-based authorization (Client/Manager)
  • Password encryption with BCrypt
  • Rate limiting in API Gateway
  • Input validation on all endpoints

🎯 Implemented Best Practices

  • Separation of Concerns: Each microservice has a single responsibility
  • Event-Driven Architecture: Asynchronous communication via events
  • Exception Handling: Global middleware for exception handling
  • Logging: Structured logs in all services
  • Testing: Comprehensive unit test coverage
  • Containerization: Fully dockerized application
  • Database per Service: Each microservice with its own database

🀝 Contributing

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

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for more details.

πŸ“ž Support

For questions or support, please contact through GitHub issues.

About

E-commerce with microservices in .NET 9 + RabbitMQ + MySQL + Docker. Product, inventory, and sales management with JWT authentication and asynchronous communication

Topics

Resources

License

Stars

Watchers

Forks