我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)
创始人
2024-04-11 15:20:47

14. DiscoveryEnabledServer Not Found

主要问题还是 eureka 中没有了 ribbon 相关的依赖。

Caused by: java.lang.NoClassDefFoundError: com/netflix/niws/loadbalancer/DiscoveryEnabledServerat java.lang.Class.getDeclaredMethods0(Native Method) ~[?:?]at java.lang.Class.privateGetDeclaredMethods(Class.java:3167) ~[?:?]at java.lang.Class.getDeclaredMethods(Class.java:2310) ~[?:?]at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:467) ~[spring-core-5.3.23.jar:5.3.23]at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321) ~[spring-core-5.3.23.jar:5.3.23]
复制代码

解决方案:手动引入相关依赖包。

com.netflix.ribbonribbon-loadbalancer2.7.18

com.netflix.ribbonribbon-eureka2.7.18

org.springframework.cloudspring-cloud-starter-netflix-ribbon2.2.10.RELEASE

复制代码

15. 中间件循环依赖

依然是循环依赖报错,之前没注意看代码,简单的设置了一下为延迟初始化,仔细一看发现代码这样写的,你细品。

然后启动报错:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cachesEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/cache/CachesEndpointAutoConfiguration.class]: Unsatisfied dependency expressed through method 'cachesEndpoint' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Requested bean is currently in creation: Is there an unresolvable circular reference?
复制代码

16. CacheMetricsRegistrarConfiguration 报错

由于在解决 15 的问题一开始是设置为延迟初始化,然后启动发现仍然报错。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsRegistrarConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsRegistrarConfiguration]: Constructor threw exception; nested exception is java.lang.StackOverflowError
复制代码

解决方案:去掉 Autowired 注入,15和16的问题全部解决。

17. kafka-clients 版本和 spring-kafka 不兼容

升级后默认spring-kafka是2.8.10版本,KafkaTemplate 报错找不到类,原因在于本地kafka-clients使用的是 2.3.0 版本。

Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.kafka.core.KafkaTemplate] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@9e89d68]
Caused by: java.lang.NoClassDefFoundError: org/apache/kafka/clients/consumer/ConsumerGroupMetadata
复制代码

解决方案:kafka-clients升级到兼容版本 3.0.2 ,这个版本是 spring-cloud-dependency 中依赖的版本。

18. swagger启动报错

这个报错是因为新版本 Spring Boot 将 Spring MVC 默认路径匹配策略由AntPathMatcher改成了PathPatternParser,这个报错在我这里是WARN,而且非常隐蔽,需要仔细查找。

[WARN] [2022.11.08 16:17:39.963] [10.135.0.95] [] [main] [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext()] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
复制代码

解决方案:配置成原来的AntPathMatcher,添加配置spring.mvc.pathmatch.matching-strategy= ANT_PATH_MATCHER

这个报错信息是一行 WARN 日志,非常难找,另外原因是根据网上信息搜索定位到的,这个报错信息我真的服了。

19. spring-session依赖报错

启动报错信息:

n attempt was made to call a method that does not exist. The attempt was made from the following location:org.springframework.boot.autoconfigure.session.SessionAutoConfiguration$ServletSessionConfiguration.cookieSerializer(SessionAutoConfiguration.java:109)The following method did not exist:'void org.springframework.session.web.http.DefaultCookieSerializer.setSameSite(java.lang.String)'The calling method's class, org.springframework.boot.autoconfigure.session.SessionAutoConfiguration$ServletSessionConfiguration, was loaded from the following location:jar:file:/Users/user/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.7.5/spring-boot-autoconfigure-2.7.5.jar!/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration$ServletSessionConfiguration.classThe called method's class, org.springframework.session.web.http.DefaultCookieSerializer, is available from the following locations:jar:file:/Users/user/.m2/repository/org/springframework/session/spring-session/1.3.5.RELEASE/spring-session-1.3.5.RELEASE.jar!/org/springframework/session/web/http/DefaultCookieSerializer.class
复制代码

spring-session使用的是1.3.5.RELEASE,但是打开 Maven 仓库一看,这居然是最新版本?而且还是 2019 年的版本?

其实并非如此,查找 Github 代码后发现是代码做了模块化拆分,新版本应该引入spring-session-core

org.springframework.sessionspring-session-core2.7.0

复制代码

20. spring-security版本兼容问题

在看到 SessionAutoConfiguration里面代码同时发现spring-security相关依赖代码发生了改变。

解决方案:引入最新版本spring-security-web

org.springframework.securityspring-security-web5.7.4

复制代码

21. RibbonLoadBalancerClient启动报错

报错信息:

org.springframework.retry.RetryException: Could not recover; nested exception is java.lang.AbstractMethodError: Receiver class org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient does not define or inherit an implementation of the resolved method abstract choose(Ljava/lang/String;Lorg/springframework/cloud/client/loadbalancer/Request;)Lorg/springframework/cloud/client/ServiceInstance; of interface org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser.
复制代码

原因在于位于spring-cloud-commons里面的ServiceInstanceChooser#choose方法发生了改变。

而我们由于为了继续使用spring-cloud-netflix-ribbon包,引入的只能是更新到2021年的最新版本2.2.10.RELEASE,这个包最后更新时间是 2021年11月份,所以这里面实现的仍然是老的choose方法。

解决方案:使用同 package 名方式自己重写该类,choose 方法的逻辑其实是和原来传参 object 方法一样的,或者自己把包拉下来改代码重新打包。

22. MongoDB报错

spring-boot-autoconfigure新版本下MongoClientFactory构造函数发生改变,以前的写法发生编译错误。

以前的这种写法传参是MongoProperties

return new MongoClientFactory(mongoProperties).createMongoClient(mongoClientOptions());
复制代码

现在的写法:

MongoClientSettingsBuilderCustomizer customizer = new MongoPropertiesClientSettingsBuilderCustomizer(mongoProperties, environment);
return new MongoClientFactory(Lists.newArrayList(customizer)).createMongoClient(mongoClientOptions());
复制代码

另外一个问题是原来的createMongoClient传参是 MongoClientOptions,现在是 MongoClientSettings。

原来使用heartbeatFrequencyheartbeatConnectTimeout等等一些写法也不一样了,示意一下现在的写法:

MongoClientSettings.builder().applyToServerSettings(builder -> builder.heartbeatFrequency(8000, TimeUnit.MILLISECONDS)).applyToConnectionPoolSettings(builder -> builder.maxConnectionIdleTime(30000,TimeUnit.MILLISECONDS)).applyToSocketSettings(builder -> builder.connectTimeout(30000,TimeUnit.MILLISECONDS)).build();
复制代码

另外,如果使用到了 morphia 的话,这个改动就更大了,基本老代码没法用了,尝试了一下,改不动,暂时放弃了。

总结

事情基本到这里就暂时告一段落了,有一些老的代码改动太大,基本要废弃重写了,暂时搁置吧。

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...