SpringCloudAlibaba升级手册

目录

[1. 版本对照](#1. 版本对照)

版本现状

SpringCloud与AlibabaCloud对应版本

Springboot与Elasticsearch版本对应

[2. openfeign问题](#2. openfeign问题)

问题

解决方案

[3. Feign请求问题](#3. Feign请求问题)

问题

解决方法

[4. Sentinel循环依赖](#4. Sentinel循环依赖)

问题

解决方案

[5. bootstrap配置文件不生效](#5. bootstrap配置文件不生效)

问题

解决方案

[6. Nacos的连接错误](#6. Nacos的连接错误)

问题

解决方案

[7. 跨域问题](#7. 跨域问题)

问题

解决方案


1. 版本对照

版本现状

|----------------------------------------|---------------|---------------------------|
| 组件 | 版本 | 新版本 |
| SpringBoot | 2.3.2.RELEASE | 2.7.18 |
| SpringCloud | Hoxton.SR9 | 2021.0.9 |
| SpringCloudAlibaba | 2.2.6.RELEASE | 2021.0.5.0 |
| spring-boot-starter-data-elasticsearch | 2.3.2.RELEASE | SpringBoot版本内部定义 |
| nacos-client | 1.4.2 | AlibabaCloud版本内部定义(2.2.0) |

SpringCloud与AlibabaCloud对应版本

信息来源:

版本发布说明-阿里云Spring Cloud Alibaba官网

Springboot与Elasticsearch版本对应

Spring Boot 和 Elasticsearch 的版本兼容关系需要根据 Spring Data Elasticsearch 模块的版本来确定,因为 Spring Boot 通过 Spring Data Elasticsearch 来集成 Elasticsearch。以下是一些常见的版本对应关系

|----------------|------------------------------|------------------|
| Spring Boot 版本 | Spring Data Elasticsearch 版本 | Elasticsearch 版本 |
| 3.1.x | 5.1.x | 8.x |
| 3.0.x | 5.0.x | 8.x |
| 2.7.x | 4.4.x | 7.x |
| 2.6.x | 4.3.x | 7.x |
| 2.5.x | 4.2.x | 7.x |
| 2.4.x | 4.1.x | 7.x |
| 2.3.x | 4.0.x | 7.x |
| 2.2.x | 3.2.x | 6.x |
| 2.1.x | 3.1.x | 6.x |
| 2.0.x | 3.0.x | 5.x |

RestHighLevelClient:从 Spring Data Elasticsearch 4.x 开始,推荐使用 Elasticsearch 官方的 RestHighLevelClient 代替旧的 TransportClient。

Elasticsearch 7.x 引入了一些重要的映射变化,比如默认情况下不再需要指定 _type 字段,所有文档类型默认使用 _doc。

2. openfeign 问题

问题

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: @RequestMapping annotation not allowed on @FeignClient interfaces |

Spring Cloud OpenFeign 3.x 版本中 @FeignClient 接口上不允许使用 @RequestMapping 注解的限制。

解决方案

将老代码中的 @RequestMapping 替换为具体的请求方式注解,例如 @GetMapping、@PostMapping 等。

修改前:

java 复制代码
@FeignClient(name = "example-service")

@RequestMapping("/example")

public interface ExampleFeignClient {

    @RequestMapping(method = RequestMethod.GET, value = "/getData")

    String getData();

}

修改后:

java 复制代码
@FeignClient(name = "example-service")

public interface ExampleFeignClient {

    @GetMapping("/example/getData")

    String getData();

}

Content-Type 头部设置了一个不允许的通配符 '*',而 Feign 不支持这种情况。

注解中加上 consumes = "application/json"参数,明确规定了请求的 Content-Type。

3. Feign请求问题

问题

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer? |

解决方法

你可以通过在 pom.xml 文件中手动添加 spring-cloud-starter-loadbalancer 依赖来解决这个问题:

XML 复制代码
<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-loadbalancer</artifactId>

</dependency>

4. Sentinel循环依赖

问题

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Description: The dependencies of some of the beans in the application context form a cycle: xxxxxImpl ↓ XxxxxImpl ↓ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration ┌─────┐ | com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration (field private java.util.Optional com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration.sentinelWebInterceptorOptional) └─────┘ Action: Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true. |

解决方案

升级为当前 Spring Cloud 一样的版本。

|-------------------------------------------------------------------------------------------------------------------------------------------------|
| <!--sentinel 版本 --> <spring-cloud-starter-alibaba-sentinel.version>2021.0.5.0</spring-cloud-starter-alibaba-sentinel.version> |

参考资料:

https://github.com/alibaba/spring-cloud-alibaba/issues/2322

https://juejin.cn/post/7080801716483915783 alibaba-sentinel启动报循环依赖

https://developer.aliyun.com/article/861163 springboot升级到2.6.1的坑

https://cloud.tencent.com/developer/article/2186649 我服了!SpringBoot升级后这服务我一个星期都没跑起来!

5. bootstrap配置文件不生效

问题

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set Description: No spring.config.import property has been defined Action: Add a spring.config.import=nacos: property to your configuration. If configuration is not required add spring.config.import=optional:nacos: instead. To disable this check, set spring.cloud.nacos.config.import-check.enabled=false. |

解决方案

XML 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

参考资料:

https://cloud.tencent.com/developer/ask/sof/108770896

https://sca.aliyun.com/en/faq/sca-user-question-history13954/

6. Nacos的连接错误

问题

ErrCode:-401, ErrMsg:Client not connected,current status:STARTING的解决方案

解决方案

开通相应新增端口

参考资料:

https://www.cnblogs.com/linyb-geek/p/16601335.html

https://nacos.io/zh-cn/docs/v2/upgrading/2.0.0-compatibility.html

https://blog.csdn.net/fenglibing/article/details/120164149

7. 跨域问题

问题

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| java.lang.IllegalArgumentException: 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.0之后,跨域配置中的.allowedOrigins不再可用,将配置中的.allowedOrigins替换成.allowedOriginPatterns即可

参考资料:

https://blog.csdn.net/weixin_43901865/article/details/119737447

相关推荐
冼紫菜16 小时前
Java开发中使用 RabbitMQ 入门到进阶详解(含注解方式、JSON配置)
java·spring boot·后端·rabbitmq·springcloud
一零贰肆1 天前
互联网大厂Java面试题:深入解析SpringCloud微服务架构中的服务注册与发现机制
java·微服务·nacos·面试题·springcloud
带刺的坐椅2 天前
SpringBoot3 使用 SolonMCP 开发 MCP
java·ai·springboot·solon·mcp
LUCIAZZZ3 天前
JVM之虚拟机运行
java·jvm·spring·操作系统·springboot
堕落年代3 天前
SpringSecurity当中的CSRF防范详解
前端·springboot·csrf
一零贰肆3 天前
深入理解SpringBoot中的SpringCache缓存技术
java·springboot·springcache·缓存技术
Naylor3 天前
微服务概述
微服务·架构·springcloud
LUCIAZZZ5 天前
JVM之内存管理(一)
java·jvm·spring·操作系统·springboot
LUCIAZZZ6 天前
JVM之内存管理(二)
java·jvm·后端·spring·操作系统·springboot
天上掉下来个程小白8 天前
缓存套餐-01.Spring Cache入门案例
java·redis·spring·缓存·springboot·springcache