20260915:traceId日志链路追踪

This commit is contained in:
zhugongping 2026-09-17 11:06:07 +08:00
parent 20a148a04e
commit 0cc08a8c5c
3 changed files with 13 additions and 2 deletions

View File

@ -10,7 +10,7 @@ import java.util.concurrent.Callable;
* 提供向后兼容的 API,内部委托给 TraceContext
*
* @author Sumuxi
* @deprecated 建议直接使用 {@link TraceContext}
* 建议直接使用 {@link TraceContext}
*/
public class TraceIdUtils {

View File

@ -1,6 +1,7 @@
package com.fastbee.framework.manager;
import com.fastbee.common.utils.Threads;
import com.fastbee.common.utils.TraceIdUtils;
import com.fastbee.common.utils.spring.SpringUtils;
import java.util.TimerTask;
@ -36,10 +37,17 @@ public class AsyncManager {
/**
* 执行任务
*
* <p>包装任务以透传 TraceId:{@code scheduledExecutorService} 是裸
* {@link java.util.concurrent.ScheduledThreadPoolExecutor},不支持
* {@code setTaskDecorator},因此必须在提交点包装任务本身。
* {@link TraceIdUtils#wrapWithMdc} 会捕获调用方线程的 TraceContext
* 快照并在 schedule-pool 线程内恢复,使登录/登出/操作日志等
* 异步落库链路的日志携带同一 TraceId。
*
* @param task 任务
*/
public void execute(TimerTask task) {
executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
executor.schedule(TraceIdUtils.wrapWithMdc(task), OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
}
/**

View File

@ -1,5 +1,6 @@
package com.fastbee.sip.conf;
import com.fastbee.common.trace.MdcTaskDecorator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
@ -26,6 +27,8 @@ public class ThreadPoolTaskConfig {
executor.setKeepAliveSeconds(keepAliveTime);
executor.setThreadNamePrefix(threadNamePrefix);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 配置 TaskDecorator,自动传播 TraceId
executor.setTaskDecorator(new MdcTaskDecorator());
executor.initialize();
return executor;
}