系统地解释了:
-
✅ Spring Boot 与 Spring Cloud 的版本兼容关系
-
✅ 官方地址与版本对照表的解读
-
✅ 如何在 Maven 中使用和管理 BOM(Bill of Materials)
-
✅ 为什么要用 <dependencyManagement>
-
✅ 实战中最佳的版本管理做法
🔧 Spring Boot 与 Spring Cloud 的版本兼容策略与 Maven BOM 管理全解析
一、📌 什么是版本兼容关系?
在使用 Spring Boot 搭配 Spring Cloud 构建微服务时,二者版本必须严格匹配,否则会出现:
-
依赖冲突(NoSuchMethodError、ClassNotFound 等)
-
Bean 无法注入
-
注册中心或配置中心连接失败
二、🌐 官方查看地址与版本对照表
📍 查看方式:
访问 https://spring.io/projects/spring-cloud
滚动到 “Release Train” 区域,即可看到如下对照关系:
|
Spring Cloud 版本(Release Train) |
对应 Spring Boot 版本 |
代号(别名) |
|---|---|---|
|
2025.0.x |
3.5.x |
Northfields |
|
2024.0.x |
3.4.x |
Moorgate |
|
2023.0.x |
3.3.x, 3.2.x |
✅ Leyton(推荐) |
|
2022.0.x |
3.1.x, 3.0.x |
Kilburn |
|
2021.0.x |
2.7.x, 2.6.x |
Jubilee |
|
2020.0.x |
2.5.x, 2.4.x |
Ilford |
|
Hoxton / Greenwich |
2.3.x ~ 2.1.x |
老项目 |
三、📦 什么是 BOM(Bill of Materials)?
BOM 是一种特殊的 POM 文件,用于集中管理依赖的版本号。
Spring Cloud 官方提供的 BOM 是:
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
四、🛠 Maven 中如何使用 BOM?
通过 <dependencyManagement> + import 引入:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.1</version> <!-- 与Spring Boot版本对应 -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后你就可以这样使用组件了(无需手动写版本):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
五、✅ 为什么使用 dependencyManagement 管理 BOM?
|
好处 |
说明 |
|---|---|
|
✅ 避免版本冲突 |
所有 cloud 组件版本自动对齐 |
|
✅ 减少配置冗余 |
子模块不再重复写 <version> |
|
✅ 易于统一升级 |
改一处即可升级整套 cloud 依赖 |
|
✅ Spring 官方推荐 |
Spring Cloud 设计就基于 BOM 管理 |
六、💡 实战推荐组合
|
Spring Boot |
Spring Cloud |
推荐版本号 |
|---|---|---|
|
3.3.x |
2023.0.x |
2023.0.1 ✅ |
|
2.7.x |
2021.0.x |
2021.0.7 |
|
3.1.x |
2022.0.x |
2022.0.3 |
七、🔍 BOM 是如何工作的?
以 spring-cloud-dependencies:2023.0.1 为例,它的 pom.xml 中有:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>4.0.4</version>
</dependency>
...
</dependencies>
</dependencyManagement>
这意味着:你在自己的项目中引用了 openfeign 而没有版本号时,会自动使用 4.0.4。
八、🚀 小结与最佳实践
-
✅ 使用 Spring Cloud 官网 Release Train 选择版本
-
✅ 在 dependencyManagement 中引入对应 BOM
-
✅ 子依赖不再写版本
-
✅ 保持 Spring Boot 与 Spring Cloud 一对一版本匹配
📎 附:快速对照模板(Spring Boot 3.3)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
浙公网安备 33010602011771号