MDC 方式实现日志链路追踪
-
拦截器
package com.cdn.log.interceptor;import com.cdn.log.consts.CLogConst; import com.cdn.log.utils.IdUtil; import org.slf4j.MDC; import org.springframework.util.StringUtils; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.UUID;/*** @author 蔡定努* @date 2023/10/06 17:33*/ public class ClogInterceptor extends HandlerInterceptorAdapter {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {String traceId = request.getHeader(CLogConst.TRACE_ID);MDC.put(CLogConst.TRACE_ID, StringUtils.isEmpty(traceId) ? IdUtil.simpleId() : traceId);return true;}@Overridepublic void postHandle(HttpServletRequest request,