Cpte-Boot/ARCHITECTURE_OVERVIEW.md

400 lines
15 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# WMS 微服务 K8s 架构总览
## 📐 架构设计
### 整体架构图
```
┌─────────────────────────────────────────────────────────────────┐
│ 用户层 │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Web │ │ App │ │ 第三方 │ │ MOM 系统 │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ 接入层 │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Nginx Ingress Controller │ │
│ │ • 统一入口 • SSL 终止 • 路由转发 │ │
│ └──────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ Kubernetes Cluster │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 服务网格层 │ │
│ │ ┌──────────────────────────────────────────────────┐ │ │
│ │ │ wms-basic-service (基础服务) │ │ │
│ │ │ - 物品管理 • 区域管理 • 库位管理 │ │ │
│ │ │ - 副本数2-10 (HPA 自动伸缩) │ │ │
│ │ └──────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────┐ │ │
│ │ │ wms-inbound-service (入库服务) │ │ │
│ │ │ - ASN 管理 • 收货 • 上架 • MOM 对接 │ │ │
│ │ │ - 副本数2-10 (HPA 自动伸缩) │ │ │
│ │ └──────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────┐ │ │
│ │ │ wms-outbound-service (出库服务) │ │ │
│ │ │ - 拣货管理 • 下架 • 发货 • MOM 对接 │ │ │
│ │ │ - 副本数2-10 (HPA 自动伸缩) │ │ │
│ │ └──────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────┐ │ │
│ │ │ wms-inventory-service(库存服务) │ │ │
│ │ │ - 库存管理 • 盘点 • 流水 • 统计分析 │ │ │
│ │ │ - 副本数2-10 (HPA 自动伸缩) │ │ │
│ │ └──────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────┐ │ │
│ │ │ wms-schedule-service (调度服务) │ │ │
│ │ │ - AGV 调度 • 输送线 • 定时任务 │ │ │
│ │ │ - 副本数2-10 (HPA 自动伸缩) │ │ │
│ │ └──────────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 服务间调用 (OpenFeign) │ │
│ │ │ │
│ │ 入库服务 ──→ 基础服务 (验证物品/库位) │ │
│ │ 入库服务 ──→ 库存服务 (增加库存) │ │
│ │ 入库服务 ──→ 调度服务 (创建上架任务) │ │
│ │ │ │
│ │ 出库服务 ──→ 基础服务 (验证物品/库位) │ │
│ │ 出库服务 ──→ 库存服务 (扣减库存) │ │
│ │ 出库服务 ──→ 调度服务 (创建下架任务) │ │
│ │ │ │
│ │ 调度服务 ──→ 基础服务 (获取库位信息) │ │
│ │ 调度服务 ──→ 库存服务 (获取库存信息) │ │
│ └─────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ 数据层 │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ MySQL Cluster │ │ Redis Cluster │ │
│ │ • 主从复制 │ │ • 哨兵模式 │ │
│ │ • 数据持久化 │ │ • 缓存/会话 │ │
│ └──────────────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
```
---
## 🎯 核心特性
### 1. 微服务拆分
| 服务 | 职责 | 数据库表 | 接口前缀 |
|------|------|---------|---------|
| **基础服务** | 物品、区域、库位管理 | wms_basic_* | /api/wms/basic |
| **入库服务** | ASN、收货、上架 | wms_inbound_* | /api/wms/inbound |
| **出库服务** | 拣货、下架、发货 | wms_outbound_* | /api/wms/outbound |
| **库存服务** | 库存、盘点、流水 | wms_inventory_* | /api/wms/inventory |
| **调度服务** | AGV、输送线调度 | wms_schedule_* | /api/wms/schedule |
### 2. K8s 原生能力
| 功能 | K8s 资源 | 说明 |
|------|---------|------|
| **服务发现** | Service + CoreDNS | 自动 DNS 解析 |
| **负载均衡** | Service | 自动负载均衡 |
| **配置管理** | ConfigMap | 统一配置管理 |
| **密钥管理** | Secret | 敏感信息加密 |
| **健康检查** | Liveness/Readiness Probe | 自动故障恢复 |
| **弹性伸缩** | HPA | 自动扩缩容 |
| **滚动更新** | Deployment | 零停机部署 |
| **服务暴露** | Ingress | 统一入口 |
### 3. 服务间调用OpenFeign
```java
// 声明式服务调用
@FeignClient(
name = "wms-inventory-service",
url = "http://wms-inventory-service:80",
fallbackFactory = InventoryServiceFallbackFactory.class
)
public interface InventoryServiceClient {
@GetMapping("/api/wms/inventory/query")
Result<Map<String, Object>> queryInventory(
@RequestParam("itemId") String itemId,
@RequestParam("pointId") String pointId
);
}
// 使用示例
@Service
public class InboundService {
@Autowired
private InventoryServiceClient inventoryServiceClient;
public void inbound(String itemId, Integer qty) {
// 直接调用,像本地方法一样
Result<Map<String, Object>> result =
inventoryServiceClient.queryInventory(itemId, null);
}
}
```
---
## 📊 部署配置
### 资源配置
```yaml
# 每个服务的资源配置
resources:
requests:
memory: "512Mi"
cpu: "250m"
limits:
memory: "1Gi"
cpu: "500m"
```
### HPA 配置
```yaml
# 自动伸缩配置
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
```
### 健康检查
```yaml
# 存活探针(判断容器是否存活)
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 60
periodSeconds: 10
# 就绪探针(判断是否可接收流量)
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
initialDelaySeconds: 30
periodSeconds: 5
```
---
## 🔄 典型业务流程
### 入库流程
```
1. 入库服务接收入库请求
2. 调用基础服务验证物品和库位
3. 调用库存服务增加库存
4. 调用调度服务创建 AGV 上架任务
5. AGV 执行上架任务
6. 库存服务记录库存流水
```
### 出库流程
```
1. 出库服务接收出库请求
2. 调用基础服务验证物品和库位
3. 调用库存服务预占库存
4. 调用调度服务创建 AGV 下架任务
5. AGV 执行下架任务
6. 库存服务扣减库存并记录流水
```
---
## 🛡️ 高可用设计
### 1. 多副本部署
- 每个服务至少 2 个副本
- Pod 反亲和性,分散到不同节点
- PDBPod Disruption Budget保证最小可用副本数
### 2. 自动故障恢复
- Liveness Probe 检测容器健康
- 容器崩溃自动重启
- 节点故障自动迁移
### 3. 服务降级
- Feign Fallback 提供降级逻辑
- 超时和重试机制
- 熔断保护(可集成 Sentinel
### 4. 数据持久化
- MySQL 主从复制
- Redis 哨兵模式
- 定期备份
---
## 📈 监控体系
### 1. 应用监控
```yaml
# Actuator 端点
management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheus
```
- **健康检查**: /actuator/health
- **应用信息**: /actuator/info
- **性能指标**: /actuator/metrics
- **Prometheus**: /actuator/prometheus
### 2. 日志收集
```
应用日志 → Fluentd → Elasticsearch → Kibana
```
### 3. 链路追踪(可选)
```
应用 → SkyWalking Agent → SkyWalking OAP → UI
```
---
## 🔒 安全设计
### 1. 网络安全
- NetworkPolicy 限制 Pod 间通信
- Ingress TLS 加密
- 服务间 mTLS可选 Istio
### 2. 认证授权
- JWT Token 认证
- RBAC 权限控制
- API 签名验证
### 3. 密钥管理
- Kubernetes Secret 加密存储
- 定期轮换密钥
- 最小权限原则
---
## 🚀 部署流程
### 开发环境
```bash
# 1. 构建镜像
./deploy.sh build
# 2. 部署到 K8s
./deploy.sh k8s
# 3. 查看状态
kubectl get pods -n wms-system
```
### 生产环境
```bash
# 1. 设置版本标签
export IMAGE_TAG=v1.0.0
# 2. 构建并推送
./deploy.sh deploy
# 3. 验证部署
kubectl rollout status deployment/wms-inbound-service -n wms-system
# 4. 监控日志
kubectl logs -f deployment/wms-inbound-service -n wms-system
```
---
## 📝 最佳实践
### 1. 服务设计
- ✅ 单一职责原则
- ✅ 接口幂等性
- ✅ 完善的错误处理
- ✅ 统一的响应格式
### 2. 配置管理
- ✅ 配置与代码分离
- ✅ 敏感信息使用 Secret
- ✅ 环境隔离Namespace
- ✅ 配置版本管理
### 3. 日志规范
- ✅ 结构化日志JSON
- ✅ 包含 TraceID
- ✅ 合理的日志级别
- ✅ 敏感信息脱敏
### 4. 性能优化
- ✅ 连接池配置
- ✅ HTTP 压缩
- ✅ 批量接口
- ✅ 合理使用缓存
---
## 📖 相关文档
- [K8s 部署文档](K8S_DEPLOYMENT.md)
- [Feign 使用指南](cpte-wms-service/FEIGN_USAGE.md)
- [架构设计文档](cpte-wms-service/ARCHITECTURE.md)
---
## 📞 联系方式
- **技术支持**: cpte@163.com
- **API 文档**: http://wms.yourcompany.com/doc.html