分布式链路追踪—traceId生成与使用(MDC)
Posted 张志翔 ̮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分布式链路追踪—traceId生成与使用(MDC)相关的知识,希望对你有一定的参考价值。
之前遇到过在分布式环境中不方便查找日志的问题,虽然现在的很多云服务环境都提供这种功能,但是在自己搭建的分布式环境中可以考虑应急的这样做,先记录下,以便下次快速使用
注意:MDC-traceId在分布式环境中使用步骤(下面的3456四个类在分布式用到的工程中都需要添加,可以考虑添加到第三方工程中统一在pom中引入)
1、 引入log4j的同时需要注意去除冲突包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 排除自带的logback依赖 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
2、resources 文件夹下添加[log4j.xml]配置文件
<appender name="trace" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="D://logs//trace.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="threshold" value="info"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%dyyyy-MM-dd HH:mm:ss.SSS] - [%XtraceId] %-6p%c:%L - %m%n"/>
</layout>
</appender>
<root>
<level value="info"/>
<appender-ref ref="trace"/>
</root>
3、添加[MDCUtils]自定义工具类
public class MDCUtils
/**
* [获取 traceId]
* @return java.lang.String
**/
public static String mdc()
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = (HttpServletRequest) requestAttributes
.resolveReference(RequestAttributes.REFERENCE_REQUEST);
String traceId;
String traceIdKey = "traceId";
if (request.getHeader(traceIdKey) == null)
traceId = UUID.randomUUID().toString();
else
traceId = request.getHeader(traceIdKey);
return traceId;
4、添加拦截器[LogInterceptor]为每一个请求头添加[traceId]
public class LogInterceptor implements HandlerInterceptor
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
String traceIdKey = "traceId";
String traceId = MDCUtils.mdc();
request.setAttribute(traceIdKey, traceId);
MDC.clear();
MDC.put(traceIdKey, traceId);
return true;
5、注册拦截器
@Configuration
public class WebMvcConfig implements WebMvcConfigurer
@Override
public void addInterceptors(InterceptorRegistry registry)
registry.addInterceptor(new LogInterceptor()).addPathPatterns("/test/*");
6、在每次调用外部接口的httpclient地方添加请求头[traceId]
public class HttpClientHelper
public static String sendGet(String url, LinkedHashMap<String, Object> paramMap, Map<String, Object> headMap)
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
String entityStr = null;
CloseableHttpResponse response = null;
try
/*
* 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
*/
URIBuilder uriBuilder = new URIBuilder(url);
// 第一种添加参数的形式
/* uriBuilder.addParameter("name", "root");
uriBuilder.addParameter("password", "123456");*/
// 第二种添加参数的形式
// 加载参数
if (!CollectionUtils.isEmpty(paramMap))
List<NameValuePair> paramertersList = new LinkedList<>();
Set<String> keys = paramMap.keySet();
for (String s : keys)
String key = String.valueOf(s);
String value = ObjectUtils.isEmpty(paramMap.get(key)) ? null : paramMap.get(key).toString();
if (ObjectUtils.isNotEmpty((value)))
BasicNameValuePair param1 = new BasicNameValuePair(key, value);
paramertersList.add(param1);
uriBuilder.setParameters(paramertersList);
// 根据带参数的URI对象构建GET请求对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
if (headMap != null)
for (Map.Entry<String, Object> entry : headMap.entrySet())
if (entry.getValue() != null)
httpGet.addHeader(entry.getKey(), entry.getValue().toString());
String traceId = MDC.get("traceId");
httpGet.addHeader("traceId", traceId);
httpGet.addHeader("Accept", "*/*");
httpGet.addHeader("Connection", "Keep-Alive");
// 浏览器表示
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
// 传输的类型
httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
// 执行请求
response = httpClient.execute(httpGet);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
entityStr = EntityUtils.toString(entity, "UTF-8");
catch (ClientProtocolException e)
System.err.println("Http协议出现问题");
e.printStackTrace();
catch (ParseException e)
System.err.println("解析错误");
e.printStackTrace();
catch (URISyntaxException e)
System.err.println("URI解析异常");
e.printStackTrace();
catch (IOException e)
System.err.println("IO异常");
e.printStackTrace();
finally
// 释放连接
if (null != response)
try
response.close();
httpClient.close();
catch (IOException e)
System.err.println("释放连接出错");
e.printStackTrace();
return entityStr;
public static String sendPost(String url, LinkedHashMap<String, Object> paramMap, Map<String, Object> headMap)
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
String entityStr = null;
CloseableHttpResponse response = null;
try
// 创建POST请求对象
HttpPost httpPost = new HttpPost(url);
UrlEncodedFormEntity entityParam = null;
/*
* 添加请求参数
*/
// 创建请求参数
if (!CollectionUtils.isEmpty(paramMap))
List<NameValuePair> paramertersList = new LinkedList<>();
Set<String> keys = paramMap.keySet();
for (String s : keys)
String key = String.valueOf(s);
String value = ObjectUtils.isEmpty(paramMap.get(key)) ? null : paramMap.get(key).toString();
if (ObjectUtils.isNotEmpty(value))
BasicNameValuePair param1 = new BasicNameValuePair(key, value);
paramertersList.add(param1);
// 使用URL实体转换工具
entityParam = new UrlEncodedFormEntity(paramertersList, "UTF-8");
httpPost.setEntity(entityParam);
if (headMap != null)
for (Map.Entry<String, Object> entry : headMap.entrySet())
if (entry.getValue() != null)
httpPost.addHeader(entry.getKey(), entry.getValue().toString());
String traceId = MDC.get("traceId");
httpPost.addHeader("traceId", traceId);
httpPost.addHeader("Accept", "*/*");
httpPost.addHeader("Connection", "Keep-Alive");
httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
// 传输的类型
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
// 执行请求
response = httpClient.execute(httpPost);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
entityStr = EntityUtils.toString(entity, "UTF-8");
// 此处获取所有的响应头信息并进行打印
System.out.println(Arrays.toString(response.getAllHeaders()));
catch (ClientProtocolException e)
System.err.println("Http协议出现问题");
e.printStackTrace();
catch (ParseException e)
System.err.println("解析错误");
e.printStackTrace();
catch (IOException e)
System.err.println("IO异常");
e.printStackTrace();
finally
// 释放连接
if (null != response)
try
response.close();
httpClient.close();
catch (IOException e)
System.err.println("释放连接出错");
e.printStackTrace();
return entityStr;
7、请求实例: 1-调用-2
1:
2021-07-21 11:14:30.168 - [283ba7ba-8910-48e3-8864-cd8de7af07cb] INFO com.example.demolog4jmdc.controller.TestController:27 - 测试 test1
2021-07-21 11:14:30.169 - [283ba7ba-8910-48e3-8864-cd8de7af07cb] INFO com.example.demolog4jmdc.controller.TestService:18 - =====TestService.test1
2021-07-21 11:14:30.169 - [283ba7ba-8910-48e3-8864-cd8de7af07cb] INFO com.example.demolog4jmdc.controller.TestService:22 - =====TestService.test3
2021-07-21 11:14:30.169 - [283ba7ba-8910-48e3-8864-cd8de7af07cb] INFO com.example.demolog4jmdc.controller.TestService:26 - =====TestService.test3
2021-07-21 11:14:30.771 - [283ba7ba-8910-48e3-8864-cd8de7af07cb] INFO com.example.demolog4jmdc.controller.TestController:35 - ===s===aaaaa
2:
2021-07-21 11:14:30.746 - [283ba7ba-8910-48e3-8864-cd8de7af07cb] INFO com.example.demolog4jmdc2.controller.TestController:26 - 测试 test1-traceId:283ba7ba-8910-48e3-8864-cd8de7af07cb
到此分布式链路追踪—traceId生成与使用(MDC)介绍完成。
以上是关于分布式链路追踪—traceId生成与使用(MDC)的主要内容,如果未能解决你的问题,请参考以下文章
dubbo traceId透传实现日志链路追踪(基于Filter和RpcContext实现)