dubbo traceID 实验8
This commit is contained in:
parent
9055a4c4ff
commit
318ab208be
|
|
@ -1 +1,2 @@
|
|||
traceIdFilter=com.czg.config.TraceIdFilter
|
||||
traceConsumerFilter=com.czg.config.TraceConsumerFilter
|
||||
traceProviderFilter=com.czg.config.TraceProviderFilter
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ pagehelper:
|
|||
|
||||
dubbo:
|
||||
provider:
|
||||
filter: traceIdFilter
|
||||
filter: traceProviderFilter
|
||||
consumer:
|
||||
filter: traceIdFilter
|
||||
filter: traceConsumerFilter
|
||||
check: false
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
traceIdFilter=com.czg.TraceIdFilter
|
||||
traceConsumerFilter=com.czg.config.TraceConsumerFilter
|
||||
traceProviderFilter=com.czg.config.TraceProviderFilter
|
||||
|
|
|
|||
|
|
@ -31,8 +31,5 @@ pagehelper:
|
|||
support-methods-arguments: true
|
||||
|
||||
dubbo:
|
||||
provider:
|
||||
filter: traceIdFilter
|
||||
consumer:
|
||||
filter: traceIdFilter
|
||||
check: false
|
||||
|
|
@ -1 +1,2 @@
|
|||
traceIdFilter=com.czg.config.TraceIdFilter
|
||||
traceConsumerFilter=com.czg.config.TraceConsumerFilter
|
||||
traceProviderFilter=com.czg.config.TraceProviderFilter
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
traceIdFilter=com.czg.config.TraceIdFilter
|
||||
traceConsumerFilter=com.czg.config.TraceConsumerFilter
|
||||
traceProviderFilter=com.czg.config.TraceProviderFilter
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
traceIdFilter=com.czg.config.TraceIdFilter
|
||||
traceConsumerFilter=com.czg.config.TraceConsumerFilter
|
||||
traceProviderFilter=com.czg.config.TraceProviderFilter
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
package com.czg.config;
|
||||
|
||||
import org.apache.dubbo.common.constants.CommonConstants;
|
||||
import org.apache.dubbo.common.extension.Activate;
|
||||
import org.apache.dubbo.rpc.*;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author ww
|
||||
* @description
|
||||
*/
|
||||
@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER})
|
||||
public class DubboTraceIdFilter implements Filter {
|
||||
// 定义 traceId 在 Dubbo 调用上下文的键
|
||||
private static final String TRACE_ID_KEY = "traceId";
|
||||
|
||||
@Override
|
||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||
// 1. 获取当前调用的上下文(消费端/服务端)
|
||||
RpcContext context = RpcContext.getContext();
|
||||
|
||||
try {
|
||||
// 2. 处理逻辑:
|
||||
// 若为消费端(发送请求前):生成或复用 traceId,设置到请求上下文
|
||||
if (context.isConsumerSide()) {
|
||||
String traceId = MDC.get(TRACE_ID_KEY);
|
||||
// 若 MDC 中无 traceId,则生成一个(如 UUID)
|
||||
if (traceId == null || traceId.isEmpty()) {
|
||||
traceId = UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
// 将 traceId 设置到 Dubbo 调用的附件(跨服务传递)
|
||||
context.setAttachment(TRACE_ID_KEY, traceId);
|
||||
// 设置到 MDC,供当前服务日志使用
|
||||
MDC.put(TRACE_ID_KEY, traceId);
|
||||
}
|
||||
|
||||
// 若为服务端(接收请求后):从请求附件中获取 traceId,设置到 MDC
|
||||
if (context.isProviderSide()) {
|
||||
String traceId = context.getAttachment(TRACE_ID_KEY);
|
||||
// 若消费端未传递,则生成一个(避免空值)
|
||||
if (traceId == null || traceId.isEmpty()) {
|
||||
traceId = UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
// 设置到 MDC,供当前服务日志使用
|
||||
MDC.put(TRACE_ID_KEY, traceId);
|
||||
}
|
||||
|
||||
// 3. 执行实际调用(继续链路)
|
||||
return invoker.invoke(invocation);
|
||||
|
||||
} finally {
|
||||
// 4. 清理 MDC(避免线程池复用导致的上下文污染)
|
||||
MDC.remove(TRACE_ID_KEY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package com.czg.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.dubbo.common.Constants;
|
||||
import org.apache.dubbo.common.extension.Activate;
|
||||
import org.apache.dubbo.rpc.*;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
/**
|
||||
* @author ww
|
||||
* @description
|
||||
*/
|
||||
@Activate(group = Constants.CONSUMER)
|
||||
public class TraceConsumerFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||
String traceId = MDC.get("traceId");
|
||||
if (StrUtil.isBlank(traceId)) {
|
||||
traceId = StrUtil.uuid();
|
||||
}
|
||||
invocation.setAttachment("traceId", traceId);
|
||||
return invoker.invoke(invocation);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package com.czg.config;
|
||||
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author ww
|
||||
* @description
|
||||
*/
|
||||
public class TraceContext {
|
||||
private static final String TRACE_ID = "traceId";
|
||||
|
||||
public static void setTraceId(String traceId) {
|
||||
MDC.put(TRACE_ID, traceId);
|
||||
}
|
||||
|
||||
public static String getTraceId() {
|
||||
return MDC.get(TRACE_ID);
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
MDC.clear();
|
||||
}
|
||||
|
||||
public static String generateTraceId() {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
package com.czg.config;
|
||||
|
||||
import org.apache.dubbo.common.constants.CommonConstants;
|
||||
import org.apache.dubbo.common.extension.Activate;
|
||||
import org.apache.dubbo.rpc.*;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author ww
|
||||
* @description
|
||||
*/
|
||||
@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER})
|
||||
public class TraceIdFilter implements Filter {
|
||||
// 定义 traceId 在 Dubbo 调用上下文的键
|
||||
private static final String TRACE_ID_KEY = "traceId";
|
||||
|
||||
@Override
|
||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||
// 1. 获取当前调用的上下文(消费端/服务端)
|
||||
RpcContext context = RpcContext.getContext();
|
||||
|
||||
try {
|
||||
// 2. 处理逻辑:
|
||||
// 若为消费端(发送请求前):生成或复用 traceId,设置到请求上下文
|
||||
if (context.isConsumerSide()) {
|
||||
String traceId = MDC.get(TRACE_ID_KEY);
|
||||
// 若 MDC 中无 traceId,则生成一个(如 UUID)
|
||||
if (traceId == null || traceId.isEmpty()) {
|
||||
traceId = UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
// 将 traceId 设置到 Dubbo 调用的附件(跨服务传递)
|
||||
context.setAttachment(TRACE_ID_KEY, traceId);
|
||||
// 设置到 MDC,供当前服务日志使用
|
||||
MDC.put(TRACE_ID_KEY, traceId);
|
||||
}
|
||||
|
||||
// 若为服务端(接收请求后):从请求附件中获取 traceId,设置到 MDC
|
||||
if (context.isProviderSide()) {
|
||||
String traceId = context.getAttachment(TRACE_ID_KEY);
|
||||
// 若消费端未传递,则生成一个(避免空值)
|
||||
if (traceId == null || traceId.isEmpty()) {
|
||||
traceId = UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
// 设置到 MDC,供当前服务日志使用
|
||||
MDC.put(TRACE_ID_KEY, traceId);
|
||||
}
|
||||
|
||||
// 3. 执行实际调用(继续链路)
|
||||
return invoker.invoke(invocation);
|
||||
|
||||
} finally {
|
||||
// 4. 清理 MDC(避免线程池复用导致的上下文污染)
|
||||
MDC.remove(TRACE_ID_KEY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.czg.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.apache.dubbo.rpc.*;
|
||||
|
||||
import com.alibaba.dubbo.common.Constants;
|
||||
import org.apache.dubbo.common.extension.Activate;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
/**
|
||||
* @author ww
|
||||
* @description
|
||||
*/
|
||||
@Activate(group = Constants.PROVIDER)
|
||||
public class TraceProviderFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||
String traceId = invocation.getAttachment("traceId");
|
||||
if (StrUtil.isBlank(traceId)) {
|
||||
traceId = StrUtil.uuid();
|
||||
}
|
||||
|
||||
// 设置到 MDC
|
||||
MDC.put("traceId", traceId);
|
||||
|
||||
try {
|
||||
return invoker.invoke(invocation);
|
||||
} finally {
|
||||
MDC.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue