上 我服了!SpringBoot升級后這服務我一個星期都沒跑起來!

最近由于各方面的原因在準備升級 Spring Cloud 和 Spring Boot , 經過一系列前置的調研和分析,決定把Spring Boot 相關版本從 2.1.6 升級到 2.7.5,Spring Cloud 相關版本從 Greenwich.SR1 升級為 2021.0.4 。
升級包含基礎的業務服務代碼的升級改造適配,還有就是中間件一堆代碼的改造 , 上周經歷了一周的修改,用來測試的服務仍然還沒有跑起來,所以這篇文章我會記錄下來這升級過程中的一些問題,由于革命仍未成功,所以這是上篇 。
1. hibernate-validator包下的類報錯在 Spring Boot 2.3版本之后,spring-boot-starter-web 中沒有依賴 hibernate-validator 。
解決方案:使用新的依賴 。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency>2. ApplicationEnvironmentPreparedEvent類改變Spring Boot 2.4版本之后,ApplicationEnvironmentPreparedEvent 構造函數新增了ConfigurableBootstrapContext,業務代碼還好,應該都用不上這個類,中間件代碼使用到的地方都需要修改 。
解決方案:修改代碼 。
public ApplicationEnvironmentPreparedEvent(ConfigurableBootstrapContext bootstrapContext,SpringApplication application, String[] args, ConfigurableEnvironment environment) {super(application, args);this.bootstrapContext = bootstrapContext;this.environment = environment; }

上 我服了!SpringBoot升級后這服務我一個星期都沒跑起來!

文章插圖
3. junit依賴升級升級后的junit版本默認是junit5(我沒有去確認是哪個版本發生了變化),升級之后包名發生了改變,所有的測試用例都需要修改 。
import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.Test;另外發現Assert類不存在了,可以改用Assertions
Assertions.assertNotNull(result);解決方案:修改代碼!
4. Spring Cloud兼容問題由于測試過程中先升級的 Spring Boot,發現 Spring Cloud 使用到的低版本代碼不兼容,升級到文章開頭說的版本之后問題解決 。
比如下面的 spring-cloud-context 啟動時候報錯 。
上 我服了!SpringBoot升級后這服務我一個星期都沒跑起來!

文章插圖
5. SpringApplicationRunListener類改變和第二個問題比較類似,SpringApplicationRunListener 中這兩個方法新增了 ConfigurableBootstrapContext,對應實現類都需要修改,這個應該無論在業務還是中間件代碼中都應該有大量的使用 。
解決方案:修改代碼!
default void starting(ConfigurableBootstrapContext bootstrapContext) {}default void environmentPrepared(ConfigurableBootstrapContext bootstrapContext,ConfigurableEnvironment environment) {}6. ServerProperties變更spring-boot-autoconfigure 包下 ServerProperties 中的內部類 Tomcat 屬性變更,獲取最大線程數方法發生改變 。
原寫法:serverProperties.getTomcat().getMaxThreads()
解決方案:serverProperties.getTomcat().getThreads().getMax()
7. spring-cloud-openfeign中移除ribbon和hystrix依賴Commit地址:https://github.com/spring-cloud/spring-cloud-openfeign/commit/8a08e1ec4b4f0d40193a4ea9c03afdeffe3110a6
這個提交把 spring-cloud-openfeign 里面關于 ribbon 和 hystrix 的依賴相關代碼全部刪除了,這個 commit 我找了一遍 issue 和 PR,都沒有發現相關說明,大佬直接刪的 , 具體原因不清楚為什么直接全刪干凈了 。
比如我的啟動報錯:
Caused by: java.lang.ClassNotFoundException: org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient解決方案:手動引入新的依賴 。
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId><version>2.2.10.RELEASE</version> </dependency>8. bootstrap.properties/yml 配置文件不生效根據 Spring Cloud 配置方式 , 發現很多業務的本地配置配置在 bootstrap.properties中 , 新版本默認會不生效 。
老版本中 spring.cloud.bootstrap.enabled 默認為 true 。
上 我服了!SpringBoot升級后這服務我一個星期都沒跑起來!

文章插圖
新版本改過之后默認是false了,導致一堆配置不生效 。
上 我服了!SpringBoot升級后這服務我一個星期都沒跑起來!

文章插圖

上 我服了!SpringBoot升級后這服務我一個星期都沒跑起來!

文章插圖
解決方案:手動設置spring.cloud.bootstrap.enabled=true
9. spring-cloud-netflix-eureka-client中移除ribbon和hystrix依賴和第七個問題差不多,

推薦閱讀