上一次的晋级过程中差不多现已跑起来90%了,这周一上班处理完一点小问题,服务现已正常跑起来了,所以再拿着一些其他的服务测试了一下,又发现了一些其他的报错,所以持续。

14. DiscoveryEnabledServer Not Found

首要问题仍是 eureka 中没有了 ribbon 相关的依靠。

Caused by: java.lang.NoClassDefFoundError: com/netflix/niws/loadbalancer/DiscoveryEnabledServer
	at 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]

处理方案:手动引进相关依靠包。

<dependency>
  <groupId>com.netflix.ribbon</groupId>
  <artifactId>ribbon-loadbalancer</artifactId>
  <version>2.7.18</version>
</dependency>
<dependency>
  <groupId>com.netflix.ribbon</groupId>
  <artifactId>ribbon-eureka</artifactId>
  <version>2.7.18</version>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  <version>2.2.10.RELEASE</version>
</dependency>

15. 中间件循环依靠

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

我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)

然后发动报错:

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

我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)

处理方案: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.class
The 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 年的版别?

我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)

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

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-core</artifactId>
    <version>2.7.0</version>
</dependency>

20. spring-security版别兼容问题

在看到 SessionAutoConfiguration里边代码同时发现spring-security相关依靠代码产生了改动。

我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)

处理方案:引进最新版别spring-security-web

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-web</artifactId>
  <version>5.7.4</version>
</dependency>

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办法产生了改动。

我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)

而我们由于为了持续运用spring-cloud-netflix-ribbon包,引进的只能是更新到2021年的最新版别2.2.10.RELEASE,这个包最终更新时间是 2021年11月份,所以这里边实现的仍然是老的choose办法。

我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)

处理方案:运用同 package 名方式自己重写该类,choose 办法的逻辑其实是和本来传参 object 办法相同的,或者自己把包拉下来改代码重新打包。

我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)

22. MongoDB报错

spring-boot-autoconfigure新版别下MongoClientFactory构造函数产生改动,曾经的写法产生编译过错。

我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)

曾经的这种写法传参是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 的话,这个改动就更大了,根本老代码无法用了,尝试了一下,改不动,暂时抛弃了。

总结

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

本文正在参加「金石方案 . 瓜分6万现金大奖」