微服务分布式架构中,如何实现日志链路跟踪?
Logback 背景
Logback是由log4j创始人设计的另一个开源日志组件,官方网站:http://logback.qos.ch。它当前分为下面下个模块:
logback-core:其它两个模块的基础模块
logback-classic:它是log4j的一个改良版本,同时它完整实现了slf4j API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging
logback-access:访问模块与Servlet容器集成提供通过Http来访问日志的功能
▐ 普通debug日志
▐ SQL执行日志
Logback 配置案例
日志级别排序为:TRACE < DEBUG < INFO < WARN < ERROR
%d:表示日期
%n:换行
%thread:表示线程名
%level:日志级别
%msg:日志消息
%file:表示文件名
%class:表示文件名
%logger:Java类名(含包名,这里设定了36位,若超过36位,包名会精简为类似a.b.c.JavaBean)
%line:Java类的行号
%-4relative %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread][%X{TRACE_ID}] %-5level %logger{100}.%M\(%line\) - %msg%n
▐ 案例
3452487 2021-08-03 15:19:36.940 [thread-monitor-daemon][] WARN com.xxxx.common.util.MonitorLogger.warn(27) - 发现超时线程notify-replay-consumer...
由于案例中是守护线程thread-monitor-daemon,所以不记录链路ID。
回归正题比如下面的例子中记录了请求的链路ID
19006989 2021-08-04 22:35:25.776 [http-nio-0.0.0.0-8010-exec-10][1fc8pebmgwukw863w2p342rp2936a3r157w0:0:] INFO com.xxx.framework.eureka.core.listener.EurekaStateChangeListener.listen(58) - 服务实例[XX-PAAS]注册成功,当前服务器已注册服务实例数量[3]
链路ID设计
▐ 第一种
▐ 第二种
public static class ThreadTraceListener implements ThreadListener {
@Override
public void onThreadBegin(HttpServletRequest request) {
String traceToken = ThreadLocalUtil.getTranVar(TRACE_ID);
String fromServer = ThreadLocalUtil.getTranVar(FROM_SERVER);
int deep;
String traceId;
if (StringUtils.isBlank(traceToken)) {
traceId = IDGenerator.generateID();
deep = 0;
traceToken = StringHelper.join(traceId, ":0");
} else {
int index = traceToken.lastIndexOf(':');
traceId = traceToken.substring(0, index);
deep = Integer.valueOf(traceToken.substring(index + 1));
}
ThreadLocalUtil.setLocalVar(TRACE_ID, traceId);
ThreadLocalUtil.setLocalVar(TRACE_DEEP, deep);
ThreadLocalUtil.setTranVar(TRACE_ID, StringHelper.join(traceId, ":", deep + 1));
ThreadLocalUtil.setLocalVar(FROM_SERVER, fromServer);
ThreadLocalUtil.setTranVar(FROM_SERVER, getCurrentServer());
MDC.put(TRACE_ID, StringHelper.join(traceToken, ":", fromServer));
}
@Override
public void onThreadEnd(HttpServletRequest request) {
MDC.remove(TRACE_ID);
}
}
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain chain) throws ServletException, IOException {
long startTime = System.currentTimeMillis();
// 从Header中装载传递过来的变量
Map<String, Object> tranVar = new HashMap<String, Object>();
Enumeration<String> headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
String key = headers.nextElement();
if (!StringUtils.isEmpty(key)
&& key.startsWith(ThreadLocalUtil.TRAN_PREFIX)) {
tranVar.put(key.substring(ThreadLocalUtil.TRAN_PREFIX.length()),
request.getHeader(key));
}
}
ThreadLocalHolder.begin(tranVar, request);
try {
if (isGateway) {
response.addHeader("X-TRACE-ID", TraceUtil.getTraceId());
}
// 检查RPC调用深度
checkRpcDeep(request, response);
// 业务处理
chain.doFilter(request, response);
// 记录RPC调用次数
logRpcCount(request, response);
} catch (Throwable ex) {
// 错误处理
Response<?> result = ExceptionUtil.toResponse(ex);
Determine determine = ExceptionUtil.determineType(ex);
ExceptionUtil.doLog(result, determine.getStatus(), ex);
response.setStatus(determine.getStatus().value());
response.setCharacterEncoding("UTF-8");
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.getWriter().write(JsonUtil.toJsonString(result));
} finally {
try {
doMonitor(request, response, startTime);
if (TraceUtil.isTraceLoggerOn()) {
log.warn(StringHelper.join(
"TRACE-HTTP-", request.getMethod(),
" URI:", request.getRequestURI(),
", dt:", System.currentTimeMillis() - startTime,
", rpc:", TraceUtil.getRpcCount(),
", status:", response.getStatus()));
} else if (log.isTraceEnabled()) {
log.trace(StringHelper.join(request.getMethod(),
" URI:", request.getRequestURI(),
", dt:", System.currentTimeMillis() - startTime,
", rpc:", TraceUtil.getRpcCount(),
", status:", response.getStatus()));
}
} finally {
ThreadLocalHolder.end(request);
}
}
}
往期推荐
码农架构
专注于系统架构、高可用、高性能、高并发类技术分享
Official Account
🧐分享、点赞、在看,给个3连击呗👇