一、版本说明

升级前的版本:

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 提供了一种在启动时分析应用程序环境和打印诊断信息的方法,还可以在运行时为您临时迁移属性。要启用该功能,请将以下依赖项添加到您的项目中: 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-properties-migrator</artifactId>
    <scope>runtime</scope>
</dependency>

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

2022-08-09 20:00:40.133|my-test||ERROR|main|org.springframework.boot.context.properties.migrator.PropertiesMigrationListener|100|
The use of configuration keys that are no longer supported was found in the environment:

Property source 'bootstrapProperties-my-test-common.yaml,IMS_GROUP':
	Key: spring.flyway.ignore-missing-migrations
		Reason: Replacement key 'spring.flyway.ignore-migration-patterns' uses an incompatible target type


Please refer to the release notes or reference guide for potential alternatives.

2022-08-09 20:00:40.146|my-test||INFO|main|org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener|136|

 注意:


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


三、 升级的问题



问题1 :Hystrix依赖包找不到

springsecurity jwt怎么更新_spring

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

 问题2:No Feign Client for loadBalancing defined.

springsecurity jwt怎么更新_spring boot_02

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

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

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

springsecurity jwt怎么更新_java_03

解决:两种方法

方法一:在配置文件增加如下配置项

spring:
   main:
     allow-circular-references: true



方法二:修改pagehelper版本为1.4.1

    <dependencies>
        <!--MyBatis分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.1</version><!--必须是这个版本,其他版本有相互依赖的类,例如A类依赖B类,B类又引用了A类 -->
        </dependency>
    </dependencies>

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

springsecurity jwt怎么更新_java_04

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

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

spring: cloud: nacos: discovery: service: testgrpc

问题5:日志输出applicationName_IS_UNDEFINED

springsecurity jwt怎么更新_spring boot_05

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

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

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

spring:
  application:
    name: mytest
  cloud:
    nacos:
      discovery:
        server-addr: ${NACOS_ADDR:127.0.0.1}:${NACOS_PORT:8848}
        namespace: ${NACOS_NAMESPACE:test}
        group: ${NACOS_GROUP:TEST_GROUP}
        service: ${spring.application.name}

springsecurity jwt怎么更新_java_06

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

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:420)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
	at com.zhdgps.ims.GnssApplication.main(GnssApplication.java:32)
Caused by: java.lang.NullPointerException: null
	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112)
	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109)
	at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37)
	at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
	at java.base/java.util.TimSort.sort(TimSort.java:220)
	at java.base/java.util.Arrays.sort(Arrays.java:1441)
	at com.google.common.collect.Ordering.sortedCopy(Ordering.java:855)
	at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57)
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138)
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135)
	at com.google.common.collect.Iterators$7.transform(Iterators.java:750)
	at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47)
	at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47)
	at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52)
	at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50)
	at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249)
	at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209)
	at com.google.common.collect.FluentIterable.toList(FluentIterable.java:614)
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111)
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96)
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167)
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
	... 14 common frames omitted

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

第一步:升级Swagger 版本到3.0

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
    </dependency>

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

<dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

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

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

第三步: 增加swagger配置

@EnableOpenApi
@Configuration
public class SpringFoxSwaggerConfig {

    /**
     * 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题
     **/
    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }
    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }
}

注意地址后缀变化:

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类

springsecurity jwt怎么更新_spring_07

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

springsecurity jwt怎么更新_spring boot_08

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

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.mail.properties' to java.util.Map<java.lang.String, java.lang.String>:

    Property: spring.mail.properties
    Value: ""
    Origin: "spring.mail.properties" from property source "bootstrapProperties-my-test-notify-service.yaml,IMS_GROUP"
    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>]

Action:

Update your application's configuration

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

spring: 
  mail:
    protocol: smtp
    host: smtp.exmail.qq.com
    port: 25
    username: root
    password: 123456
    default-encoding: utf-8
    properties:
    debug: true