错误说明

使用2021.1版的spring-cloud-alibaba时,如果启用了sentinel作为流控、降级服务,启动的时候会报错误:
The dependencies of some of the beans in the application context form a cycle:
com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration (field private java.util.Optional com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration.sentinelWebInterceptorOptional)
在这里插入图片描述

这个报错其实很明显了,就是sentinel下的某一个类,循环依赖了(A中注入了B,B中也注入了A), 原因就是新版本的Spring默认不允许循环依赖!

解决方案

方式一:把enabled设为false(不推荐)

spring.cloud.sentinel.enabled默认是启用状态,为true,把它设为false之后就是不启用了。这样确实是不报循环依赖的错误了,但是项目中也没有使用sentinel了

spring:
  cloud:
    sentinel:
      enabled: false
      transport:
        dashboard: 127.0.0.1:20059
        port: 8719

方式二:配置允许循环依赖(推荐)

修改Spring的配置,设置允许循环依赖,把spring.main.allow-circular-references设为true

spring:
  application:
    name: ms-demo
  profiles:
    active: dev
  #允许循环依赖
  main:
    allow-circular-references: true
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐