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


    14. DiscoveryEnabledServer Not Found

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

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

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

    1. <dependency>
    2. <groupId>com.netflix.ribbon</groupId>
    3. <artifactId>ribbon-loadbalancer</artifactId>
    4. <version>2.7.18</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.netflix.ribbon</groupId>
    8. <artifactId>ribbon-eureka</artifactId>
    9. <version>2.7.18</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.springframework.cloud</groupId>
    13. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    14. <version>2.2.10.RELEASE</version>
    15. </dependency>
    16. 复制代码

    15. 中间件循环依赖

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

    然后启动报错:

    1. 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:
    2. Requested bean is currently in creation: Is there an unresolvable circular reference?
    3. 复制代码

    16. CacheMetricsRegistrarConfiguration 报错

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

    1. 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
    2. 复制代码

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

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

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

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

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

    18. swagger启动报错

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

    1. [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
    2. 复制代码

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

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

    19. spring-session依赖报错

    启动报错信息:

    1. n attempt was made to call a method that does not exist. The attempt was made from the following location:
    2. org.springframework.boot.autoconfigure.session.SessionAutoConfiguration$ServletSessionConfiguration.cookieSerializer(SessionAutoConfiguration.java:109)
    3. The following method did not exist:
    4. 'void org.springframework.session.web.http.DefaultCookieSerializer.setSameSite(java.lang.String)'
    5. The calling method's class, org.springframework.boot.autoconfigure.session.SessionAutoConfiguration$ServletSessionConfiguration, was loaded from the following location:
    6. 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
    7. The called method's class, org.springframework.session.web.http.DefaultCookieSerializer, is available from the following locations:
    8. 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
    9. 复制代码

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

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

    1. <dependency>
    2. <groupId>org.springframework.session</groupId>
    3. <artifactId>spring-session-core</artifactId>
    4. <version>2.7.0</version>
    5. </dependency>
    6. 复制代码

    20. spring-security版本兼容问题

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

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

    1. <dependency>
    2. <groupId>org.springframework.security</groupId>
    3. <artifactId>spring-security-web</artifactId>
    4. <version>5.7.4</version>
    5. </dependency>
    6. 复制代码

    21. RibbonLoadBalancerClient启动报错

    报错信息:

    1. 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.
    2. 复制代码

    原因在于位于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

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

    现在的写法:

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

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

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

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

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

    总结

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

  • 相关阅读:
    centos7安装mysql8.x的注意事项,与5.x版本有许多不同
    Dubbo的SPI机制
    el-input 输入后失去焦点
    【PAT甲级】1015 Reversible Primes
    UGUI学习笔记(十)自制雷达图
    linux内核分析:网络协议栈
    基于80C51单片机的经纬度定位显示装置设计
    TypeScript 基础使用
    Spring Boot 中使用 Poi-tl 渲染数据并生成 Word 文档
    IDEA导入jar包
  • 原文地址:https://blog.csdn.net/BASK2311/article/details/127995110