跨境电商全栈微服务系统设计
概述
基于前面学习的 Spring 全家桶 + 22 种中间件,设计一个跨境电商全栈微服务系统,作为整个学习系列的实战完结项目。
一、需求分析
1.1 核心业务
text
跨境电商平台(假设面向东南亚市场)
用户端(App/Web):
├── 用户注册/登录(邮箱/手机/第三方)
├── 商品浏览/搜索(多语言/多币种)
├── 购物车管理
├── 下单支付(COD / 信用卡 / 本地支付)
├── 订单跟踪
└── 售后/退款
运营端(Admin):
├── 商品管理(上架/发布/库存)
├── 订单管理(审核/发货/退款)
├── 用户管理(风控/禁言)
├── 促销管理(优惠券/秒杀)
└── 数据分析(销售报表/用户分析)
后台服务:
├── 多语言(i18n 6 种语言)
├── 多币种(IDR / THB / VND / MYR / PHP / SGD)
├── 跨境物流(国内仓 + 海外仓)
├── 支付网关(Stripe / Xendit / Midtrans)
└── 合规/税务(各国税率不同)1.2 非功能需求
| 指标 | 目标 |
|---|---|
| 日活用户 | 500 万 |
| 日订单量 | 50 万 |
| 可用性 | 99.99% |
| P99 延迟 | < 500ms |
| 支付成功率 | > 99.5% |
| 多语言 | 6 种 |
| 部署区域 | 3 个(新加坡、雅加达、曼谷) |
二、架构设计
2.1 整体架构
┌─────────────────────────────────────────────────────┐
│ CDN / CloudFront │
└──────────────────────┬──────────────────────────────┘
│
┌──────────────────────▼──────────────────────────────┐
│ API Gateway (Spring Cloud Gateway) │
│ 鉴权 / 限流 / 路由 / 多语言 / 日志 │
└──────┬──────┬──────┬──────┬──────┬──────┬──────────┘
│ │ │ │ │ │
┌──────▼┐ ┌──▼───┐ ┌▼────┐ ┌▼───┐ ┌▼───┐ ┌▼─────┐
│ 用户 │ │ 商品 │ │ 订单 │ │ 支付 │ │ 物流 │ │ 搜索/│
│ 服务 │ │ 服务 │ │ 服务 │ │ 服务 │ │ 服务 │ │推荐 │
└───┬───┘ └──┬───┘ └──┬─┘ └──┬─┘ └──┬─┘ └──┬───┘
│ │ │ │ │ │
┌───▼───┐ ┌──▼───┐ ┌─▼───┐ ┌▼───┐ ┌▼───┐ ┌▼───┐
│ MySQL │ │MySQL │ │MySQL│ │MySQL│ │MySQL│ │ ES │
│ (分库) │ │(分库) │ │(分库)│ │ │ │ │ │ │
└───────┘ └──────┘ └─────┘ └────┘ └────┘ └────┘
基础设施层
┌─────────────────────────────────────────┐
│ Nacos(注册 + 配置) / Sentinel / Seata │
│ Kafka / RocketMQ / Redis Cluster │
│ SkyWalking / Prometheus / Grafana │
│ K8s / Istio / Docker Registry │
└─────────────────────────────────────────┘2.2 核心技术选型
| 模块 | 技术选型 | 说明 |
|---|---|---|
| 开发框架 | Spring Boot 3.2 + Spring Cloud 2023.0 | 最新稳定版 |
| 注册/配置 | Nacos 2.3 | 配置中心 + 注册中心 |
| 网关 | Spring Cloud Gateway | 响应式网关 |
| RPC | OpenFeign + LoadBalancer | 声明式调用 |
| 限流降级 | Sentinel 1.8 | 流量治理 |
| 分布式事务 | Seata AT + TCC | 最终一致性 |
| 消息队列 | RocketMQ 5.x | 异步解耦 |
| 数据库 | MySQL 8.0 + ShardingSphere | 分库分表 |
| 缓存 | Redis Cluster | 热点数据 |
| 搜索引擎 | Elasticsearch 8.x | 商品搜索 |
| 链路追踪 | SkyWalking 9.x | 无侵入 APM |
| 监控 | Prometheus + Grafana | 指标监控 |
| 日志 | ELK (Elasticsearch + Logstash + Kibana) | 日志聚合 |
| 部署 | Docker + K8s + Istio | 容器化/服务网格 |
三、服务拆分
3.1 服务列表
| 服务 | 职责 | 数据存储 | QPS 预估 |
|---|---|---|---|
user-service | 用户注册/登录/认证 | MySQL + Redis | 5000 |
product-service | 商品 CRUD / 库存 | MySQL + Redis | 8000 |
order-service | 订单创建/状态管理 | MySQL (分库) | 3000 |
payment-service | 支付对接/回调 | MySQL | 2000 |
shipping-service | 物流/发货/追踪 | MySQL | 1000 |
search-service | 商品搜索/推荐 | Elasticsearch | 10000 |
cart-service | 购物车 | Redis | 5000 |
promotion-service | 优惠券/秒杀 | MySQL + Redis | 2000 |
notification-service | 短信/邮件/Push | RocketMQ 消费 | 500 |
admin-service | 运营后台 | MySQL | 500 |
3.2 服务依赖
user-service ←── 所有服务(认证)
product-service ←── order-service, cart-service, search-service
order-service ←── user-service, product-service, payment-service
payment-service ←── order-service
shipping-service ←── order-service
search-service ←── product-service
notification-service ←── order-service, payment-service四、数据设计
4.1 分库分表
yaml
# 订单服务分片(按 user_id 分库,按 order_id 分表)
shardingsphere:
rules:
sharding:
tables:
t_order:
actual-data-nodes: ds$->{0..7}.t_order_$->{0..15}
database-strategy:
standard:
sharding-column: user_id
sharding-algorithm-name: database-inline
table-strategy:
standard:
sharding-column: order_id
sharding-algorithm-name: table-inline4.2 缓存设计
热点数据缓存策略:
商品详情:
Cache-Aside: Caffeine(1min) → Redis(30min) → MySQL
用户 Session:
Redis Hash: user:session:{userId} → 30 天过期
商品库存:
Redis String: stock:{productId} → 预扣库存 + Lua 脚本
首页 Feed:
Redis ZSET: home:feed:page:1 → 每 5 分钟刷新五、部署架构
5.1 K8s 部署
yaml
# K8s 资源规划
apiVersion: apps/v1
kind: Deployment
metadata:
name: order-service
spec:
replicas: 20 # 自动伸缩 10-50
selector:
matchLabels:
app: order-service
template:
spec:
containers:
- name: order-service
image: registry/order-service:latest
resources:
requests:
cpu: 2
memory: 4Gi
limits:
cpu: 4
memory: 8Gi
env:
- name: SPRING_PROFILES_ACTIVE
value: "prod"
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: order-service-hpa
spec:
minReplicas: 10
maxReplicas: 50
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 705.2 成本估算
| 资源 | 规格 | 数量 | 月成本(USD) |
|---|---|---|---|
| 应用 Pod | 4C8G | 100 | $8,000 |
| MySQL | 16C64G | 8 | $6,000 |
| Redis Cluster | 16G | 6 | $2,000 |
| ES | 16C64G | 6 | $4,000 |
| Kafka | 8C32G | 3 | $1,500 |
| Nacos | 4C8G | 3 | $500 |
| K8s 管理 | - | - | $1,000 |
| CDN / 带宽 | - | - | $3,000 |
| 监控/日志 | - | - | $1,000 |
| 合计 | $27,000/月 |
六、关键流程
6.1 下单流程
text
1. 用户点击「下单」
2. Gateway → 鉴权 (JWT) → 路由到 order-service
3. order-service:
a. Sentinel 限流检查(QPS 5000)
b. 创建订单(写分库分表)
c. 发送「锁定库存」事务消息(RocketMQ)
d. product-service 消费 → 扣减 Redis 库存
e. 发送「支付通知」消息
4. 用户支付:
a. payment-service 调三方支付
b. 支付回调 → 更新订单状态
c. 发送「发货」消息到 shipping-service
5. 用户收到发货通知6.2 秒杀流程
text
1. 促销开始前 → Redis 预热库存
2. 用户请求 → Gateway 限流(总 QPS 10000)
3. Sentinel 热点参数限流(按商品 ID)
4. Redis Lua 扣库存(原子操作)
5. 抢到 → 发送 MQ 消息 → 异步创建订单
6. 抢不到 → 返回「已售罄」
7. 支付倒计时 15 分钟 → 超时释放库存七、压测结果
| 场景 | 目标 QPS | 实际 QPS | P99 延迟 | 要点 |
|---|---|---|---|---|
| 浏览商品 | 10000 | 12000 | 80ms | 缓存命中率 95% |
| 搜索 | 8000 | 8500 | 200ms | ES 查询优化 |
| 下单 | 3000 | 2800 | 350ms | 分库分表 + 异步 |
| 支付 | 2000 | 2200 | 500ms | 三方依赖 Mock |
| 秒杀 | 10000 | 9500 | 150ms | Redis Lua 扣库存 |
| 全链路 | 10000 | 8500 | 800ms | Gateway 限流瓶颈 |
八、总结
| 维度 | 说明 |
|---|---|
| 业务 | 跨境电商 6 国 8 大服务模块 |
| 技术 | Spring Cloud + 22 种中间件 |
| 部署 | K8s 自动伸缩 10-50 Pod |
| 存储 | MySQL 8 分片 + Redis 集群 + ES |
| 消息 | RocketMQ 事务消息 + 异步解耦 |
| 可观测 | SkyWalking + Prometheus + ELK |
| 成本 | 月均 $27,000(约 20 万人民币) |
参考链接: