Files
full/wiki/architecture.md

39 lines
1.8 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.
# 系统架构
## 模块边界
仓库包含 20 个 Go modulemodule 下 19 个业务模块可独立运行all 聚合其中 15 个 base、ec、finance 服务social 三个模块暂未聚合。
典型模块目录职责:
| 目录 | 职责 |
| --- | --- |
| cmd/main | 进程入口、配置和生命周期 |
| internal/config | 模块配置 |
| internal/impl | DB、Redis、etcd、内存缓存 |
| internal/logic | 业务逻辑 |
| internal/server | 服务构造与注册 |
| service | 对 all 公开的 Expose API |
| pb | protobuf 生成代码 |
| etc | 配置样例 |
独立入口初始化自己的基础设施。聚合调用 Expose 时不重复初始化,而是注入 all 创建的共享对象。
## all 启动流程
1. 校验 --workspace 并读取 etc/<workspace>_<runtime>.yaml。
2. 初始化共享 DB、Redis、etcd 和内存缓存。
3. 创建独立端口的 gRPC server以及承载 grpc-gateway、动态 RPC 和 Gin 的 HTTP server。
4. 按 Services 或 BSM_SERVICES 调用模块 Expose。
5. 分别监听 `Server.GRPC``Server.HTTP` 配置的地址,并响应退出信号。
HTTP 请求先进入 grpc-gatewaygateway 返回 404 时回退到 Gin。Gin 提供 `/rpc/{package}/{service}/{method}` 动态 JSON-to-protobuf unary 调用,以及 `/rest/{module}/...` 原生 REST 路由。动态调用通过本机 gRPC 端口进入完整 gRPC 处理链。
## 依赖注入
ExposeOptions.Dependencies 可接收 RedisClient、etcd Client、gorm DB 和 Cache。参数非 nil 时覆盖模块 internal/impl 对象。address、market、order、mgt 的业务数据库访问已统一使用 internal/impl.DBService。
## 生命周期
统一 HTTP server 设置请求头读取超时、空闲超时和最大请求头。关闭时先执行 gRPC GracefulStop超过 context 截止时间后强制 Stop再关闭 HTTP server。