# louisyi-n/miaosha architecture

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

- Interactive diagram: https://gitdiagram.com/louisyi-n/miaosha
- Repository: https://github.com/louisyi-n/miaosha

## Overview

##### Project type and purpose
- This is a full-stack, high-concurrency “秒杀”(flash sale) system built with Spring Boot, Dubbo/RPC, Redis, RabbitMQ, MySQL and Zookeeper.
- It consists of multiple Maven sub-modules: core seckill app (miaosha-2version), admin portal (miaosha-admin), order microservice (miaosha-order), demo SpringBoot + Dubbo examples (springboot-dubbo), plus common libraries.

##### Top-level components
a. Clients
– Web browser/mobile app (静态页面 + AJAX calls)
– Admin UI
b. Edge/proxy
– Nginx (static caching, load balancing)
c. API/Web Tier
– miaosha-web (Spring Boot controllers, interceptors, templates, static resources)
– miaosha-admin-web (Freemarker, JSP)
d. Service Tier
– miaosha-service (business logic: GoodsService, MiaoshaService, OrderService)
– miaosha-admin-service (admin APIs)
– miaosha-order-provider (order microservice via Dubbo)
– springboot-dubbo/provider & consumer demos
e. RPC framework
– Dubbo clients & providers, Zookeeper for service registry
f. Messaging
– RabbitMQ (MQSender → MQReceiver for order queue)
g. Cache & Locking
– Redis cluster (stock pre-decrement, distributed locks via Lua/redisson)
h. Persistence
– MySQL master-slave cluster
i. Infrastructure & config
– Zookeeper ensemble, monitoring/logs (optional)

##### Relationships & interactions
1. Client → HTTP → Nginx (LB/static) → Spring Boot web app
2. Web app →
- Redis (GET initial stock, check sold-out flag)
- Local in-memory flags
- If allowed, push message to RabbitMQ queue
3. MQReceiver (in service tier) →
- Consume seckill request → RPC (Dubbo) if order service is remote → Business logic →
- Persist order to MySQL → Cache update
4. Web app polls or returns async result (via Redis or database)
5. Admin UI → Dubbo/Admin service → MySQL/Redis

##### Architectural patterns & principles
- Layered architecture: controller → service → dao → database
- Asynchronous processing with message queue for smoothing spikes
- Cache-aside & pre-decrement pattern in Redis for inventory
- Distributed locking via Redis/Lua scripts or Redisson
- RPC microservices via Dubbo + Zookeeper
- Master-slave MySQL for read/write separation
- Externalized configuration via Spring profiles, properties, XML for Dubbo

##### Guidance
– RPC (dot-dash) between service modules via Dubbo/ZK
– UI/Client (blue)
– Web/API (green)
– Business Service (yellow)
– Infrastructure (gray)
- Flows: e.g., “1. 浏览器请求商品列表”, “2. Redis 库存检查”, “3. MQ 下单消息”, “4. 消费并生成订单”.

##### Detailed elements
- Nginx/Load balancer → multiple instances of miaosha-web & miaosha-admin-web
- Dubbo consumers in web/app modules → Dubbo providers in service modules
- RabbitMQ cluster between Web and Service
- Redis cluster for cache, lock, sold-out flag
- MySQL master for writes, slaves for reads
- Zookeeper ensemble for service registry
- Local memory map in Web app (sold-out flags for fast-fail)
- Key configuration files (application-*.properties, Dubbo XML) can be footnoted

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Layer
    subgraph "Clients" 
        direction TB
        Browser["Browser/Mobile"]:::client
        AdminUI["Admin UI"]:::client
    end

    %% Edge Layer
    Nginx["Nginx (LB / Static Cache)"]:::infra

    %% Web/API Tier
    subgraph "Web/API Tier"
        direction TB
        MSWeb["miaosha-web\n(Spring Boot + Thymeleaf)"]:::web
        AdminWeb["miaosha-admin-web\n(Freemarker/JSP)"]:::web
    end

    %% Service Tier
    subgraph "Service Tier"
        direction TB
        MService["miaosha-service\n(Business Logic)"]:::service
        AdminService["miaosha-admin-service\n(Admin APIs)"]:::service
        OrderProvider["miaosha-order-provider\n(Dubbo Order MS)"]:::service
        subgraph "Dubbo/Demo"
            direction TB
            DConsumer["SpringBoot-Dubbo\nConsumer Demo"]:::service
            DProvider["SpringBoot-Dubbo\nProvider Demo"]:::service
        end
    end

    %% Infrastructure
    subgraph "Infrastructure"
        direction TB
        ZK["Zookeeper\n(Service Registry)"]:::infra
        Redis["Redis Cluster\n(Cache & Lock)"]:::infra
        RabbitMQ["RabbitMQ Cluster\n(Message Queue)"]:::infra
        MySQLMaster["MySQL Master"]:::infra
        MySQLSlave["MySQL Slave"]:::infra
    end

    %% Flows
    Browser -->|"1. HTTP Request"| Nginx
    AdminUI -->|"1. HTTP Request"| Nginx

    Nginx --> MSWeb
    Nginx --> AdminWeb

    MSWeb -->|"2. GET stock / Check flag"| Redis
    MSWeb -->|"2a. Local memory flags"| MSWeb
    MSWeb -->|"3. Push order msg"| RabbitMQ

    RabbitMQ -->|"4. Consume order"| MService
    MService -->|"RPC / Dubbo"| OrderProvider
    OrderProvider -->|"Write Order"| MySQLMaster
    MService -->|"Update Cache"| Redis

    AdminWeb -->|"Admin RPC"| AdminService
    AdminService -->|"Read/Write"| Redis
    AdminService -->|"Read/Write"| MySQLMaster

    MSWeb -->|"RPC / Dubbo"| MService
    MService --> ZK
    OrderProvider --> ZK
    DConsumer -->|"RPC / Dubbo"| DProvider
    DProvider --> ZK

    MSWeb -->|"Poll Result"| Redis
    MSWeb -->|"Fallback read"| MySQLSlave

    %% Click Events
    click Browser "https://github.com/louisyi-n/miaosha/tree/master/miaosha-2version/miaosha-web/src/main/resources/static"
    click Browser "https://github.com/louisyi-n/miaosha/tree/master/miaosha-2version/miaosha-web/src/main/resources/templates"
    click AdminUI "https://github.com/louisyi-n/miaosha/tree/master/miaosha-admin/miaosha-admin-web/src/main/webapp/WEB-INF/views"
    click AdminUI "https://github.com/louisyi-n/miaosha/tree/master/miaosha-admin/miaosha-admin-web/src/main/webapp"
    click MSWeb "https://github.com/louisyi-n/miaosha/tree/master/miaosha-2version/miaosha-web/src/main/java/com/geekq/miaosha/controller"
    click AdminWeb "https://github.com/louisyi-n/miaosha/tree/master/miaosha-admin/miaosha-admin-web/src/main/java/com/geekq/web/controller"
    click MService "https://github.com/louisyi-n/miaosha/tree/master/miaosha-2version/miaosha-service/src/main/java/com/geekq/miaosha/service"
    click AdminService "https://github.com/louisyi-n/miaosha/tree/master/miaosha-admin/miaosha-admin-service/src/main/java/com/geekq/admin/service/impl"
    click OrderProvider "https://github.com/louisyi-n/miaosha/tree/master/miaosha-order/miaosha-order-provider/src/main/java/com/geekq/provider"
    click DConsumer "https://github.com/louisyi-n/miaosha/tree/master/springboot-dubbo/consumer/src/main/java/com/geekq/dubbo/springboot/consumer"
    click DProvider "https://github.com/louisyi-n/miaosha/tree/master/springboot-dubbo/privoder/src/main/java/com/geekq/dubbo/springboot/privoder"
    click Redis "https://github.com/louisyi-n/miaosha/blob/master/miaosha-2version/miaosha-service/src/main/java/com/geekq/miaosha/redis/RedisConfig.java"
    click Redis "https://github.com/louisyi-n/miaosha/tree/master/miaosha-2version/miaosha-service/src/main/java/com/geekq/miaosha/redis/redismanager/lua"
    click MService "https://github.com/louisyi-n/miaosha/tree/master/miaosha-2version/miaosha-service/src/main/java/com/geekq/miaosha/rabbitmq"
    click MService "https://github.com/louisyi-n/miaosha/blob/master/miaosha-2version/miaosha-service/src/main/resources/consumer.xml"
    click AdminService "https://github.com/louisyi-n/miaosha/blob/master/miaosha-admin/miaosha-admin-service/src/main/resources/spring/applicationContext-dubbo-provider.xml"
    click MService "https://github.com/louisyi-n/miaosha/tree/master/miaosha-2version/miaosha-service/src/main/java/com/geekq/miaosha/mapper"
    click AdminService "https://github.com/louisyi-n/miaosha/tree/master/miaosha-admin/miaosha-admin-service/src/main/java/com/geekq/admin/mapper"
    click ZK "https://github.com/louisyi-n/miaosha/blob/master/docs/dubbo-zk.md"
    click Nginx "https://github.com/louisyi-n/miaosha/blob/master/docs/ngnix-good.md"

    %% Styles
    classDef client fill:#cce5ff,stroke:#004085,color:#004085
    classDef web fill:#d4edda,stroke:#155724,color:#155724
    classDef service fill:#fff3cd,stroke:#856404,color:#856404
    classDef infra fill:#e2e3e5,stroke:#6c757d,color:#6c757d
```
