前几天小心翼翼地把 spring-boot-starter 由 2.1.4.RELEASE 升级到 2.2.13.RELEASE,这个项目只有 spring-boot-starter 和第三方框架,因此这个项目升级得非常顺利,代码零改动。
一、踩坑经历
接下来的一个项目,除了 spring-boot-starter,还有 spring-cloud-dependencies, spring-boot-starter 升级到 2.2.13.RELEASE,spring-cloud-dependencies 升级到哪个版本呢?
查看 spring-cloud 官网:https://spring.io/projects/spring-cloud
看到这张图,我非常兴奋,以为找到了升级的密码。
二话不说,将 spring-cloud-dependencies 升级到 Hoxton.SR12,版本高好呀,版本高修复的漏洞多。
事实很快给了我一巴掌。
测试环境项目启动成功,把项目里的大致功能走一遍,发现一个问题,sqlite的数据库加载不了。
org.xerial.sqlite-jdbc 这个框架,是在父项目里进行版本管理的。而在新版本的Spring boot 下,是由 Spring boot 进行版本管理的。
这个好办,移除父项目里的 org.xerial.sqlite-jdbc 版本管理,在使用的项目里直接引用 org.xerial.sqlite-jdbc,maven更新后sqlite运行正常。
本以为这下稳妥了,事实又给了一巴掌。
测试环境跑得好好的,到了线上环境启动失败,即找不到注册中心,也找不到配置中心,这下尴尬了,如何是好?
把 spring-cloud-dependencies 的版本降级到 Hoxton.SR5总可以了吧?
事实证明还是不行!
那就把 spring-cloud-dependencies 的各个版本了解一下。
二、spring-cloud-dependencies是什么
spring-cloud-dependencies是 Spring Cloud 项目的版本管理器,它的作用是统一管理 Spring Cloud 相关的各种库的版本。
当我们使用 Spring Cloud 时,通常需要引入这个依赖,它会确保我们所引入的 Spring Cloud 相关的所有依赖都是兼容的。
Spring Cloud 和 Spring Boot 的版本需要互相匹配。通常,Spring Cloud 会发布和某个 Spring Boot 版本兼容的版本。在 Spring Cloud 中,每个版本的发布都会标注它与哪个版本的 Spring Boot 兼容。
兼容表还是那张兼容表。到底是哪里出现的问题呢?
Spring Cloud 是根据不同的 Spring Boot 版本进行发布的,版本体系按照年份和功能大致分为以下几个系列:
Kilburn (2022.0.x): 对应 Spring Boot 2.6.x 和 2.7.x。
Jubilee (2021.0.x): 对应 Spring Boot 2.4.x 和 2.5.x。
Ilford (2020.0.x): 对应 Spring Boot 2.2.x 和 2.3.x。
Hoxton (2020.0.x): 对应 Spring Boot 2.2.x 和 2.3.x(Hoxton SR5 是开始支持 Spring Boot 2.3.x 的版本)。
因此,Hoxton版本是与Spring Boot 2.2.x 和 2.3.x兼容的,Hoxton在 Spring Cloud 的版本更新中非常重要。
把Hoxton的各个版本研究一下吧!
三、Spring Cloud Hoxton版本体系
1. Hoxton.SR1(2020年2月发布)
支持 Spring Boot 2.2.x。
初步实现了Spring Cloud Hoxton版本,提供了微服务架构所需的一些基础功能,如服务发现、配置管理、消息驱动等。
修复了一些小的 bug 和稳定性问题。
2. Hoxton.SR2(2020年4月发布)
支持 Spring Boot 2.2.x。
修复了 SR1 中的一些问题,特别是在 Spring Cloud Config 和服务发现方面。
增强了微服务的稳定性,优化了 Spring Cloud Gateway 的支持。
3. Hoxton.SR3(2020年6月发布)
支持 Spring Boot 2.2.x。
引入了一些新特性,改善了微服务架构的可扩展性。
增强了对Spring Cloud Config和Spring Cloud Stream的支持。
4. Hoxton.SR4(2020年7月发布)
支持 Spring Boot 2.2.x 和 2.3.x。
在 SR4 中,Spring Cloud 开始支持 Spring Boot 2.3.x 版本,优化了跨版本的兼容性。
进一步优化了微服务架构中的服务注册与发现功能。
5. Hoxton.SR5(2020年9月发布)
支持 Spring Boot 2.3.x。
Hoxton.SR5是支持 Spring Boot 2.3.x 的起始版本,改进了与 Spring Boot 2.3.x 之间的兼容性。
增强了 Spring Cloud Gateway 和 Spring Cloud Config 的功能。
修复了一些服务注册、发现和配置管理中的 bug。
6. Hoxton.SR6(2020年10月发布)
支持 Spring Boot 2.3.x。
进一步优化了与 Spring Boot 2.3.x 的兼容性,并且修复了 SR5 中的部分问题。
增强了微服务的配置管理和服务发现功能,特别是对Eureka的支持。
7. Hoxton.SR7(2020年12月发布)
支持 Spring Boot 2.3.x 和 2.4.x。
对 Spring Boot 2.4.x 的支持正式加入,增强了服务发现、负载均衡等功能。
修复了与 Spring Boot 2.4.x 相关的兼容性问题。
8. Hoxton.SR8(2021年2月发布)
支持 Spring Boot 2.3.x 和 2.4.x。
对 Spring Boot 2.4.x 进行了更多的优化和兼容性修复。
增强了对Spring Cloud Stream和Spring Cloud Gateway的支持,提升了微服务的可扩展性和稳定性。
9. Hoxton.SR9(2021年3月发布)
支持 Spring Boot 2.4.x 和 2.5.x。
继续优化与 Spring Boot 2.5.x 的兼容性,解决了多个 bug。
改进了微服务架构中的健康检查和度量功能。
10. Hoxton.SR10(2021年6月发布)
支持 Spring Boot 2.4.x 和 2.5.x。
修复了 SR9 中的 bug,进一步提高了与 Spring Boot 2.5.x 的兼容性。
增强了 Spring Cloud Sleuth(分布式追踪)和微服务监控的功能。
11. Hoxton.SR11(2021年9月发布)
支持 Spring Boot 2.4.x 和 2.5.x。
继续优化 Spring Cloud Config、Spring Cloud Gateway 和其他基础设施的功能。
修复了多个与服务发现和健康检查相关的 bug。
12. Hoxton.SR12(2022年1月发布)
支持 Spring Boot 2.4.x 和 2.5.x。
Hoxton 系列的最终版本,包含了对 Spring Boot 2.4.x 和 2.5.x 的完全支持,修复了所有已知的 bug,特别是在分布式环境中的配置和服务发现问题。
这个版本是Hoxton的稳定版本,适合生产环境使用。
四、到底选哪个版本
把Hoxton的各个版本捋了一遍,终于知道错在哪里了。
根据我们的 Spring Boot 版本,选择对应的 Spring Cloud 版本:
如果我们使用Spring Boot 2.2.x:建议使用Spring Cloud Hoxton.SR1 到 Hoxton.SR4。
如果我们使用Spring Boot 2.3.x:建议使用Spring Cloud Hoxton.SR4 到 Hoxton.SR6。
如果我们使用Spring Boot 2.4.x:建议使用Spring Cloud Hoxton.SR7 到 Hoxton.SR9。
如果我们使用Spring Boot 2.5.x:建议使用Spring Cloud Hoxton.SR9 到 Hoxton.SR12。
Spring Cloud Hoxton系列主要支持Spring Boot 2.2.x 和 2.3.x,并且从Hoxton.SR5开始支持Spring Boot 2.3.x。
五、最后总结在 Maven 中,我们使用 spring-boot-dependencies 来管理 Spring Boot 版本,使用 spring-cloud-dependencies 来管理 Spring Cloud 版本。通常,我们只需要选择合适的版本,在 pom.xml 中进行配置,其他的 Spring Boot 和 Spring Cloud 相关依赖就会自动使用对应的版本。
赶紧把 Hoxton 调整为 SR1版,放到线上正常,而后又调整到 SR4版本,运行也正常。
是我太粗心了,再次想起那个版本兼容表。
这个表上写的是2.3.x才从 SR5开始的好吧!
Hoxton 2.2.x, 2.3.x (Starting with SR5)
瞧我这理解力!给了答案也没看懂,真是尴尬。
线上项目升级后,应用漏洞从104降到81个,一下子又解决了23漏洞。
需要注意的是,Hoxton 每个版本管理的依赖数量不一样,这个值得留意一下,哪个框架冲突了,说不定就是它的事。
领取专属 10元无门槛券
私享最新 技术干货