Cloud Sleuth 从 Spring Boot 1.5 更改为 2.x

Cloud Sleuth change from SpringBoot 1.5 to 2.x

之前这段代码运行良好:

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
class CustomTraceableExecutorServiceImpl implements ExecutorServiceProvider {

    @Qualifier(value = "defaultExecutorService")
    private final ExecutorService executorService;
    @Qualifier(value = "scheduledTimeoutExecutorService")
    private final ScheduledExecutorService scheduledExecutorService;
    private final Tracer tracer;
    private final SpanNamer spanNamer;

    @Override
    public ExecutorService get() {
        return new TraceableExecutorService(executorService, tracer, new TraceKeys(), spanNamer);
    }

    @Override
    public ScheduledExecutorService getScheduled() {
        return new TraceableScheduledExecutorService(scheduledExecutorService, tracer, new TraceKeys(), spanNamer);
    }
}

但是现在 TraceableExecutorService 构造函数需要 TraceableExecutorService(BeanFactory beanFactory, final ExecutorService delegate) 所以我需要将代码更改为:

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
class CustomTraceableExecutorServiceImpl implements ExecutorServiceProvider {

    @Qualifier(value = "defaultExecutorService")
    private final ExecutorService executorService;
    @Qualifier(value = "scheduledTimeoutExecutorService")
    private final ScheduledExecutorService scheduledExecutorService;
    //@Qualifier(value = "defaultBeanFactory")
    @Autowired
    private final BeanFactory beanFactory;
    //@Qualifier(value = "scheduledBeanFactory")
    @Autowired
    private final BeanFactory scheduledBeanFactory;

    @Override
    public ExecutorService get() {
        return new TraceableExecutorService(beanFactory, executorService);
    }

    @Override
    public ScheduledExecutorService getScheduled() {
        return new TraceableScheduledExecutorService(scheduledBeanFactory, scheduledExecutorService);
    }
}

我遇到的问题是关于 BeanFactory 我不知道如何创建它? 我没有可以使用的 xml 文件。

正如我在这里读到的:what are the different ways of creating beanfactory object? 我们可以通过以下方式创建此 BeanFactory

1. BeanFactory fac=new ClassPathXmlApplicationContext("Spring-Config.xml"); 

2. Resource res=new Classpathresource("Spring-Config.xml");
    BeanFactory fac=new XmlBeanFactory(res);

如果我没有任何 xml 文件,我如何简单地创建它?我只是希望我的代码与旧版本(SpringBoot 1.5 版本)兼容。

Spring 为您创建 BeanFactory。如果您使用的是 Spring 上下文,则开箱即用。但是,如果出于某种原因您没有使用 Spring 而使用 Sleuth(我不知道您为什么要这样做),您可以检查我们是否使用 BeanFactory 来检索 bean。 BeanFactory 是一个接口,所以你可以做的是覆盖 getBean 方法来检索这样的 beans:

Tracing tracing() {
        if (this.tracing == null && this.beanFactory != null) {
            this.tracing = this.beanFactory.getBean(Tracing.class);
        }
        return this.tracing;
    }

    SpanNamer spanNamer() {
        if (this.spanNamer == null && this.beanFactory != null) {
            this.spanNamer = this.beanFactory.getBean(SpanNamer.class);
        }
        return this.spanNamer;
    }

你需要模拟 BeanFactory 到 return this.beanFactory.getBean(Tracing.class)this.beanFactory.getBean(SpanNamer.class)

但只需使用 Spring,您就可以开箱即用。坦白说,我不太明白你在做什么。