第 1 章 · 微服务架构与 Spring Cloud 2023 全景
本章目标:理解微服务架构的核心动机与代价;掌握 Spring Cloud 2023 发行版与 Spring Boot 3 的对应关系;读懂组件全景图(注册配置、网关、调用、熔断、消息、事务);对照 shop-spring-demo 单体 MVP,规划拆分为虚构练习项目 svc-spring-demo 多服务骨架;建立 api.example.com 统一入口与 nacos.example.com 基础设施域名的教学心智模型。
学时建议:5~6 小时(含 2 小时架构图绘制与拆分讨论)
前置:完成 spring-boot-web ch01~ch12(尤其 ch07 REST、ch10 缓存异步、ch12 毕业 MVP);了解 HTTP、JSON、Maven 多模块基础。
版本对齐:本模块 BOM 为 Spring Cloud 2023.0.x(Leyton),对应 Spring Boot 3.2.x / 3.3.x。若你在 spring-boot-web 使用了 Boot 3.4.x,进入本模块前请将父 POM 的 Boot 版本降至 3.3.x(或整体升级到 Spring Cloud 2024.0.x 与 Boot 3.4 配套)。勿混用不兼容的 BOM 组合。
1.1 从 shop-spring-demo 单体到微服务
在 spring-boot-web ch12,你已交付单体 shop-spring-demo:用户、商品、订单 API 共用一个进程、一个数据库连接池、一次 java -jar 部署。随着业务增长,团队遇到典型瓶颈:
| 痛点 | 单体表现 | 微服务思路 |
|---|---|---|
| 发布频率 | 改订单逻辑需全量重启 | 订单服务独立发版 |
| 技术选型 | 全站绑定同一 ORM/缓存 | 搜索服务可用 ES,支付可独立栈 |
| 故障隔离 | 商品慢查询拖垮整站 | 商品实例扩容,订单不受影响 |
| 团队协作 | 多人改同一仓库冲突多 | 按服务划分仓库与负责人 |
| 数据边界 | 一张大库、表互相 JOIN | 每服务自有库,通过 API/事件协作 |
┌─────────────────────────────────────────────────────────────┐
│ shop-spring-demo(单体 ch12) │
│ ┌─────────┬─────────┬─────────┬──────────┐ │
│ │ User │ Product │ Order │ Security │ 同一 JVM │
│ └────┬────┴────┬────┴────┬────┴──────────┘ │
│ └─────────┴─────────┘ │
│ MySQL(单库) │
└─────────────────────────────────────────────────────────────┘
│ 拆分(本章规划)
▼
┌─────────────────────────────────────────────────────────────┐
│ svc-spring-demo(微服务练习,ch02~ch07 逐步实现) │
│ user-svc │ product-svc │ order-svc │ gateway-svc │
│ └──────────┬──────────┘ │
│ Nacos(nacos.example.com) │
│ 对外:api.example.com → Gateway │
└─────────────────────────────────────────────────────────────┘
说明:shop-spring-demo、svc-spring-demo、api.example.com、nacos.example.com均为教学虚构,不涉及任何真实公司内网、仓库地址或生产密钥。
1.2 微服务定义与边界原则
微服务:围绕业务能力拆分的、可独立部署的小型服务,通过轻量机制(HTTP/消息)通信,各自拥有数据存储。
1.2.1 拆分维度(以电商为例)
| 候选服务 | 核心职责 | 数据归属 |
|---|---|---|
| user-svc | 注册、登录、资料 | user_db |
| product-svc | 商品 CRUD、上下架、搜索 | product_db |
| order-svc | 下单、状态、我的订单 | order_db |
| gateway-svc | 路由、鉴权、限流 | 无业务库 |
| inventory-svc(选修) | 库存扣减 | inventory_db |
边界原则:
- 高内聚:同一服务内变更原因一致(订单状态机留在 order-svc)。
- 低耦合:避免跨服务数据库 JOIN;用 API 或 领域事件(ch06)交换数据。
- 团队对齐:一个服务最好对应一个「 zwei-pizza team」可维护规模。
- 渐进拆分:不必一次拆完;从 order-svc 或读多写少的 product-svc 切入常见。
1.2.2 微服务不是银弹
| 收益 | 代价 |
|---|---|
| 独立扩展与发布 | 分布式调试、链路追踪复杂 |
| 技术异构 | 运维、监控、CI/CD 套数增加 |
| 故障隔离 | 网络延迟、部分失败、数据一致性难 |
| 组织自治 | 需契约测试、API 版本治理 |
结论:shop-spring-demo 单体在 MVP 阶段完全合理;当「发布耦合」或「扩展维度不同」成为主矛盾时,再引入 svc-spring-demo 微服务练习。
1.3 Spring Cloud 2023 与 Spring Boot 3 基线
Spring Cloud 采用日历化版本(如 2023.0.x,代号 Leyton),与 Spring Boot 3.2+/3.3+ 对齐,统一 Jakarta EE 9+(jakarta.* 包名)。
| 组件 | Spring Cloud 2023 中的实现 | 本章/后续 |
|---|---|---|
| 注册发现 | Spring Cloud Alibaba Nacos 或 Netflix Eureka | ch02 Nacos |
| 配置中心 | Nacos Config | ch02 |
| API 网关 | Spring Cloud Gateway | ch03 |
| 声明式 HTTP | OpenFeign | ch04 |
| 客户端负载均衡 | Spring Cloud LoadBalancer | ch05 |
| 熔断限流 | Resilience4j | ch05 |
| 消息驱动 | Spring Cloud Stream | ch06 |
| 分布式事务 | Seata(Alibaba) | ch07 |
| 链路追踪 | Micrometer Tracing + Zipkin/Jaeger | 进阶章节 |
依赖管理(父 POM 示意,虚构版本号仅教学):
<properties>
<java.version>21</java.version>
<spring-cloud.version>2023.0.3</spring-cloud.version>
<spring-cloud-alibaba.version>2023.0.1.2</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.4 Spring Cloud 2023 组件全景图
客户端 / SPA / 移动端
│
▼
┌───────────────────────────────┐
│ api.example.com │
│ Spring Cloud Gateway (ch03) │
│ 路由 · 过滤器 · 限流 │
└───────────────┬───────────────┘
│
┌─────────────────────┼─────────────────────┐
▼ ▼ ▼
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ user-svc │ │ product-svc │ │ order-svc │
│ :8081 │ │ :8082 │ │ :8083 │
└──────┬──────┘ └──────┬──────┘ └──────┬──────┘
│ │ │
│ OpenFeign + LoadBalancer (ch04/ch05) │
│◄───────────────────┼───────────────────►│
│ │ │
└────────────────────┼────────────────────┘
│
┌──────────────┴──────────────┐
│ nacos.example.com │
│ Nacos 注册 + 配置 (ch02) │
└──────────────┬──────────────┘
│
┌────────────────────┼────────────────────┐
▼ ▼ ▼
user_db product_db order_db
│ │ │
└────────────────────┼────────────────────┘
│
┌──────────────┴──────────────┐
│ RabbitMQ / Kafka (ch06) │
│ 异步事件 · 削峰填谷 │
└──────────────┬──────────────┘
│
┌──────────────┴──────────────┐
│ Seata TC Server (ch07) │
│ AT 模式分布式事务 │
└─────────────────────────────┘
请求典型路径:GET https://api.example.com/api/v1/products → Gateway 按路径转发 product-svc → LoadBalancer 从 Nacos 取健康实例 → 返回 JSON。