Spring boot分布式链路追踪
- 前言
- 演示项目核心依赖
- 一、系统配置文件 日志文件路径配置
- 二、日志文件 logback.xml 其中重要的标志是“requestId”字眼
- 三、配置全局拦截器 RequestIdTraceInterceptor.java 拦截交互请求带有 “requestId” 字眼的值
- 四、实际使用
- 五、效果展示 MDC.put下方所有在内的完整日志链路都会出现 如【2f61b36c-2eaa-4701-aafe-648eb43e0c40】字眼,在查询时直接搜索即可
前言
在分布式开发中,经常会遇到程序出现问题定位困难,处理起来耗时耗力,做无用功
下面就来给出一种方案来解决这一问题
演示项目核心依赖
依赖 | 版本 |
---|---|
Spring Boot | 2.4.4 |
Spring Cloud | 2020.0.2 |
Spring Cloud Alibaba | 2021.1 |
Spring Security OAuth2 | 2.3.6 |
Mybatis Plus | 3.4.2 |
hutool | 5.6.1 |
Avue | 2.6.16 |
一、系统配置文件 日志文件路径配置
#日志配置
logging:config: classpath:log/logback.xml
二、日志文件 logback.xml 其中重要的标志是“requestId”字眼
<?xml version="1.0" encoding="UTF-8"?>
<!--~ Copyright (c) 2021 ctsc-cloudx Authors. All Rights Reserved.~~ Licensed under the Apache License, Version 2.0 (the "License");~ you may not use this file except in compliance with the License.~ You may obtain a copy of the License at~~ http://www.apache.org/licenses/LICENSE-2.0~~ Unless required by applicable law or agreed to in writing, software~ distributed under the License is distributed on an "AS IS" BASIS,~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.~ See the License for the specific language governing permissions and~ limitations under the License.--><configuration debug="false" scan="false"><springProperty scop="context" name="spring.application.name" source="spring.application.name" defaultValue=""/><property name="log.path" value="logs/${spring.application.name}"/><!-- 彩色日志格式 --><property name="CONSOLE_LOG_PATTERN"value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr([%X{requestId}]){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/><!-- 彩色日志依赖的渲染类 --><conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/><conversionRule conversionWord="wex"converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/><conversionRule conversionWord="wEx"converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/><!-- 控制台输出 --><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><pattern>${CONSOLE_LOG_PATTERN}</pattern><charset>utf8</charset></encoder></appender><!-- 按照每天生成日志文件 --><appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"><!--日志文件输出的文件名 --><FileNamePattern>${log.path}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log</FileNamePattern><maxFileSize>10MB</maxFileSize><totalSizeCap>1GB</totalSizeCap><!--日志文件保留天数 --><MaxHistory>30</MaxHistory></rollingPolicy><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 --><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] [%thread] %-5level %logger{50} - %msg%n</pattern><charset>UTF-8</charset></encoder></appender><!-- 异步输出 --><appender name="ASYNC-INFO" class="ch.qos.logback.classic.AsyncAppender"><!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 --><discardingThreshold>0</discardingThreshold><!-- 更改默认的队列的深度,该值会影响性能.默认值为256 --><queueSize>256</queueSize><!-- 添加附加的appender,最多只能添加一个 --><appender-ref ref="FILE"/></appender><!-- MyBatis log configure --><logger name="com.apache.ibatis" level="INFO"/><logger name="org.mybatis.spring" level="INFO"/><logger name="java.sql.Connection" level="INFO"/><logger name="java.sql.Statement" level="INFO"/><logger name="java.sql.PreparedStatement" level="INFO"/><!-- 减少部分debug日志 --><logger name="druid.sql" level="INFO"/><logger name="org.apache.shiro" level="INFO"/><logger name="org.mybatis.spring" level="INFO"/><logger name="org.springframework" level="INFO"/><logger name="org.springframework.context" level="INFO"/><logger name="org.springframework.beans" level="INFO"/><logger name="com.baomidou.mybatisplus" level="INFO"/><logger name="org.apache.ibatis.io" level="INFO"/><logger name="org.apache.velocity" level="INFO"/><logger name="org.eclipse.jetty" level="INFO"/><logger name="io.undertow" level="INFO"/><logger name="org.xnio.nio" level="INFO"/><logger name="org.thymeleaf" level="INFO"/><logger name="springfox.documentation" level="ERROR"/><logger name="org.hibernate" level="ERROR"/><logger name="com.netflix.loadbalancer" level="INFO"/><logger name="com.netflix.hystrix" level="INFO"/><logger name="com.netflix.zuul" level="INFO"/><logger name="de.codecentric" level="INFO"/><!-- cache INFO --><logger name="net.sf.ehcache" level="INFO"/><logger name="org.apache.commons" level="INFO" /><logger name="org.springframework.cache" level="INFO"/><!-- cloud --><logger name="org.apache.http" level="INFO"/><logger name="com.netflix.discovery" level="INFO"/><logger name="com.netflix.eureka" level="INFO"/><!-- 减少nacos日志 --><logger name="com.alibaba.nacos" level="ERROR"/><!-- 业务日志 --><Logger name="com.cl.frame" level="INFO"/><Logger name="com.cl.framecore.version" level="INFO"/><!-- 日志输出级别 --><root level="INFO"><appender-ref ref="ASYNC-INFO"/><appender-ref ref="STDOUT"/></root></configuration>
三、配置全局拦截器 RequestIdTraceInterceptor.java 拦截交互请求带有 “requestId” 字眼的值
package com.xxx.interceptor;import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** 链路追踪*/
@Slf4j
@Component
public class RequestIdTraceInterceptor implements HandlerInterceptor {public static final String REQUEST_ID_KEY = "requestId";@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {MDC.put(REQUEST_ID_KEY, getRequestId(request));return true;}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {//把requestId添加到响应头,以便其它应用使用response.addHeader(REQUEST_ID_KEY, MDC.get(REQUEST_ID_KEY));//请求完成,从MDC中移除requestIdMDC.remove(REQUEST_ID_KEY);}public static String getRequestId(HttpServletRequest request) {String requestId;String parameterRequestId = request.getParameter(REQUEST_ID_KEY);String headerRequestId = request.getHeader(REQUEST_ID_KEY);// 根据请求参数或请求头判断是否有“request-id”,有则使用,无则创建if (parameterRequestId == null && headerRequestId == null) {log.debug("no request-id in request parameter or header");requestId = IdUtil.simpleUUID();} else {requestId = parameterRequestId != null ? parameterRequestId : headerRequestId;}return requestId;}
}
四、实际使用
String requestId= UuidUtils.generateUuid();//hutool带的线程池方法 可不用ThreadUtil.execAsync(() -> {//添加唯一字眼MDC.put(RequestIdTraceInterceptor.REQUEST_ID_KEY, requestId);try {//业务代码} catch (Exception e) {log.info("异常={}", e);}//请求完成,从MDC中移除requestIdMDC.remove(RequestIdTraceInterceptor.REQUEST_ID_KEY);});
五、效果展示 MDC.put下方所有在内的完整日志链路都会出现 如【2f61b36c-2eaa-4701-aafe-648eb43e0c40】字眼,在查询时直接搜索即可
2022-12-12 11:02:56.915 [2f61b36c-2eaa-4701-aafe-648eb43e0c40] [pool-2-thread-1] INFO xxx - 上报异常={}