Loading

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

上一次的升级过程中差不多已经跑起来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. 中间件循环依赖

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

然后启动报错:

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.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 年的版本?

其实并非如此,查找 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相关依赖代码发生了改变。

解决方案:引入最新版本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方法发生了改变。

而我们由于为了继续使用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 的话,这个改动就更大了,基本老代码没法用了,尝试了一下,改不动,暂时放弃了。

总结

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

posted @ 2022-11-22 18:09  艾小仙  阅读(321)  评论(0编辑  收藏  举报