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

<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依赖包找不到

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

 问题2:No Feign Client for loadBalancing defined.

 解决:原因是移除了对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.

解决:两种方法

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

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 

 解决:原因是使用了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

解决:原因是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}

问题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类

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

 问题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

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud 2021.0.1是Spring Cloud框架的一个版本,它引入了许多新特性和改进。 OAuth2是一个开放的授权协议,用于保护Web资源。在使用Spring Cloud开发微服务架构时,可以使用OAuth2来实现身份验证和授权。 Spring Cloud 2021.0.1中的OAuth2模块提供了一套简单而强大的工具,用于在微服务架构中实现安全认证和资源授权。它提供了一种方式来管理和保护服务之间的通信,防止未经授权的访问。 使用OAuth2,我们可以定义资源服务器和授权服务器。资源服务器提供受保护的资源,而授权服务器负责验证用户的身份并授予访问令牌。这种方式使得微服务架构中的服务可以相互信任,并确保只有经过授权的服务可以访问受保护的资源。 Spring Cloud 2021.0.1的OAuth2模块还支持多种授权模式,包括密码模式、授权码模式、客户端模式和刷新令牌模式。这些模式适用于不同的场景和需求,可以根据实际情况选择合适的授权方式。 此外,Spring Cloud 2021.0.1还提供了一套可扩展的API,用于自定义身份验证和授权逻辑。我们可以根据自己的需求来实现自定义的授权策略,并轻松集成到Spring Cloud中。 综上所述,Spring Cloud 2021.0.1中的OAuth2模块为微服务架构提供了强大的安全认证和资源授权功能。它使得服务之间的通信更加可靠和安全,为微服务架构的开发和部署提供了便利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值