分享Spring Boot 3.x微服務(wù)升級歷程
前言
Spring Boot 3.0.0 GA版已經(jīng)發(fā)布,好多人也開始嘗試升級,有人測試升級后,啟動速度確實快了不少,如下為網(wǎng)絡(luò)截圖,于是我也按捺不住的想嘗試下。
歷程
首先就是要把Spring Boot、Spring Cloud 相關(guān)的依賴升一下
Spring Boot:3.0.0
Spring Cloud:2022.0.0-RC2
統(tǒng)一依賴版本管理:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2022.0.0-RC2</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
現(xiàn)在還不能下載Spring 相關(guān)依賴包,需要加入Spring 倉庫。
在你的maven倉庫中加入如下配置,我是加在了pom.xml
中
<repository> <id>netflix-candidates</id> <name>Netflix Candidates</name> <url>https://artifactory-oss.prod.netflix.net/artifactory/maven-oss-candidates</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository>
另外Spring Boot 3.X 開始使用了Java 17,將java版本調(diào)整到>17,為了不必要的麻煩,就選17IDEA選擇17,并在pom.xml文件中指定版本:
<java.version>17</java.version>
到這里我們的common 包是能正常編譯了。
接下來是服務(wù)的配置
同樣調(diào)整Spring Boot、Spring Cloud、Java的版本,同common的配置。
碰到如下的幾個問題:
找不到hystrix的依賴問題
:
升級后找不到hystrix的版本,官網(wǎng)也找不到,這里我顯式指定了版本
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.2.9.RELEASE</version> </dependency>
rabbitmq問題
:
相關(guān)的配置丟失,比如如下圖,這邊進行適當調(diào)整或者直接注釋解決。
TypeVariableImpl丟失問題
:
原來服務(wù)中引入了sun.reflect.generics.reflectiveObjects.TypeVariableImpl
,現(xiàn)在17中已經(jīng)被隱藏無法直接使用,這邊為了能夠先啟動,暫時注釋,后面再想辦法。
Log 異常問題
:
由于之前我們項目中歷史原因,既有用log4j
,也有用logback
,升級后已經(jīng)不行,提示沖突,報錯如下
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.helpers.NOPLoggerFactory loaded from file:/Users/chenjujun/.m2/repository/org/slf4j/slf4j-api/1.7.0/slf4j-api-1.7.0.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.helpers.NOPLoggerFactory
at org.springframework.util.Assert.instanceCheckFailed(Assert.java:713)
at org.springframework.util.Assert.isInstanceOf(Assert.java:632)
意思是,要么移除Logback,要么解決slf4j-api的沖突依賴,這里兩種方式都嘗試了,slf4j-api依賴的地方太多,后面移除了Logback。
要排除依賴一個好辦法:使用Maven Helper
插件
logback依賴:
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.8</version> </dependency>
Apollo問題
:
使用Apollo會提示該錯誤,需要在啟動中加入--add-opens java.base/java.lang=ALL-UNNAMED
Caused by: com.ctrip.framework.apollo.exceptions.ApolloConfigException: Unable to load instance for com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory!
at com.ctrip.framework.apollo.spring.util.SpringInjector.getInstance(SpringInjector.java:40)
at com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer.<init>(ApolloApplicationContextInitializer.java:66)
... 16 more
Caused by: com.ctrip.framework.apollo.exceptions.ApolloConfigException: Unable to initialize Apollo Spring Injector!
at com.ctrip.framework.apollo.spring.util.SpringInjector.getInjector(SpringInjector.java:24)
at com.ctrip.framework.apollo.spring.util.SpringInjector.getInstance(SpringInjector.java:37)
... 17 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @16612a51
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
at com.google.inject.internal.cglib.core.$ReflectUtils$1.run(ReflectUtils.java:52)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at com.google.inject.internal.cglib.core.$ReflectUtils.<clinit>(ReflectUtils.java:42)
通過上述配置調(diào)整后,能編譯成功,但是無法啟動,控制沒有任何日志,初步懷疑還是log依賴問題,由于時間關(guān)系,沒有再繼續(xù),問題留到以后再弄,后面有新進展,會持續(xù)更新該文。
到此這篇關(guān)于Spring Boot 3.x微服務(wù)升級經(jīng)歷的文章就介紹到這了,更多相關(guān)Spring Boot 3.x微服務(wù)升級內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 關(guān)于SpringBoot3.x中spring.factories功能被移除的解決方案
- 淺析SpringBoot微服務(wù)中異步調(diào)用數(shù)據(jù)提交數(shù)據(jù)庫的問題
- 使用Spring?Boot+gRPC構(gòu)建微服務(wù)并部署的案例詳解
- 教你在Spring Boot微服務(wù)中集成gRPC通訊的方法
- SpringBoot+Nacos+Kafka微服務(wù)流編排的簡單實現(xiàn)
- SpringBoot+SpringCloud用戶信息微服務(wù)傳遞實現(xiàn)解析
- SpringBoot集成gRPC微服務(wù)工程搭建實踐的方法
- Spring Boot 快速搭建微服務(wù)框架詳細教程
相關(guān)文章
RocketMQ?Broker實現(xiàn)高可用高并發(fā)的消息中轉(zhuǎn)服務(wù)
RocketMQ消息代理(Broker)是一種高可用、高并發(fā)的消息中轉(zhuǎn)服務(wù),能夠接收并存儲生產(chǎn)者發(fā)送的消息,并將消息發(fā)送給消費者。它具有多種消息存儲模式和消息傳遞模式,支持水平擴展和故障轉(zhuǎn)移等特性,可以為分布式應(yīng)用提供可靠的消息傳遞服務(wù)2023-04-04SpringBoot?整合?ShardingSphere4.1.1實現(xiàn)分庫分表功能
ShardingSphere是一套開源的分布式數(shù)據(jù)庫中間件解決方案組成的生態(tài)圈,它由Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar(計劃中)這3款相互獨立的產(chǎn)品組成,本文給大家介紹SpringBoot?整合?ShardingSphere4.1.1實現(xiàn)分庫分表,感興趣的朋友一起看看吧2023-12-12java統(tǒng)計字符串中指定元素出現(xiàn)次數(shù)方法
這篇文章主要介紹了java統(tǒng)計字符串中指定元素出現(xiàn)次數(shù)方法,需要的朋友可以參考下2015-12-12SpringBoot中利用@Valid和@Validated進行參數(shù)校驗
為了保證數(shù)據(jù)的正確性、完整性,前后端都需要進行數(shù)據(jù)檢驗,作為一名后端開發(fā)工程師,不能僅僅依靠前端來校驗數(shù)據(jù),我們還需要對接口請求的參數(shù)進行后端的校驗,所以本文給大家介紹了SpringBoot中利用@Valid和@Validated進行參數(shù)校驗,需要的朋友可以參考下2024-09-09Java常用HASH算法總結(jié)【經(jīng)典實例】
這篇文章主要介紹了Java常用HASH算法,結(jié)合實例形式總結(jié)分析了Java常用的Hash算法,包括加法hash、旋轉(zhuǎn)hash、FNV算法、RS算法hash、PJW算法、ELF算法、BKDR算法、SDBM算法、DJB算法、DEK算法、AP算法等,需要的朋友可以參考下2017-09-09