美文网首页
SpringBoot——升级SpringBoot2.7.X相关问

SpringBoot——升级SpringBoot2.7.X相关问

作者: 小波同学 | 来源:发表于2023-03-16 21:07 被阅读0次

    SpringBoot 2.2.5 升级到 2.7.7
    SpringCloudHoxton.SR10升级到2021.0.5
    SpringCloudAlibaba2.2.1.RELEASE升级到2021.0.4.0

    • 1、如果项目中使用了@NotNull、@NotBlank等注解时,可能会报错java: 程序包javax.validation.constraints不存在
      2.2.5在spring-boot-starter中引入的该依赖,在高版本中(具体哪个版本未知)中将其排除了
      引入jakarta.validation-api依赖即可解决
    <!-- spring-boot-dependencies 管理了改以来版本,正常不需要指定版本 -->
     <dependency>
        <groupId>jakarta.validation</groupId>
        <artifactId>jakarta.validation-api</artifactId>
    </dependency>
    
    • 2、加载不到BootStrap.yml文件
      在升级到 2.4版本后,加载方式发生改

    添加依赖

    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
    
    • 3、Feign接口的FallbackFactory类包发生改变
      由原先的feign.hystrix.FallbackFactory改为org.springframework.cloud.openfeign.FallbackFactory

    • 4、The bean ‘xxx.FeignClientSpecification’ could not be registered. A bean with that name has already been defined and overriding is disabled.
      查看依赖包中是否有@EnableFeignClients注解,如果一个项目中有多个该注解会报错

    • 5、nacos注册失败

    ERROR | com.alibaba.nacos.client.Worker | com.alibaba.nacos.common.remote.client | Send request fail, request …
    

    确保部署了对应版本的Nacos服务端,我部署的版本为nacos-2.2.0
    Nacos 配置文件结构发生变化
    修改前:


    修改后:


    • 6、The dependencies of some of the beans in the application context form a cycle

    A类引用了B类,B类引用了A类。

    在Spring 2.6之前会自动类解决循环依赖问题,升级之后只能手动解决

    去除循环依赖的逻辑
    配置文件配置Spring.main.allow-circular-references=true
    主启动类

    public static void main(String[] args) {
        SpringApplication s = new SpringApplication(xx.class);
        s.setAllowCircularReferences(Boolean.TRUE);//加入的参数
        s.run(args);
    }
    

    或者注入添加@Lazy注解

    @Resource
    @Lazy
    private UserService userService;
    
    • 7、No application config found or it’s not a valid config! Please add <dubbo:application name=“…” /> to your spring config.
      Dubbo2会和JDK版本有冲突导致该问题,Dubbo3针对该问题进行了重构
      Dubbo2和Dubbo3版本配置文件不兼容会导致该问题,需要根据官网配置项进行配置
    • 8、Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
      SpringBoot2.6之后将SpringMVC 默认路径匹配策略从AntPathMatcher 更改为PathPatternParser,导致出错
      添加配置
    spring:
      mvc:
        pathmatch:
          matching-strategy: ant_path_matcher
    
    • 9、When allowCredentials is true, allowedOrigins cannot contain the special value “” since that cannot be set on the “Access-Control-Allow-Origin” response header. To allow credentials to a set of origins, list them explicitly or consider using “allowedOriginPatterns” instead.
      Springboot升级到2.4之后 AllowedOrigin不能有'
      ,可以用addAllowedOriginPattern代替。

    如果是gateway报错可以修改配置文件:

    spring:
      cloud:
        gateway:
          default-filters:
            - DedupeResponseHeader=Access-Control-Allow-Origin, RETAIN_UNIQUE
          globalcors:
            cors-configurations:
              '[/**]':
                # 允许携带认证信息
                allowCredentials: true
                # 允许跨域的源(网站域名/ip),设置*为全部1
                # allowedOrigins: "*"
                allowedOriginPatterns: "*"
                # 允许跨域的method, 默认为GET和OPTIONS,设置*为全部
                allowedMethods: "*"
                # 允许跨域请求里的head字段,设置*为全部
                allowedHeaders: "*"
    
    • 10、Gateway报错Unable to find instance for xxx
      高版本移除了Ribbon,需要手动添加·spring-cloud-starter-loadblancer`依赖
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    </dependency>
    

    参考:
    https://blog.csdn.net/qq_40104261/article/details/128674498

    相关文章

      网友评论

          本文标题:SpringBoot——升级SpringBoot2.7.X相关问

          本文链接:https://www.haomeiwen.com/subject/mlpdrdtx.html