一句话总结
Spring Cloud 版本选型核心原则:用新不用旧、用活不用死。推荐组合:Spring Boot 3.x + Spring Cloud 2022.x + Spring Cloud Alibaba 2022.x。组件选型:Nacos(注册+配置)、Gateway(网关)、Feign(远程调用)、Sentinel(熔断限流)、Seata(分布式事务)、SkyWalking(链路追踪)。Netflix 套件(Eureka/Hystrix/Ribbon/Zuul)已进入维护或停止状态,新项目不推荐使用。
初级理解
版本对应关系
| Spring Cloud | Spring Boot | Spring Cloud Alibaba | 状态 |
|---|---|---|---|
| 2023.0.x (Leyton) | 3.2.x | 2023.0.x | 最新 |
| 2022.0.x (Kilburn) | 3.0.x - 3.1.x | 2022.0.x | 推荐 |
| 2021.0.x (Jubilee) | 2.6.x - 2.7.x | 2021.0.x | 维护中 |
| 2020.0.x (Ilford) | 2.4.x - 2.5.x | 2021.1 | 停止维护 |
| Hoxton | 2.2.x - 2.3.x | 2.2.x | 停止维护 |
中级深入
组件选型推荐
| 功能 | 推荐 | 不推荐(已停止/维护) |
|---|---|---|
| 注册中心 | Nacos | Eureka 2.x |
| 配置中心 | Nacos Config | Spring Cloud Config(功能弱) |
| 网关 | Gateway | Zuul 1.x |
| 远程调用 | Feign/OpenFeign | RestTemplate(代码繁琐) |
| 负载均衡 | Spring Cloud LoadBalancer | Ribbon |
| 熔断降级 | Sentinel | Hystrix |
| 分布式事务 | Seata | — |
| 链路追踪 | SkyWalking / Micrometer Tracing | Sleuth(已迁移) |
项目结构最佳实践
# 推荐项目结构
mall-cloud/
├── mall-gateway/ # 网关服务
├── mall-common/ # 公共模块
│ ├── common-core/ # 核心工具类
│ ├── common-web/ # Web 通用配置
│ ├── common-security/ # 安全模块
│ └── common-mybatis/ # 数据库模块
├── mall-modules/ # 业务模块
│ ├── mall-user/ # 用户服务
│ │ ├── user-api/ # Feign 接口(供其他服务调用)
│ │ └── user-service/ # 服务实现
│ ├── mall-order/ # 订单服务
│ └── mall-product/ # 商品服务
└── pom.xml # 父 POM(依赖管理)
# 关键设计
# 1. api 模块:定义 Feign 接口 + DTO,供其他服务依赖
# 2. service 模块:实现业务逻辑,不对外暴露
# 3. common 模块:公共代码,避免重复
高级拓展
常见坑与解决方案
# 坑1:Feign 调用超时
# 原因:默认超时时间太短(1秒)
# 解决:配置 connectTimeout 和 readTimeout
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 10000
# 坑2:Feign 调用时 Token 丢失
# 原因:Feign 新线程,ThreadLocal 丢失
# 解决:使用 RequestInterceptor 传递
@Component
public class FeignAuthInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
String token = RequestContextHolder.getCurrentToken();
template.header("Authorization", "Bearer " + token);
}
}
# 坑3:Nacos 配置不生效
# 原因:bootstrap.yml 优先级问题
# 解决:Spring Cloud 2020.x 后需引入 bootstrap 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
# 坑4:Sentinel 控制台规则不持久化
# 原因:默认规则存储在内存,重启丢失
# 解决:配置规则持久化到 Nacos
spring:
cloud:
sentinel:
datasource:
ds1:
nacos:
server-addr: localhost:8848
data-id: sentinel-rules
group-id: DEFAULT_GROUP
data-type: json
rule-type: flow
实战场景
场景:从零搭建微服务项目
# 技术选型清单
# Spring Boot: 3.1.x
# Spring Cloud: 2022.0.x
# Spring Cloud Alibaba: 2022.0.x
# 注册中心 + 配置中心: Nacos 2.x
# 网关: Gateway
# 远程调用: OpenFeign
# 负载均衡: Spring Cloud LoadBalancer
# 熔断限流: Sentinel
# 分布式事务: Seata(按需)
# 链路追踪: SkyWalking
# 监控: Prometheus + Grafana
# 日志: ELK
# 数据库: MySQL 8.0
# 缓存: Redis 7.x
# 消息队列: RocketMQ 5.x
面试模拟
面试官:Spring Cloud 和 Spring Cloud Alibaba 怎么选?
你:Spring Cloud Alibaba 是 Spring Cloud 的增强实现,组件更丰富(Nacos/Sentinel/Seata),社区更活跃。Netflix 套件已停止维护。新项目推荐 Spring Cloud Alibaba 全家桶。
面试官:微服务项目结构怎么设计?
你:按业务模块拆分,每个模块分 api(Feign 接口+DTO)和 service(业务实现)。公共代码抽到 common 模块。父 POM 统一管理依赖版本。这样其他服务只需依赖 api 模块即可调用。