优化日志链路追踪代码
This commit is contained in:
@@ -57,5 +57,15 @@
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
<optional>true</optional>
|
||||
</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>
|
||||
</project>
|
||||
|
||||
+55
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
@@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+66
@@ -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();
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.central.log.trace;
|
||||
|
||||
import com.central.log.properties.TraceProperties;
|
||||
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 javax.annotation.Resource;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* web过滤器,生成日志链路追踪id,并赋值MDC
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2020/10/14
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@ConditionalOnClass(value = {HttpServletRequest.class, OncePerRequestFilter.class})
|
||||
@Order(value = MDCTraceUtils.FILTER_ORDER)
|
||||
public class WebTraceFilter extends OncePerRequestFilter {
|
||||
@Resource
|
||||
private TraceProperties traceProperties;
|
||||
|
||||
@Override
|
||||
protected boolean shouldNotFilter(HttpServletRequest request) {
|
||||
return !traceProperties.getEnable();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||
FilterChain filterChain) throws IOException, ServletException {
|
||||
try {
|
||||
String traceId = request.getHeader(MDCTraceUtils.TRACE_ID_HEADER);
|
||||
if (StringUtils.isEmpty(traceId)) {
|
||||
MDCTraceUtils.addTraceId();
|
||||
} else {
|
||||
MDCTraceUtils.putTraceId(traceId);
|
||||
}
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
MDCTraceUtils.removeTraceId();
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
logTrace=com.central.log.trace.DubboTraceFilter
|
||||
+3
-1
@@ -5,4 +5,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.log.config.LogAutoConfigure,\
|
||||
com.central.log.service.impl.LoggerAuditServiceImpl,\
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user