use*_*727 6 spring spring-boot micrometer-tracing
Spring Boot 3 改变了跟踪中的上下文传播。 https://github.com/micrometer-metrics/tracing/wiki/Spring-Cloud-Sleuth-3.1-Migration-Guide#async-instrumentation
他们现在提供针对此问题的库。我想我不太明白它是如何工作的。我已经按照指南创建了一个任务执行器。
@Bean(name = "taskExecutor")
ThreadPoolTaskExecutor threadPoolTaskScheduler() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor() {
@Override
protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
ExecutorService executorService = super.initializeExecutor(threadFactory, rejectedExecutionHandler);
return ContextExecutorService.wrap(executorService, ContextSnapshot::captureAll);
}
};
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
Run Code Online (Sandbox Code Playgroud)
我已经像这样标记了@Async:
@Async("taskExecutor")
public void run() {
// invoke some service
}
Run Code Online (Sandbox Code Playgroud)
但上下文不会传播到 taskExecutor 线程中的子上下文。
您可以ThreadPoolTaskExecutor在 AsyncConfigurer 中自动装配您的上下文包装。
import io.micrometer.context.ContextExecutorService;
import io.micrometer.context.ContextSnapshot;
import java.util.concurrent.Executor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
public class AsyncTraceContextConfig implements AsyncConfigurer {
// NOTE: By design you can only have one AsyncConfigurer, thus only one executor pool is
// configurable.
@Qualifier("taskExecutor") // if you have more than one task executor pools
private final ThreadPoolTaskExecutor taskExecutor;
@Override
public Executor getAsyncExecutor() {
return ContextExecutorService.wrap(
taskExecutor.getThreadPoolExecutor(), ContextSnapshot::captureAll);
}
}
Run Code Online (Sandbox Code Playgroud)
更新
如果您有多个执行程序池并希望向所有执行程序池添加跟踪,请使用TaskDecoratorwith ContextSnapshot.wrap():
import io.micrometer.context.ContextSnapshot;
import java.util.concurrent.Executor;
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskDecorator;
@Configuration
public class AsyncConfig {
@Bean
public TaskDecorator otelTaskDecorator() {
return (runnable) -> ContextSnapshot.captureAll(new Object[0]).wrap(runnable);
}
@Bean("asyncExecutorPool1")
public Executor asyncExecutorPool1(TaskDecorator otelTaskDecorator) {
return new TaskExecutorBuilder()
.corePoolSize(5)
.maxPoolSize(10)
.queueCapacity(10)
.threadNamePrefix("threadPoolExecutor1-")
.taskDecorator(otelTaskDecorator)
.build();
}
@Bean("asyncExecutorPool2")
public Executor asyncExecutorPool2(TaskDecorator otelTaskDecorator) {
return new TaskExecutorBuilder()
.corePoolSize(5)
.maxPoolSize(10)
.queueCapacity(10)
.threadNamePrefix("threadPoolExecutor2-")
.taskDecorator(otelTaskDecorator)
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
注意:您可以关注此博客以获取更多设置详细信息和示例 github 项目代码。
| 归档时间: |
|
| 查看次数: |
7921 次 |
| 最近记录: |