# pig-mesh/pig architecture

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

- Interactive diagram: https://gitdiagram.com/pig-mesh/pig
- Repository: https://github.com/pig-mesh/pig

## Overview

##### Main Components
- Frontend & API Layer: Although the project includes a separate pig-ui repository (hosted externally on Gitee), the core backend is exposed via the gateway.
- API Gateway (pig-gateway): Acts as the front door for external requests. It routes and filters traffic via Spring Cloud Gateway on port [9999].
- Authorization Service (pig-auth): Provides OAuth2–based authentication/authorization, issuing tokens and handling login–related endpoints (port 3000).
- User Management Module (pig-upms): Divided between an API module (common interfaces) and a business processing module (pig-upms-biz, on port [4000]), handling user, role and permission logic.
- Service Discovery/Registry (pig-register): Implements a Nacos server (port [8848]) allowing the dynamic discovery of microservices.
- Boot Starter (pig-boot): In a monolithic setting this module can bootstrap the entire application.
- Common Modules (pig-common): A suite of shared libraries (dependency management, logging, datasource, security, mybatis enhancements, file upload/OSS, distributed transactions with Seata, swagger documentation and XSS protection) that many services depend on.
- Visual Management & DevOps Tools (pig-visual): Contains submodules for monitoring (pig-monitor, port [5001]), code generation (pig-codegen, port [5002]), and scheduled task management (pig-quartz, port [5007]).
- Infrastructure & Database: The “db” folder contains the Docker deployment files and SQL scripts (pig.sql, pig_config.sql); a docker-compose.yml file is available for container orchestration during development. Also, integration with container orchestration systems like Kubernetes or Rancher2 is mentioned in the README.

##### Relationships and Interactions
- External clients first hit the API Gateway, which forwards requests according to routing rules.
- The gateway routes secured requests to the pig-auth service for token validation and the pig-upms module for permission checks.
- When microservices need to communicate, they make use of the pig-common libraries and the service registry (pig-register) for dynamic discovery.
- Pig-boot can be used in a monolith mode, aggregating many of the microservices internally when a simpler deployment is preferred.
- Shared concerns such as logging, security, distributed transactions, and file storage are abstracted into the pig-common modules.
- Visual modules (monitoring, code generation, quartz for scheduled tasks) provide supporting dashboards and administrative interfaces.

##### Architectural Patterns and Design Principles
- Microservices Architecture: Separate services (auth, gateway, user management, etc.) communicate through REST APIs and use a centralized service registry.
- Separation of Concerns: Each module (authorization, user management, gateway, etc.) handles a discrete responsibility.
- Shared Common Libraries: The pig-common modules define cross-cutting concerns that benefit from reusability and reduced code duplication.
- Containerization: The presence of Dockerfiles for almost every service and a docker-compose configuration file indicates that container orchestration (and eventual deployment to Kubernetes or similar platforms) is a key architectural element.
- Security and RBAC: The project uses Spring Authorization Server techniques, OAuth2 access token management (with custom generators and converters), and security filters to enforce application security.

## Diagram (Mermaid)

```mermaid
flowchart TD

%% External Layer
subgraph "External Layer"
    EX("External Clients"):::external
end

%% API Layer
subgraph "API Layer"
    API_Gateway("API Gateway (pig-gateway)<br/>(Port 9999)"):::gateway
end

%% Core Microservices
subgraph "Core Microservices"
    Auth("Authorization Service (pig-auth)<br/>(Port 3000)"):::service
    UM_Api("User Management – API (pig-upms-api)"):::service
    UM_Biz("User Management – Business (pig-upms-biz)<br/>(Port 4000)"):::service
    Registry("Service Registry (pig-register)<br/>(Port 8848)"):::service
    Common("Common Modules (pig-common)"):::common
end

%% Visual Management Tools
subgraph "Visual Management Tools"
    Monitor("Monitoring (pig-monitor)<br/>(Port 5001)"):::visual
    Codegen("Code Generation (pig-codegen)<br/>(Port 5002)"):::visual
    Quartz("Scheduled Tasks (pig-quartz)<br/>(Port 5007)"):::visual
end

%% Monolithic Deployment
subgraph "Monolithic Deployment"
    Boot("Boot Starter (pig-boot)"):::monolithic
end

%% Infrastructure
subgraph "Infrastructure"
    Infra("Database & Infrastructure (db)"):::infra
end

%% Connections
EX --> API_Gateway

API_Gateway -->|"routes"| Auth
API_Gateway -->|"routes"| UM_Api
API_Gateway -->|"routes"| UM_Biz

Auth -->|"discovers"| Registry
UM_Api -->|"discovers"| Registry
UM_Biz -->|"discovers"| Registry
API_Gateway -->|"discovers"| Registry

Auth -->|"uses"| Common
UM_Api -->|"uses"| Common
UM_Biz -->|"uses"| Common

Boot -->|"aggregates"| Auth
Boot -->|"aggregates"| UM_Biz
Boot -->|"uses"| Common

EX -->|"accesses"| Monitor
EX -->|"accesses"| Codegen
EX -->|"accesses"| Quartz

Monitor --> Registry
Codegen --> Registry
Quartz --> Registry

Infra -->|"hosts"| Registry
Infra -->|"hosts"| API_Gateway
Infra -->|"hosts"| Auth
Infra -->|"hosts"| UM_Api
Infra -->|"hosts"| UM_Biz
Infra -->|"hosts"| Boot
Infra -->|"hosts"| Monitor
Infra -->|"hosts"| Codegen
Infra -->|"hosts"| Quartz

%% Click Events
click API_Gateway "https://github.com/pig-mesh/pig/tree/jdk17/pig-gateway/"
click Auth "https://github.com/pig-mesh/pig/tree/jdk17/pig-auth/"
click UM_Api "https://github.com/pig-mesh/pig/tree/jdk17/pig-upms/pig-upms-api"
click UM_Biz "https://github.com/pig-mesh/pig/tree/jdk17/pig-upms/pig-upms-biz"
click Registry "https://github.com/pig-mesh/pig/tree/jdk17/pig-register/"
click Boot "https://github.com/pig-mesh/pig/blob/jdk17/pig-boot/PigBootApplication.java"
click Common "https://github.com/pig-mesh/pig/tree/jdk17/pig-common/"
click Codegen "https://github.com/pig-mesh/pig/tree/jdk17/pig-visual/pig-codegen"
click Monitor "https://github.com/pig-mesh/pig/tree/jdk17/pig-visual/pig-monitor"
click Quartz "https://github.com/pig-mesh/pig/tree/jdk17/pig-visual/pig-quartz"
click Infra "https://github.com/pig-mesh/pig/tree/jdk17/db"

%% Styles
classDef external fill:#F4D03F,stroke:#D4AC0D,stroke-width:2px;
classDef gateway fill:#5DADE2,stroke:#2471A3,stroke-width:2px;
classDef service fill:#58D68D,stroke:#27AE60,stroke-width:2px;
classDef common fill:#AF7AC5,stroke:#8E44AD,stroke-width:2px;
classDef visual fill:#F1948A,stroke:#C0392B,stroke-width:2px;
classDef monolithic fill:#F7DC6F,stroke:#F1C40F,stroke-width:2px;
classDef infra fill:#D5DBDB,stroke:#7F8C8D,stroke-width:2px;
```
