springcloud项目整合sleuth或zipkin后启动报错,与redis初始化冲突死锁

在SpringCloud项目中整合Sleuth和Zipkin时遇到了启动报错,错误源于bean创建的死锁。解决方法是在application.properties中设置spring.sleuth.redis.enabled=false,并在启动类中排除TraceRedisAutoConfiguration,同时自定义配置采样器。通过配置一个概率为10%的ProbabilityBasedSampler来避免使用Spring Boot的自动配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

springcloud项目整合sleuth后启动报错

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name ‘org.springframework.cloud.sleuth.sampler.SamplerAutoConfiguration$RefreshScopedSamplerConfiguration’: Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

原因是造成了死锁,具体分析过程见“导入 spring-cloud-starter-zipkin 之后系统启动卡住”
“引入 spring-cloud-starter-zipkin 组件之后,启动项目卡住(死锁)”

解决方式

  1. 在配置文件application.properties加上spring.sleuth.redis.enabled=false
  2. 启动类排除TraceRedisAutoConfiguration.class
    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,TraceRedisAutoConfiguration.class})
  3. 自己声明配置一个zipkin的采样器,而不是使用 spring boot 自动配置的 代码如下:
@Configuration
public class SleuthSamplerConfiguration {
    
    @Bean
    public Sampler defaultSampler() throws Exception {
        // 这个值是采样率,设置为1就是100%采样,每一条请求都采,0.1就是10%采样率
        Float f = new Float(0.1f);
        SamplerProperties samplerProperties = new SamplerProperties();
        samplerProperties.setProbability(f);
        ProbabilityBasedSampler sampler = new ProbabilityBasedSampler(samplerProperties);
        return sampler;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值