全局請求添加TraceId輕松看日志

引言

不知道大傢有沒有一堆日志就是定位不到那塊是異常部分,接口錯誤無法復現,也找不到報錯信息等比較棘手的問題。

其實解決上面的問題很簡單,隻要我們為每一個請求都分配一個唯一的 RequestId 或者叫 TraceId ,一旦出瞭問題,隻需要拿著 Id 去日志裡一搜,妖魔鬼怪立馬原形畢露。

對於分佈式鏈路追蹤,有很多開源中間件,本文主要通過 logback 的 MDC 實現。

請求攔截器

@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輕松看日志的詳細內容,更多關於全局請求添加TraceId的資料請關註WalkonNet其它相關文章!

推薦閱讀: