解决引入sentinel依赖时与SentinelWebAutoConfiguration循环依赖的问题

解决引入sentinel依赖时与web依赖形成循环依赖的问题

最近在学习黑马的微服务课程,在学到Sentinel并引入相关依赖时发现出现了SentinelWebAutoConfiguration循环依赖的异常,在此小小记录一下解决问题方法:

在学习黑马微服务课程微服务保护机制,在购物车模块引入Sentinel的依赖,在重启微服务的过程中,出现如下错误:

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

Description:

The dependencies of some of the beans in the application context form a cycle:

   cartController defined in file [D:\JavaFrameworkAndProjectExercises\hmall\cart-sevice\target\classes\com\hmall\cart\controller\CartController.class]
      ↓
   cartServiceImpl
      ↓
   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.

这个错误提示表明在应用程序上下文中存在循环依赖。具体来说,‘cartController’ 依赖于 ‘cartServiceImpl’,而 ‘cartServiceImpl’ 又依赖于 ‘org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration’,形成了一个循环依赖关系。

为了解决这个问题,可以尝试以下方法:

  1. 检查并更新您的代码,确保没有循环依赖。这可能涉及到重构代码、更改注入方式(例如使用构造函数注入代替字段注入)等。

  2. 如果问题是由库的特定版本引起的,尝试更新到较新的版本,看看问题是否得到解决。

  3. 禁用Sentinel的自动配置。在application.properties文件中添加以下行:

spring.autoconfigure.exclude=com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration

或者在application.yml文件中添加以下内容:

spring:
  autoconfigure:
    exclude: com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration

这将禁用Sentinel的自动配置,从而避免循环依赖的问题。但请注意,这也意味着将无法使用Sentinel的流量控制和熔断降级功能。

  1. 作为最后的手段,可以设置spring.main.allow-circular-references属性为true,以允许循环引用。但是,请注意,这可能会导致难以调试的问题和意外的应用程序行为。在application.properties文件中添加以下行:
spring.main.allow-circular-references=true

或者在application.yml文件中添加以下内容:

spring:
  main:
    allow-circular-references: true

​ 5.禁用Sentinel的过滤器

spring:
    sentinel:
      filter:
        enabled: false

在Spring Boot中,通过设置spring.sentinel.filter.enabled属性为false,可以禁用Sentinel的过滤器功能。这一配置直接影响到Sentinel的流量控制和熔断降级功能,因为这些特性通常依赖于过滤器来实施规则和管理流量。

最后这几种方法可能会对程序产生影响,或者会使Sentinel部分功能失效等,因此,都不是推荐的解决方案。

最好考虑版本兼容性寻找SpringBoot、SpringCloud、Spring Cloud Alibaba对应版本,减少bug的产生

以下是三者之间版本对应关系:

SpringBoot、SpringCloud、Spring Cloud Alibaba版本对应

由于 Spring Boot 3.0,Spring Boot 2.7~2.4 和 2.4 以下版本之间变化较大,目前企业级客户老项目相关 Spring Boot 版本仍停留在 Spring Boot 2.4 以下,为了同时满足存量用户和新用户不同需求,社区以 Spring Boot 3.0 和 2.4 分别为分界线,同时维护 2022.x、2021.x、2.2.x 三个分支迭代。如果不想跨分支升级,如需使用新特性,请升级为对应分支的新版本。 为了规避相关构建过程中的依赖冲突问题,可以通过 云原生应用脚手架 进行项目创建。

2022.x 分支

适配 Spring Boot 3.0,Spring Cloud 2022.x 版本及以上的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记): (注意,该分支 Spring Cloud Alibaba 版本命名方式进行了调整,未来将对应 Spring Cloud 版本,前三位为 Spring Cloud 版本,最后一位为扩展版本,比如适配 Spring Cloud 2022.0.0 版本对应的 Spring Cloud Alibaba 第一个版本为:2022.0.0.0,第个二版本为:2022.0.0.1,依此类推)

Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version
2022.0.0.0-RC2*Spring Cloud 2022.0.03.0.2
2022.0.0.0-RC1Spring Cloud 2022.0.03.0.0

2021.x 分支

适配 Spring Boot 2.4,Spring Cloud 2021.x 版本及以上的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记):

Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version
2021.0.5.0*Spring Cloud 2021.0.52.6.13
2021.0.4.0Spring Cloud 2021.0.42.6.11
2021.0.1.0Spring Cloud 2021.0.12.6.3
2021.1Spring Cloud 2020.0.12.4.2

2.2.x 分支

适配 Spring Boot 为 2.4,Spring Cloud Hoxton 版本及以下的 Spring Cloud Alibaba 版本按从新到旧排列如下表(最新版本用*标记):

Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version
2.2.10-RC1*Spring Cloud Hoxton.SR122.3.12.RELEASE
2.2.9.RELEASESpring Cloud Hoxton.SR122.3.12.RELEASE
2.2.8.RELEASESpring Cloud Hoxton.SR122.3.12.RELEASE
2.2.7.RELEASESpring Cloud Hoxton.SR122.3.12.RELEASE
2.2.6.RELEASESpring Cloud Hoxton.SR92.3.2.RELEASE
2.2.1.RELEASESpring Cloud Hoxton.SR32.2.5.RELEASE
2.2.0.RELEASESpring Cloud Hoxton.RELEASE2.2.X.RELEASE
2.1.4.RELEASESpring Cloud Greenwich.SR62.1.13.RELEASE
2.1.2.RELEASESpring Cloud Greenwich2.1.X.RELEASE
2.0.4.RELEASE(停止维护,建议升级)Spring Cloud Finchley2.0.X.RELEASE
1.5.1.RELEASE(停止维护,建议升级)Spring Cloud Edgware1.5.X.RELEASE

Spring Cloud Alibaba 组件版本关系

每个 Spring Cloud Alibaba 版本及其自身所适配的各组件对应版本如下表所示(注意,Spring Cloud Dubbo 从 2021.0.1.0 起已被移除出主干,不再随主干演进):

Spring Cloud Alibaba VersionSentinel VersionNacos VersionRocketMQ VersionDubbo VersionSeata Version
2022.0.0.0-RC21.8.62.2.14.9.4~1.7.0-native-rc2
2021.0.5.01.8.62.2.04.9.4~1.6.1
2.2.10-RC11.8.62.2.04.9.4~1.6.1
2022.0.0.0-RC11.8.62.2.1-RC4.9.4~1.6.1
2.2.9.RELEASE1.8.52.1.04.9.4~1.5.2
2021.0.4.01.8.52.0.44.9.4~1.5.2
2.2.8.RELEASE1.8.42.1.04.9.3~1.5.1
2021.0.1.01.8.31.4.24.9.2~1.4.2
2.2.7.RELEASE1.8.12.0.34.6.12.7.131.3.0
2.2.6.RELEASE1.8.11.4.24.4.02.7.81.3.0
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE1.8.01.4.14.4.02.7.81.3.0
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE1.8.01.3.34.4.02.7.81.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE1.7.11.2.14.4.02.7.61.2.0
2.2.0.RELEASE1.7.11.1.44.4.02.7.4.11.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE1.7.01.1.44.4.02.7.30.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE1.6.31.1.14.4.02.7.30.7.1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值