这篇“全局请求怎么添加TraceId”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“全局请求怎么添加TraceId”文章吧。
请求拦截器
@Component public class TraceIdInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String traceId = String.format("%s - %s",request.getRequestURI(), UUID.fastUUID().toString(true)); MDC.put("traceId", traceId); return true; }
注册拦截器
@Component @RequiredArgsConstructor public class GlobalWebMvcConfigurer implements WebMvcConfigurer { private final TraceIdInterceptor traceIdInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(traceIdInterceptor); } }
统一返回值
@Data @NoArgsConstructor @AllArgsConstructor public class CommonResponse<T> implements Serializable { /** 错误码 */ private Integer code; /** 错误消息 */ private String message; /** 泛型响应数据 */ private T Data; private String traceId; public CommonResponse(Integer code, String message) { this.code = code; this.message = message; this.traceId = MDC.get("traceId"); } }
日志配置
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="LOG_PATTERN" value="%d{yyyy-MM-dd} %d{HH:mm:ss.SSS} [%highlight(%-5level)] [%boldYellow(%X{traceId})] [%boldYellow(%thread)] %boldGreen(%logger{36} %F.%L) %msg%n"> </property> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${LOG_PATTERN}</pattern> </encoder> <!-- 控制台打印INFO及以上级别的日志 --> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> </appender> <root> <appender-ref ref="STDOUT"/> </root> </configuration>
测试
接口返回值
{ "code": 0, "message": "", "traceId": "/commerce-user/user/findById - 73e470120ef24d57a111de6b671d030b", "data": { "id": 1, "username": "test1", "password": "test", "extraInfo": "{}", "createTime": "2022-06-20T09:23:18.000+00:00", "updateTime": "2022-06-20T09:23:18.000+00:00", "balance": 0 } }
日志
如此,如果线上有接口出现问题,拿着 traceId 到日志文件搜索,就能检索到该请求的日志调用链路,处理问题就变得简单了。
异步调用配置
因为是 ThreadLocal ,异步任务必然是获取不到 traceId 的,需要再线程池配置中手动添加。
线程池配置
@Configuration public class AsyncConfig implements AsyncConfigurer { @Bean @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); executor.setMaxPoolSize(20); executor.setQueueCapacity(20); executor.setKeepAliveSeconds(60); executor.setThreadNamePrefix("user-async-"); // 等待所有任务结果候再关闭线程池 executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(60); // 定义拒绝策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //设置线程装饰器 executor.setTaskDecorator(runnable -> ThreadMdcUtils.wrapAsync(runnable, MDC.getCopyOfContextMap())); // 初始化线程池, 初始化 core 线程 executor.initialize(); return executor; } }
public class ThreadMdcUtils { public static Runnable wrapAsync(Runnable task, Map<String,String> context){ return () -> { if(context==null){ MDC.clear(); }else { MDC.setContextMap(context); } if(MDC.get("traceId")==null){ MDC.put("traceId", UUID.fastUUID().toString(true)); } try { task.run(); }finally { MDC.clear(); } }; } }
再次测试
以上就是关于“全局请求怎么添加TraceId”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注花开半夏行业资讯频道。