# lileijordan/hmall architecture

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

- Interactive diagram: https://gitdiagram.com/lileijordan/hmall
- Repository: https://github.com/lileijordan/hmall

## Overview

##### Main Components
a. UI / Client Layer:
- Web applications and static content served via hmall-nginx. This includes the admin and customer portal pages.
b. API Gateway:
- hm-gateway serves as the entry point. It handles routing, authentication (via Jwt and security configs), and filters (e.g., LoginGlobalFilter).
c. Microservices:
- cart-service: Manages shopping cart operations.
- item-service: Handles product and inventory management.
- pay-service: Processes payments.
- trade-service: Manages orders and trade details.
- user-service: Handles user and address information.
d. Shared Components:
- hm-common: Contains common utilities, configuration files (e.g., MyBatisConfig, MvcConfig), exception handling, and database scripts.
- hm-api: Provides the open feign interfaces, DTOs, fallback mechanisms, and interceptors that allow inter-service communication.
e. Middleware / External Services:
- Nacos: For centralized configuration management.
- Seata: For distributed transactions.
- Sentinel: For fault tolerance and rate limiting.
- Other middleware (e.g., Feign for remote calls).

##### Relationships and Interactions
- Requests from users initiate at the UI layer (nginx) and are forwarded to the hm-gateway.
- The gateway, after applying security and routing rules (with JWT and security filters), directs the requests to the corresponding microservices.
- Within each microservice, the MVC (or layered) structure is evident:
- Controllers receive HTTP requests.
- Service layers (with interfaces and implementations) process business logic.
- Mappers/data access layers interact with the underlying MySQL databases.
- Inter-service communication is handled via Feign clients defined in hm-api. For example, the Cart service might call the Item service via CartClient or ItemClient.
- Shared modules (hm-common) are imported across services to standardize exception handling, JSON configurations, and other utilities.
- Middleware components such as Nacos, Sentinel, and Seata are integrated into configuration files (bootstrap.yaml, application.yaml) in each service to support configuration, monitoring, and distributed transaction management.

##### Key Architectural Patterns
- Microservices orchestration using Spring Cloud.
- API gateway pattern to centralize access and routing.
- Layered (MVC) architecture within each service.
- Use of shared libraries (hm-common) and API contracts (hm-api) for consistency.
- Distributed transaction management (Seata) and circuit breaking/rate limiting (Sentinel).

##### Relevant Technologies and Frameworks
- Spring Boot and Spring Cloud across services.
- MyBatis for data persistence.
- Nacos for dynamic configuration.
- Seata for distributed transaction management.
- Sentinel for fault-tolerance.
- Feign for declarative REST clients.
- Docker (as mentioned) for deploying non-JDK components like Sentinel.
- JDK 11 as the project’s runtime base.
- NGINX for serving static web pages (admin/portal).

##### Detailed Steps for the Principal Software Engineer
- Within the microservices layer, list and separate each service (cart, item, pay, trade, user) and briefly annotate their responsibilities based on the file paths and domain classes.
- A side note block for shared modules (hm-common, hm-api) and how they integrate with the microservices (imported as dependencies for consistent functionality).
- Middleware services (Nacos, Seata, Sentinel) to each microservice and the gateway to emphasize their roles in configuration and resiliency.
- Flow of requests from the UI to the backend, ensuring a clear depiction of how requests are authenticated, processed, and how inter-service communication works via Feign clients.
- Finally, mark configuration points (application.yaml, bootstrap.yaml) as examples of where central configuration is applied.
- Being detailed by capturing not only the distinct services but also the crucial infrastructural dependencies that drive the project’s behavior.

## Diagram (Mermaid)

```mermaid
graph TD
    %% UI / Client Layer
    subgraph "UI/Client Layer"
        UI["hMall Nginx (Static Assets)"]:::ui
    end

    %% API Gateway Layer
    subgraph "API Gateway"
        GW["hm-gateway"]:::gateway
    end

    %% Microservices Layer
    subgraph "Microservices"
        CART["Cart Service"]:::microservice
        ITEM["Item Service"]:::microservice
        PAY["Pay Service"]:::microservice
        TRADE["Trade Service"]:::microservice
        USER["User Service"]:::microservice
    end

    %% Shared Components
    subgraph "Shared Components"
        COMMON["hm-common"]:::shared
        API["hm-api"]:::shared
    end

    %% Middleware / External Services
    subgraph "Middleware"
        NACOS["Nacos"]:::middleware
        SEATA["Seata"]:::middleware
        SENTINEL["Sentinel"]:::middleware
    end

    %% Request Flow
    UI -->|"HTTPRequest"| GW

    %% Gateway routes requests to Microservices
    GW -->|"Route&Auth"| CART
    GW -->|"Route&Auth"| ITEM
    GW -->|"Route&Auth"| PAY
    GW -->|"Route&Auth"| TRADE
    GW -->|"Route&Auth"| USER

    %% Microservices depend on Shared Components
    CART -->|"Uses"| COMMON
    CART -->|"Uses"| API
    ITEM -->|"Uses"| COMMON
    ITEM -->|"Uses"| API
    PAY  -->|"Uses"| COMMON
    PAY  -->|"Uses"| API
    TRADE -->|"Uses"| COMMON
    TRADE -->|"Uses"| API
    USER -->|"Uses"| COMMON
    USER -->|"Uses"| API

    %% Middleware integrations for Gateway
    GW -->|"Config/Resilience"| NACOS
    GW -->|"Config/Resilience"| SEATA
    GW -->|"Config/Resilience"| SENTINEL

    %% Middleware integrations for Microservices
    CART -->|"Config/Resilience"| NACOS
    CART -->|"Config/Resilience"| SEATA
    CART -->|"Config/Resilience"| SENTINEL

    ITEM -->|"Config/Resilience"| NACOS
    ITEM -->|"Config/Resilience"| SEATA
    ITEM -->|"Config/Resilience"| SENTINEL

    PAY  -->|"Config/Resilience"| NACOS
    PAY  -->|"Config/Resilience"| SEATA
    PAY  -->|"Config/Resilience"| SENTINEL

    TRADE -->|"Config/Resilience"| NACOS
    TRADE -->|"Config/Resilience"| SEATA
    TRADE -->|"Config/Resilience"| SENTINEL

    USER -->|"Config/Resilience"| NACOS
    USER -->|"Config/Resilience"| SEATA
    USER -->|"Config/Resilience"| SENTINEL

    %% Inter-service communication example using Feign Clients
    CART <-->|"FeignCall"| ITEM

    %% Styles
    classDef ui fill:#a8d0e6,stroke:#333,stroke-width:2px;
    classDef gateway fill:#f76c6c,stroke:#333,stroke-width:2px;
    classDef microservice fill:#f7d794,stroke:#333,stroke-width:2px;
    classDef shared fill:#c9e4c5,stroke:#333,stroke-width:2px;
    classDef middleware fill:#b8a9c9,stroke:#333,stroke-width:2px;

    %% Click Events
    click UI "https://github.com/lileijordan/hmall/tree/master/hmall-nginx/html"
    click GW "https://github.com/lileijordan/hmall/tree/master/hm-gateway/src/main/java/com/hmall/gateway"
    click CART "https://github.com/lileijordan/hmall/tree/master/cart-service/src/main/java/com/hmall/cart"
    click ITEM "https://github.com/lileijordan/hmall/tree/master/item-service/src/main/java/com/hmall/item"
    click PAY "https://github.com/lileijordan/hmall/tree/master/pay-service/src/main/java/com/hmall/pay"
    click TRADE "https://github.com/lileijordan/hmall/tree/master/trade-service/src/main/java/com/hmall/trade"
    click USER "https://github.com/lileijordan/hmall/tree/master/user-service/src/main/java/com/hmall/user"
    click COMMON "https://github.com/lileijordan/hmall/tree/master/hm-common/src/main/java/com/hmall/common"
    click API "https://github.com/lileijordan/hmall/tree/master/hm-api/src/main/java/com/hmall/api"
```
