mengstack-website/guide/architecture.md
SoftUnis ecb29e0aa4 初始化 MengStack 官方网站仓库
包含 VitePress 文档站完整源码及全新自定义首页设计:
- 自定义首页组件 (Home.vue):Hero + 终端动效 + 核心能力 + 架构展示 + 快速开始 + CTA
- 自定义 Layout.vue:首页/文档页布局切换
- 金铜色品牌主题样式
- 完整文档页面(指南、API、更新日志、演示、社区)
- SEO 配置(sitemap、robots、meta 标签)
2026-10-02 23:37:25 +08:00

74 lines
3.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 三层隔离架构
MengStack 采用三层隔离架构,从外到内依次为:**Kernel → App → Modules**。
## 架构总览
```
┌─────────────────────────────────────────────────┐
│ Modules │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Auth │ │ Org │ │ User │ ... │
│ └────┬────┘ └────┬────┘ └────┬────┘ │
│ │ │ │ │
├───────┼───────────┼───────────┼──────────────────┤
│ └───────────┼───────────┘ │
│ │ │
│ App │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Database │ │ Cache │ │Middleware│ ... │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ │ │ │ │
├───────┼────────────┼────────────┼────────────────┤
│ └────────────┼────────────┘ │
│ │ │
│ Kernel │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Response │ │ Error │ │ Paginator│ ... │
│ └──────────┘ └──────────┘ └──────────┘ │
│ │
└──────────────────────────────────────────────────┘
```
## 依赖规则
**只能从上到下依赖,禁止反向依赖。**
| 层 | 可以依赖 | 禁止依赖 |
|---|---|---|
| Modules | App, Kernel | 其他 Modules |
| App | Kernel | Modules |
| Kernel | 标准库 | App, Modules |
## Kernel 层
零业务语义的基础组件。判断标准:**这个组件是否知道"用户"、"租户"、"订单"等业务概念?** 如果知道,它就不属于 Kernel。
Kernel 提供的都是纯粹的技术工具:
- 统一响应格式
- 通用错误类型
- 分页工具
- 字符串/时间工具
## App 层
应用基础设施,负责连接外部世界:
- 数据库连接(GORM + PostgreSQL)
- 缓存连接(Redis)
- 全局中间件
- 健康检查
App 层可以引用 Kernel,但不知道任何业务模块的存在。
## Modules 层
业务模块,每个模块独立自包含。模块间不能直接引用,只能通过接口或事件通信。
## 为什么这样分?
1. **Kernel 可复用**:换一个项目,Kernel 可以直接搬过去
2. **App 可替换**:换数据库、换缓存,只改 App 层
3. **Modules 可删除**:删除一个业务模块,不影响其他模块和基础设施