# leanthuyen/microserviceslearning architecture

> Architecture diagram of the GitHub repository leanthuyen/microserviceslearning, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/leanthuyen/microserviceslearning
- Repository: https://github.com/leanthuyen/microserviceslearning

## Overview

##### Project type and purpose
- This is a .NET 6 “Clean Architecture” microservices solution orchestrated via Docker Compose.
- It comprises an Ocelot API Gateway at the edge, a set of domain‐centric API services (Product, Customer, Basket, Inventory, Ordering, Hangfire) plus a background subscriber, all sharing common Building-Blocks libraries for logging, contracts, event bus integration, and shared DTOs/configurations.
- It’s designed for an e-commerce workflow: product catalog, customer management, shopping basket, inventory, order placement via events, and email notifications.

##### File structure
- Top level: solution file (TanaMicroservices.sln), Docker files, Compose files, global.json.
- src/ApiGateways/OcelotApiGW → API Gateway project.
- src/BuildingBlocks → shared libraries (Common.Logging, Contracts, EventBus, Infrastructure, Shared DTOs).
- src/Services → each microservice in its own folder; each follows Clean Architecture layering (API host, Application, Domain, Infrastructure/Persistence, Migrations).
- docker-compose.yml & override → container definitions for all services plus RabbitMQ, Redis, Postgres, MongoDB, Kibana, Portainer, Jenkins.

##### README
- Confirms ports for each service in local vs Docker.
- Lists external infrastructure: Portainer, Kibana, RabbitMQ, Postgres GUI, Jenkins.
- Mentions Clean Architecture layers and EF Core migration commands.

##### Main components
a. Edge Layer
- OcelotApiGW (HTTP entry point)
b. Microservices Layer
- Product.API (.NET 6, EF Core → Postgres)
- Customer.API (.NET 6, EF Core → Postgres)
- Basket.API (.NET 6, Redis cache)
- Inventory.API (.NET 6, MongoDB)
- Ordering.API (.NET 6, EF Core → Postgres)
- Hangfire.API (.NET 6, background job dashboard)
- Ordering.Subscriber (background event handler)
c. Integration Layer
- RabbitMQ message broker (pub/sub for integration events)
d. Data Layer
- Postgres/SQL Server instances for Product, Customer, Ordering
- MongoDB for Inventory
- Redis for Basket
e. Shared Building-Blocks
- Contracts (events, DTOs, interfaces)
- Common.Logging (Serilog)
- Infrastructure (UnitOfWork, Repositories, RabbitMQProducer)
f. External Infrastructure
- SMTP Email Service (SmtpEmailService)
- Jenkins (CI/CD)
- Kibana (log visualization)
- Portainer (container management)

##### Relationships and interactions
1. Client → HTTP calls → OcelotApiGW → Routes to individual Microservice endpoints
2. Microservice → own database for persistent data
3. Basket.API publishes BasketCheckoutEvent → RabbitMQ
4. Ordering.API & Ordering.Subscriber subscribe to BasketCheckoutEvent → process order, persist → send confirmation email via SMTP
5. Internal sync calls (if any) for product/customer lookups via Gateway
6. Hangfire.API exposes dashboards for scheduling/monitoring background jobs

##### Architectural patterns and design principles
- Microservices: independent deployable units, one database per service
- Clean Architecture: each service layered (API/Host → Application → Domain → Infrastructure → Persistence/Migrations)
- API Gateway pattern: Ocelot routing, aggregation, authentication, rate-limiting if needed
- Event-Driven Architecture: RabbitMQ for decoupled integration events
- DDD elements: Entities, Aggregates, Domain Events, Repositories, Behaviours (performance, validation)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Edge Layer
    subgraph "Edge Layer"
        OcelotApiGW["Ocelot API Gateway"]:::micro
    end

    %% Microservices Layer
    subgraph "Microservices" 
        direction TB
        ProductAPI["Product.API"]:::micro
        CustomerAPI["Customer.API"]:::micro
        BasketAPI["Basket.API"]:::micro
        InventoryAPI["Inventory.API"]:::micro
        OrderingAPI["Ordering.API"]:::micro
        HangfireAPI["Hangfire.API"]:::micro
        OrderingSubscriber["Ordering.Subscriber"]:::micro
    end

    %% Integration Layer
    subgraph "Integration"
        RabbitMQ["RabbitMQ"]:::broker
    end

    %% Data Layer
    subgraph "Datastores"
        Postgres1["Postgres (Product)"]:::db
        Postgres2["Postgres (Customer)"]:::db
        Postgres3["Postgres (Ordering)"]:::db
        Redis["Redis"]:::db
        MongoDB["MongoDB"]:::db
    end

    %% Shared Building-Blocks
    subgraph "Shared Building-Blocks"
        CommonLogging["Common.Logging"]:::shared
        Contracts["Contracts"]:::shared
        EventBusBB["EventBus.Messages"]:::shared
        InfrastructureBB["Infrastructure"]:::shared
        SharedDTOs["Shared DTOs & Config"]:::shared
    end

    %% Operations
    subgraph "Operations"
        SMTP["SMTP Email Service"]:::external
        Jenkins["Jenkins"]:::external
        Kibana["Kibana"]:::external
        Portainer["Portainer"]:::external
    end

    %% Client to Gateway
    Client["Client"]:::external -->|"HTTP"| OcelotApiGW

    %% Gateway to Services
    OcelotApiGW -->|"HTTP"| ProductAPI
    OcelotApiGW -->|"HTTP"| CustomerAPI
    OcelotApiGW -->|"HTTP"| BasketAPI
    OcelotApiGW -->|"HTTP"| InventoryAPI
    OcelotApiGW -->|"HTTP"| OrderingAPI
    OcelotApiGW -->|"HTTP"| HangfireAPI

    %% Services to Datastores
    ProductAPI -->|EF Core| Postgres1
    CustomerAPI -->|EF Core| Postgres2
    OrderingAPI -->|EF Core| Postgres3
    BasketAPI -->|Cache| Redis
    InventoryAPI -->|NoSQL| MongoDB

    %% Event Flows
    BasketAPI -.->|"AMQP BasketCheckoutEvent"| RabbitMQ
    RabbitMQ -.->|"AMQP BasketCheckoutEvent"| OrderingAPI
    RabbitMQ -.->|"AMQP BasketCheckoutEvent"| OrderingSubscriber
    OrderingAPI -->|"SMTP"| SMTP

    %% Shared Blocks Usage
    ProductAPI --> CommonLogging
    ProductAPI --> Contracts
    ProductAPI --> EventBusBB
    ProductAPI --> InfrastructureBB
    ProductAPI --> SharedDTOs

    CustomerAPI --> CommonLogging
    CustomerAPI --> Contracts
    CustomerAPI --> InfrastructureBB
    CustomerAPI --> SharedDTOs

    BasketAPI --> CommonLogging
    BasketAPI --> Contracts
    BasketAPI --> InfrastructureBB
    BasketAPI --> SharedDTOs

    InventoryAPI --> CommonLogging
    InventoryAPI --> Contracts
    InventoryAPI --> InfrastructureBB
    InventoryAPI --> SharedDTOs

    OrderingAPI --> CommonLogging
    OrderingAPI --> Contracts
    OrderingAPI --> EventBusBB
    OrderingAPI --> InfrastructureBB
    OrderingAPI --> SharedDTOs

    HangfireAPI --> CommonLogging
    HangfireAPI --> SharedDTOs

    OrderingSubscriber --> CommonLogging
    OrderingSubscriber --> Contracts
    OrderingSubscriber --> EventBusBB
    OrderingSubscriber --> SharedDTOs

    %% Operations Monitoring
    HangfireAPI --> Kibana
    ProductAPI --> Kibana
    CustomerAPI --> Kibana
    BasketAPI --> Kibana
    InventoryAPI --> Kibana
    OrderingAPI --> Kibana
    OrderingSubscriber --> Kibana

    RabbitMQ --> Portainer
    Postgres1 --> Portainer
    Redis --> Portainer
    MongoDB --> Portainer

    Jenkins --> Portainer

    %% Click Events
    click OcelotApiGW "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/ApiGateways/OcelotApiGW/OcelotApiGW.csproj"
    click CommonLogging "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/BuildingBlocks/Common.Logging/Common.Logging.csproj"
    click Contracts "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/BuildingBlocks/Contracts/Contracts.csproj"
    click EventBusBB "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/BuildingBlocks/EventBus/EventBus.Messages/EventBus.Messages.csproj"
    click InfrastructureBB "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/BuildingBlocks/Infrastructure/Infrastructure.csproj"
    click SharedDTOs "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/BuildingBlocks/Shared/Shared.csproj"
    click ProductAPI "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/Services/Product.API/Product.API.csproj"
    click CustomerAPI "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/Services/Customer.API/Customer.API.csproj"
    click BasketAPI "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/Services/Basket.API/Basket.API.csproj"
    click InventoryAPI "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/Services/Inventory.API/Inventory.Product.API.csproj"
    click OrderingAPI "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/Services/Ordering.API/Ordering.Api/Ordering.API.csproj"
    click HangfireAPI "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/Services/Hangfire.API/Hangfire.API.csproj"
    click OrderingSubscriber "https://github.com/leanthuyen/microserviceslearning/blob/Develop/src/Services/Ordering.Subscriber/Ordering.Subscriber.csproj"

    %% Styles
    classDef micro fill:#D0E8FF,stroke:#3399FF,stroke-width:1px
    classDef db fill:#FFE0B2,stroke:#FF9800,stroke-width:1px
    classDef broker fill:#FFCDD2,stroke:#F44336,stroke-width:1px
    classDef shared fill:#C8E6C9,stroke:#4CAF50,stroke-width:1px
    classDef external fill:#E0E0E0,stroke:#9E9E9E,stroke-width:1px
```
