优化日志链路追踪代码

This commit is contained in:
zlt2000
2021-01-28 21:07:27 +08:00
parent f2430b7b6a
commit 0c85184e1f
9 changed files with 191 additions and 26 deletions
@@ -1,3 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.central.common.filter.TenantFilter,\ com.central.common.filter.TenantFilter
com.central.common.filter.TraceFilter
@@ -57,5 +57,15 @@
<artifactId>spring-boot-starter-jdbc</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>
</project> </project>
@@ -0,0 +1,55 @@
package com.central.log.trace;
import com.central.log.properties.TraceProperties;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
/**
* dubbo过滤器,传递traceId
*
* @author zlt
* @date 2020/10/14
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER}, order = MDCTraceUtils.FILTER_ORDER)
public class DubboTraceFilter implements Filter {
@Resource
private TraceProperties traceProperties;
/**
* 服务消费者:传递traceId给下游服务
* 服务提供者:获取traceId并赋值给MDC
*/
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
boolean isProviderSide = RpcContext.getContext().isProviderSide();
if (traceProperties.getEnable()) {
if (isProviderSide) { //服务提供者逻辑
String traceId = invocation.getAttachment(MDCTraceUtils.KEY_TRACE_ID);
if (!StringUtils.isEmpty(traceId)) {
MDCTraceUtils.addTraceId();
} else {
MDCTraceUtils.putTraceId(traceId);
}
} else { //服务消费者逻辑
String traceId = MDCTraceUtils.getTraceId();
if (!StringUtils.isEmpty(traceId)) {
invocation.setAttachment(MDCTraceUtils.KEY_TRACE_ID, traceId);
}
}
}
try {
return invoker.invoke(invocation);
} finally {
if (isProviderSide && traceProperties.getEnable()) {
MDCTraceUtils.removeTraceId();
}
}
}
}
@@ -0,0 +1,37 @@
package com.central.log.trace;
import com.central.log.properties.TraceProperties;
import feign.RequestInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
/**
* feign拦截器,传递traceId
*
* @author zlt
* @date 2021/1/28
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@ConditionalOnClass(value = {RequestInterceptor.class})
public class FeignTraceInterceptor {
@Resource
private TraceProperties traceProperties;
@Bean
public RequestInterceptor feignTraceInterceptor() {
return template -> {
if (traceProperties.getEnable()) {
//传递日志traceId
String traceId = MDCTraceUtils.getTraceId();
if (!StringUtils.isEmpty(traceId)) {
template.header(MDCTraceUtils.TRACE_ID_HEADER, traceId);
}
}
};
}
}
@@ -0,0 +1,66 @@
package com.central.log.trace;
import org.slf4j.MDC;
import java.util.UUID;
/**
* 日志追踪工具类
*
* @author zlt
* @date 2020/10/14
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
public class MDCTraceUtils {
/**
* 追踪id的名称
*/
public static final String KEY_TRACE_ID = "traceId";
/**
* 日志链路追踪id信息头
*/
public static final String TRACE_ID_HEADER = "x-traceId-header";
/**
* filter的优先级,值越低越优先
*/
public static final int FILTER_ORDER = -1;
/**
* 创建traceId并赋值MDC
*/
public static void addTraceId() {
MDC.put(KEY_TRACE_ID, createTraceId());
}
/**
* 赋值MDC
*/
public static void putTraceId(String traceId) {
MDC.put(KEY_TRACE_ID, traceId);
}
/**
* 获取MDC中的traceId值
*/
public static String getTraceId() {
return MDC.get(KEY_TRACE_ID);
}
/**
* 清除MDC的值
*/
public static void removeTraceId() {
MDC.remove(KEY_TRACE_ID);
}
/**
* 创建traceId
*/
public static String createTraceId() {
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}
}
@@ -1,14 +1,12 @@
package com.central.common.filter; package com.central.log.trace;
import cn.hutool.core.util.StrUtil;
import com.central.common.constant.CommonConstant;
import com.central.log.properties.TraceProperties; import com.central.log.properties.TraceProperties;
import org.slf4j.MDC;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@@ -16,13 +14,17 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
/** /**
* 日志链路追踪过滤器 * web过滤器,生成日志链路追踪id,并赋值MDC
* *
* @author zlt * @author zlt
* @date 2019/9/15 * @date 2020/10/14
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/ */
@ConditionalOnClass(Filter.class) @ConditionalOnClass(value = {HttpServletRequest.class, OncePerRequestFilter.class})
public class TraceFilter extends OncePerRequestFilter { @Order(value = MDCTraceUtils.FILTER_ORDER)
public class WebTraceFilter extends OncePerRequestFilter {
@Resource @Resource
private TraceProperties traceProperties; private TraceProperties traceProperties;
@@ -35,14 +37,15 @@ public class TraceFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws IOException, ServletException { FilterChain filterChain) throws IOException, ServletException {
try { try {
String traceId = request.getHeader(CommonConstant.TRACE_ID_HEADER); String traceId = request.getHeader(MDCTraceUtils.TRACE_ID_HEADER);
if (StrUtil.isNotEmpty(traceId)) { if (StringUtils.isEmpty(traceId)) {
MDC.put(CommonConstant.LOG_TRACE_ID, traceId); MDCTraceUtils.addTraceId();
} else {
MDCTraceUtils.putTraceId(traceId);
} }
filterChain.doFilter(request, response); filterChain.doFilter(request, response);
} finally { } finally {
MDC.remove(CommonConstant.LOG_TRACE_ID); MDCTraceUtils.removeTraceId();
} }
} }
} }
@@ -0,0 +1 @@
logTrace=com.central.log.trace.DubboTraceFilter
@@ -5,4 +5,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.central.log.config.LogAutoConfigure,\ com.central.log.config.LogAutoConfigure,\
com.central.log.service.impl.LoggerAuditServiceImpl,\ com.central.log.service.impl.LoggerAuditServiceImpl,\
com.central.log.service.impl.DbAuditServiceImpl,\ com.central.log.service.impl.DbAuditServiceImpl,\
com.central.log.aspect.AuditLogAspect com.central.log.aspect.AuditLogAspect,\
com.central.log.trace.WebTraceFilter,\
com.central.log.trace.FeignTraceInterceptor
@@ -1,11 +1,9 @@
package com.central.common.ribbon.config; package com.central.common.ribbon.config;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.central.common.constant.CommonConstant;
import com.central.common.constant.SecurityConstants; import com.central.common.constant.SecurityConstants;
import com.central.common.context.TenantContextHolder; import com.central.common.context.TenantContextHolder;
import feign.RequestInterceptor; import feign.RequestInterceptor;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
/** /**
@@ -15,7 +13,7 @@ import org.springframework.context.annotation.Bean;
*/ */
public class FeignInterceptorConfig { public class FeignInterceptorConfig {
/** /**
* 使用feign client访问别的微服务时,将上游传过来的client、traceid等信息放入header传递给下一个服务 * 使用feign client访问别的微服务时,将上游传过来的client等信息放入header传递给下一个服务
*/ */
@Bean @Bean
public RequestInterceptor baseFeignInterceptor() { public RequestInterceptor baseFeignInterceptor() {
@@ -25,12 +23,6 @@ public class FeignInterceptorConfig {
if (StrUtil.isNotEmpty(tenant)) { if (StrUtil.isNotEmpty(tenant)) {
template.header(SecurityConstants.TENANT_HEADER, tenant); template.header(SecurityConstants.TENANT_HEADER, tenant);
} }
//传递日志traceId
String traceId = MDC.get(CommonConstant.LOG_TRACE_ID);
if (StrUtil.isNotEmpty(traceId)) {
template.header(CommonConstant.TRACE_ID_HEADER, traceId);
}
}; };
} }
} }