最近因为各种原因,准备升级SpringCloud和SpringBoot。经过一系列的预研分析,我决定将SpringBoot的相关版本从2.1.6升级到2.7.5。SpringCloud相关版本从Greenwich.SR1升级到2021.0.4。升级包括基础业务服务代码的升级、改造和适配,以及一堆中间件代码的改造。上周修改了一周,测试用的服务还是跑不起来,所以在这篇文章中记录一下,这里是升级过程中出现的一些问题,因为一直没有革命成功,所以这是第一部分。1、hibernate-validator包下的类在SpringBoot2.3之后报错,spring-boot-starter-web不依赖hibernate-validator。解决方案:使用新的依赖项。org.springframework.bootspring-boot-starter-validation2.ApplicationEnvironmentPreparedEvent类改SpringBoot2.4后,ApplicationEnvironmentPreparedEvent构造函数增加了ConfigurableBootstrapContext,业务代码没问题,应该不用这个类,需要修改使用中间件代码的地方。解决方法:修改代码。publicApplicationEnvironmentPreparedEvent(ConfigurableBootstrapContextbootstrapContext,SpringApplicationapplication,String[]args,ConfigurableEnvironmentenvironment){super(application,args);this.bootstrapContext=bootstrapContext;this.environment=环境;junit5(我没确认是哪个版本变了),升级后包名变了,需要修改所有测试用例。导入org.junit.jupiter.api.Assertions;导入org.junit.jupiter.api.Test;另外,发现Assert类不存在,可以改用Assertions。断言.assertNotNull(结果);解决办法:修改代码!4.SpringCloud兼容性由于在测试中第一次升级了SpringBoot,发现SpringCloud使用的低版本代码不兼容。升级到文章开头提到的版本后,问题解决。比如下面的spring-cloud-context启动就报错。5.SpringApplicationRunListener类的变化与第二题类似。SpringApplicationRunListener中的两个方法添加了ConfigurableBootstrapContext,需要修改相应的实现类。这应该广泛用于业务和中间件代码。解决办法:修改代码!默认无效启动(ConfigurableBootstrapContextbootstrapContext){}默认无效环境准备(ConfigurableBootstrapContextbootstrapContext,ConfigurableEnvironment环境){}6。ServerProperties变化spring-boot-autoconfigure包下ServerProperties中的内部类Tomcat属性变化,以及获取最大线程数的方法原写法:serverProperties.getTomcat().getMaxThreads()解决方法:serverProperties.getTomcat()。getThreads().getMax()7.从spring-cloud-openfeign中移除ribbon和hystrixCommit地址:https://github.com/spring-clo...本次commit删除所有与ribbon和hystrix依赖相关的代码弹簧云openfeign。我搜索了本次提交的问题和PR,但没有找到任何相关说明。大佬直接删了,具体原因不清楚,为什么直接删了。比如我的启动报错:Causedby:java.lang.ClassNotFoundException:org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient解决方法:手动引入新的依赖。org.springframework.cloudspring-cloud-starter-netflix-ribbon2.2.10.RELEASE8.bootstrap.properties/yml配置文件不生效根据SpringCloud的配置方法,发现很多业务的本地配置都是在bootstrap.properties中配置的,新版本默认不会生效。在旧版本中,spring.cloud.bootstrap.enabled默认为true。改了新版本后,默认为false,导致一堆配置不生效。解决方法:手动设置spring.cloud.bootstrap.enabled=trueRibbon和hystrix是依赖的,所以客户端默认不会有ribbon这种东西。解决方法:手动引入新的依赖。<依赖>org.springframework.cloudspring-cloud-starter-netflix-ribbon2.2.10.RELEASE10.spring-cloud-starter-alibaba-sentinelversionisincompatiblespring-cloud-starter-alibaba-sentinel使用2.1.3.RELEASE,与新版本存在兼容性问题,导致无法启动,出现循环依赖问题。报错信息:org.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration':Unsatisfieddependencyexpressedthroughmethod'setConfigurers'parameter0;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration”的bean时出错:通过字段“sentinelWebInterceptorOptional”表示的不满足依赖关系;嵌套异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:Errorcreatingbeanwithname'com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration':Requestedbeaniscurrentlyincreation:Isthereanunresolvablecircularreference?解决方案:升级为当前SpringCloud一个版本。com.alibaba.cloudspring-cloud-starter-alibaba-sentinel2021.0.4.011.commons-pool2compatibilityerrorreportingJedisConnectionConfigurationerrorinspring-boot-autoconfigure2.7.5version,原因是我们的一些业务代码依赖指定了commons-pool2的版本。说明:试图调用不存在的方法。尝试是从以下位置进行的:org.springframework.boot.autoconfigure.data.redis.JedisConnectionConfiguration.jedisPoolConfig(JedisConnectionConfiguration.java:114)以下方法不存在:redis.clients.jedis.JedisPoolConfig.setMaxWait(Ljava/time/Duration;)VAction:更正应用程序的类路径,使其包含类org.springframework.boot.autoconfigure.data.redis.JedisConnectionConfiguration和redis.clients.jedis.JedisPoolConfigGit问题的兼容版本:https://github.com/spring-pro...很久以前就修复了。Commons-pool2在2.8.1版本之后丢失了一些方法。解决方法:不指定包版本默认使用springboot的最新依赖,或者手动指定最新版本2.11.1。12、循环依赖报错Spring-boot2.6版本禁止循环依赖。如果有,启动时会报错。信息与第十题相同,唯一不同的是业务代码的报错。解决方法:手动解决代码循环依赖问题或者设置属性spring.main.allow-circular-references=true。13、spring-rabbit版本兼容升级后,由于中间件封装了rabbit的部分功能,去掉了spring-rabbit的自动组装,基本上整个中间件包不可用,大量方法不兼容。解决方案:所有自动组装的逻辑都被2.7.5版本的代码覆盖了。总结看似这些问题只是一两句话,但实际上花了大量时间排错,寻找解决方案,重新筛选所有依赖的包版本,修改包版本,重新打包测试版本,惨无人道的体验中间真的是一两句话说不清楚。我觉得做业务开发其实还是挺好的。革命到此才迈出一小步,要解决的问题还有很多,但必须在本周全部解决!!!