sentinel报错:field private java.util.Optional com.alibaba.cloud.sentinel.SentinelWebAutoConfiguration

错误说明

使用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

猜你喜欢

转载自blog.csdn.net/weixin_50989469/article/details/129402035