Spring Cloud项目,如果采用sleuth实现链路追踪,使用线程池时建议采用Configuration配置方式实例化线程池Bean,
这样可以用到sleuth链路追踪,输出日志有traceId和spanId,方便我们查看整个执行链路日志定位问题。
定义
有2种Executor可以定义
- Spring的ThreadPoolTaskExecutor
- JDK的ThreadPoolExecutor
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class ExecutorConfig {
/**
* 使用Spring的ThreadPoolTaskExecutor自定义Executor
* @return
*/
@Bean
public Executor customExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(50);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("CustomExecutor-");
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
/**
* JDK的ThreadPoolExecutor自定义Executor
* @return
*/
@Bean
public Executor jdkCustomExecutor() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 20, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(60));
executor.setThreadFactory(new CustomizableThreadFactory("CustomExecutor-"));
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}
使用
有2种使用Executor方式,
- 直接注入Executor,手动调用execute
- 使用@Async注解(注意@Aysnc注解是采用AOP实现,同一个类内部方法间调用不会生效)
//方式1,直接用Executor
@Resource(name = "customExecutor")
private Executor executor;
public void methodA(){
executor.execute(() -> {
//do something
});
}
//方式2,使用@Async注解,注意项目要有@EnableAsync注解
@Async("customExecutor")
public void methodB(){
//do something
}
能生成traceId和spanId的原因
- 引入sleuth包后org.springframework.cloud.sleuth.instrument.async.ExecutorBeanPostProcessor类会处理ThreadPoolTaskExecutor和ThreadPoolExecutor,生成代理类。
ThreadPoolTaskExecutor对应的代理类是org.springframework.cloud.sleuth.instrument.async.LazyTraceThreadPoolTaskExecutor
JDK的ThreadPoolExecutor对应的代理类是org.springframework.cloud.sleuth.instrument.async.LazyTraceExecutor
- Runnable和Callable也会被sleuth包裹成代理类。
execute执行的Runnable被包裹成org.springframework.cloud.sleuth.instrument.async.SpanContinuingTraceRunnable
@Async注解执行的Callable被包裹成org.springframework.cloud.sleuth.instrument.async.SpanContinuingTraceCallable
- SpanContinuingTraceRunnable和SpanContinuingTraceCallable生成链路追踪traceId和spanId。
日志
2020-05-13 08:58:42.569 INFO [abc-edf-service,aefefc33ebb8b0f9,aefefc33ebb8b0f9,false] 9192 --- [ XNIO-2 task-14] c.a.z.account.controller.TestController : testExecutor请求接收: []
2020-05-13 08:58:42.600 INFO [abc-edf-service,aefefc33ebb8b0f9,aefefc33ebb8b0f9,false] 9192 --- [chMsgExecutor-2] c.a.z.account.controller.TestController : ThreadPoolTaskExecutor execute
2020-05-13 08:58:42.602 INFO [abc-edf-service,aefefc33ebb8b0f9,7dd6028c7b79ed63,false] 9192 --- [chMsgExecutor-1] c.a.z.a.component.async.CustomTask : cn.akeparking.zbcloud.account.component.async.CustomTask
上面是使用Spring的ThreadPoolTaskExecutor不同使用方式的日志,有个问题:手动使用方式日志有链路发起方一样的traceId和spanId;@Async注解方式日志只有一样的traceId,spanId是新生成的。
原因:@Async注解有个切面org.springframework.cloud.sleuth.instrument.async.TraceAsyncAspect,会拦截@Asyc注解的方法,重新生成spanId。
那么使用JDK的ThreadPoolExecutor自定义Executor呢?
日志如下,
2020-08-21 18:24:30.541 INFO [abc-edf,ffff4056ccd39dea,ffff4056ccd39dea,false] 12884 --- [afka-listener-1] c.h.s.s.c.s.l.TopicMessageListener : receive {"content":"content9999","custom_id":"9999","key":9999}
2020-08-21 18:24:30.578 INFO [abc-edf,ffff4056ccd39dea,ffff4056ccd39dea,false] 12884 --- [ustomExecutor-1] c.h.s.concurrent.ExecutorDifferentUse : manual execute
2020-08-21 18:24:30.607 INFO [abc-edf,becef15f1baafe8b,becef15f1baafe8b,false] 12884 --- [ustomExecutor-2] c.h.s.concurrent.ExecutorDifferentUse : @Async execute
手动使用方式日志有链路发起方一样的traceId和spanId;@Async注解方式日志traceId和spanId都是新的。
原因:
JDK的ThreadPoolExecutor的代理类org.springframework.cloud.sleuth.instrument.async.LazyTraceExecutor没有重新实现submit方法,而@Async注解是被包装成Callable,需要用submit方法;所以@Async注解方式最终输出的是切面TraceAsyncAspect重新生成traceId和spanId,不能关联上链路发起方。
结论
不管是Spring的ThreadPoolTaskExecutor还是JDK的ThreadPoolExecutor,建议采用直接注入Executor,手动调用execute方式,这样可以避免@Async注解不能同一个类内部调用和traceId或spanId与链路发起方不一致的问题。