• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

spring cloud 2021.0.1升级踩坑记录

武飞扬头像
ttheng88
帮助1

一、版本说明

升级前的版本:

spring boot 2.2.2.RELEASE
spring cloud Hoxton.SR1
spring cloud alibaba 2.2.0.RELEASE

升级后版本:

spring boot 2.6.10
spring cloud 2021.0.1
spring cloud alibaba 2021.0.1.0 

二、属性扫描

升级到新功能版本时,某些属性可能已重命名或删除。Spring Boot 提供了一种在启动时分析应用程序环境和打印诊断信息的方法,还可以在运行时为您临时迁移属性。要启用该功能,请将以下依赖项添加到您的项目中: 

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.boot</groupId>
  3.  
    <artifactId>spring-boot-properties-migrator</artifactId>
  4.  
    <scope>runtime</scope>
  5.  
    </dependency>

增加依赖之后运行项目,控制台会输出变更的属性信息: 

  1.  
    2022-08-09 20:00:40.133|my-test||ERROR|main|org.springframework.boot.context.properties.migrator.PropertiesMigrationListener|100|
  2.  
    The use of configuration keys that are no longer supported was found in the environment:
  3.  
     
  4.  
    Property source 'bootstrapProperties-my-test-common.yaml,IMS_GROUP':
  5.  
    Key: spring.flyway.ignore-missing-migrations
  6.  
    Reason: Replacement key 'spring.flyway.ignore-migration-patterns' uses an incompatible target type
  7.  
     
  8.  
     
  9.  
    Please refer to the release notes or reference guide for potential alternatives.
  10.  
     
  11.  
    2022-08-09 20:00:40.146|my-test||INFO|main|org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener|136|

 注意:

完成迁移后,请确保从项目的依赖项中删除此模块。

三、 升级的问题

问题1 :Hystrix依赖包找不到

学新通

原因: 移除了对Hystrix的支持.(集成alibaba的sentinel作为替代方案

 问题2:No Feign Client for loadBalancing defined.

学新通

 解决:原因是移除了对Ribbon的支持,需切换成 loadbalancer 实现

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.cloud</groupId>
  3.  
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  4.  
    </dependency>

问题3:循环依赖 Relying upon circular references is discouraged and they are prohibited by default.

学新通

解决:两种方法

  1.  
    方法一:在配置文件增加如下配置项
  2.  
     
  3.  
    spring:
  4.  
    main:
  5.  
    allow-circular-references: true
  6.  
     
  7.  
     
  8.  
     
  9.  
    方法二:修改pagehelper版本为1.4.1
  10.  
     
  11.  
    <dependencies>
  12.  
    <!--MyBatis分页插件-->
  13.  
    <dependency>
  14.  
    <groupId>com.github.pagehelper</groupId>
  15.  
    <artifactId>pagehelper-spring-boot-starter</artifactId>
  16.  
    <version>1.4.1</version><!--必须是这个版本,其他版本有相互依赖的类,例如A类依赖B类,B类又引用了A类 -->
  17.  
    </dependency>
  18.  
    </dependencies>
学新通

问题4:java.lang.IllegalArgumentException: Param 'serviceName' is illegal, serviceName is blank 

学新通 解决:原因是使用了bootstrap.yml配置文件,而springcloud2.4版本之后不支持加载bootstrap.yml文件,需增加以下依赖。并且在bootstrap.yml里面增加配置spring.cloud.nacos.discovery.service=xxx

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.cloud</groupId>
  3.  
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
  4.  
    </dependency>
spring:
  cloud:
    nacos:
      discovery:
        service: testgrpc

问题5:日志输出applicationName_IS_UNDEFINED

学新通解决:原因是logback-spring.xml文件中的配置读不到值

<springProperty scope="context" name="applicationName" source="spring.application.name"/>

 在配置文件增加配置spring.cloud.nacos.discovery.service=xxx

  1.  
    spring:
  2.  
    application:
  3.  
    name: mytest
  4.  
    cloud:
  5.  
    nacos:
  6.  
    discovery:
  7.  
    server-addr: ${NACOS_ADDR:127.0.0.1}:${NACOS_PORT:8848}
  8.  
    namespace: ${NACOS_NAMESPACE:test}
  9.  
    group: ${NACOS_GROUP:TEST_GROUP}
  10.  
    service: ${spring.application.name}

学新通

问题6:Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

  1.  
    org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
  2.  
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
  3.  
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
  4.  
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
  5.  
    at java.base/java.lang.Iterable.forEach(Iterable.java:75)
  6.  
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
  7.  
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
  8.  
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
  9.  
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
  10.  
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
  11.  
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745)
  12.  
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:420)
  13.  
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
  14.  
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317)
  15.  
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
  16.  
    at com.zhdgps.ims.GnssApplication.main(GnssApplication.java:32)
  17.  
    Caused by: java.lang.NullPointerException: null
  18.  
    at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112)
  19.  
    at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109)
  20.  
    at com.谷歌.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37)
  21.  
    at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
  22.  
    at java.base/java.util.TimSort.sort(TimSort.java:220)
  23.  
    at java.base/java.util.Arrays.sort(Arrays.java:1441)
  24.  
    at com.谷歌.common.collect.Ordering.sortedCopy(Ordering.java:855)
  25.  
    at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57)
  26.  
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138)
  27.  
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135)
  28.  
    at com.谷歌.common.collect.Iterators$7.transform(Iterators.java:750)
  29.  
    at com.谷歌.common.collect.TransformedIterator.next(TransformedIterator.java:47)
  30.  
    at com.谷歌.common.collect.TransformedIterator.next(TransformedIterator.java:47)
  31.  
    at com.谷歌.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52)
  32.  
    at com.谷歌.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50)
  33.  
    at com.谷歌.common.collect.ImmutableList.copyOf(ImmutableList.java:249)
  34.  
    at com.谷歌.common.collect.ImmutableList.copyOf(ImmutableList.java:209)
  35.  
    at com.谷歌.common.collect.FluentIterable.toList(FluentIterable.java:614)
  36.  
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111)
  37.  
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96)
  38.  
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167)
  39.  
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
  40.  
    ... 14 common frames omitted
学新通

 解决:原因是 对swagger2不兼容,需要升级为swagger3

第一步:升级Swagger 版本到3.0

  1.  
    <dependency>
  2.  
    <groupId>io.springfox</groupId>
  3.  
    <artifactId>springfox-boot-starter</artifactId>
  4.  
    <version>3.0.0</version>
  5.  
    </dependency>
  6.  
     

我们项目用的是swagger-spring-boot-starter,升级到2.0.2.RELEASE,其内部依赖了swagger3

  1.  
    <dependency>
  2.  
    <groupId>com.spring4all</groupId>
  3.  
    <artifactId>swagger-spring-boot-starter</artifactId>
  4.  
    <version>2.0.2.RELEASE</version>
  5.  
    </dependency>

第二步:因为Springfox 使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher ,需增加配置如下:

  1.  
    spring:
  2.  
    mvc:
  3.  
    pathmatch:
  4.  
    matching-strategy: ant_path_matcher

第三步: 增加swagger配置

  1.  
    @EnableOpenApi
  2.  
    @Configuration
  3.  
    public class SpringFoxSwaggerConfig {
  4.  
     
  5.  
    /**
  6.  
    * 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题
  7.  
    **/
  8.  
    @Bean
  9.  
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
  10.  
    List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
  11.  
    Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
  12.  
    allEndpoints.addAll(webEndpoints);
  13.  
    allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
  14.  
    allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
  15.  
    String basePath = webEndpointProperties.getBasePath();
  16.  
    EndpointMapping endpointMapping = new EndpointMapping(basePath);
  17.  
    boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
  18.  
    return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
  19.  
    }
  20.  
    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
  21.  
    return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
  22.  
    }
  23.  
    }
学新通

注意地址后缀变化:

swagger2的访问地址:http://127.0.0.1:8080/gnss/swagger-ui.html 

swagger3的访问地址:http://127.0.0.1:8080/gnss/swagger-ui/index.html

问题7:找不到ResourceProperties类

学新通  解决:ResourceProperties类已经被移除,用WebProperties替换,可以去WebProperties.Resources静态内部类找

学新通

 问题8:Failed to bind properties under 'spring.mail.properties'

  1.  
    ***************************
  2.  
    APPLICATION FAILED TO START
  3.  
    ***************************
  4.  
     
  5.  
    Description:
  6.  
     
  7.  
    Failed to bind properties under 'spring.mail.properties' to java.util.Map<java.lang.String, java.lang.String>:
  8.  
     
  9.  
    Property: spring.mail.properties
  10.  
    Value: ""
  11.  
    Origin: "spring.mail.properties" from property source "bootstrapProperties-my-test-notify-service.yaml,IMS_GROUP"
  12.  
    Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]
  13.  
     
  14.  
    Action:
  15.  
     
  16.  
    Update your application's configuration
学新通

原因:SpringBoot: 2.6.10 对spring某些加载规则更严格,需要适配。配置spring.mail.properties没有赋值,导致转换异常。配置如下:

  1.  
    spring:
  2.  
    mail:
  3.  
    protocol: smtp
  4.  
    host: smtp.exmail.qq.com
  5.  
    port: 25
  6.  
    username: root
  7.  
    password: 123456
  8.  
    default-encoding: utf-8
  9.  
    properties:
  10.  
    debug: true

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaaiif
系列文章
更多 icon
同类精品
更多 icon
继续加载