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

最近在学微服务,在学到Sentinel并引入相关依赖时发现出现了循环依赖的异常,翻阅多篇文章才有了如下的记录。
具体异常为:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration': Unsatisfied dependency expressed through field 'sentinelWebInterceptorOptional'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration': Requested bean is currently in creation: Is there an unresolvable circular reference?

具体来说,错误发生在 org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration 类中的 setConfigurers 方法,该方法的第一个参数存在一个无法满足的依赖。而这个依赖与 com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration 类中的 sentinelWebInterceptorOptional 字段有关。

简单来说就是Spring Cloud alibaba中的Sentinel依赖和web依赖形成了循环依赖,张三依赖李四,李四又依赖张三。同时spring是不允许存在循环依赖的,今天我遇到了就记录一下,踩个坑。

方法1:
在依赖兼容的区间内合理得更换Sentinel、Spring Cloud alibab和web依赖的版本。然而我更改后并没啥用,于是就有了第2种方法。

方法2:
在application.apropertites/yml配置文件中,添加如下的配置,声明spring允许存在依赖。但是spring是不允许存在循环依赖,尽管你声明了允许但可能会导致后续开发中出现更多的问题和bug,我试了此方法虽然循环依赖的问题不存在了,但是新的问题又出现了,因此我果断放弃。除非你声明后并且在后续的开发中未出现问题和bug,否则不推荐使用。

spring.main.allow-circular-references=true # 注意是appropertites语法

方法3:
在application.apropertites/yml配置文件中,添加如下的配置。我也是用了此方法后才解决循环依赖的问题,并且后续未产生新问题。

spring:
    sentinel:
      filter:
        enabled: false

希望能够对大家有所帮助。 

  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值