mufengCc commented on issue #583:
URL:
https://github.com/apache/rocketmq-spring/issues/583#issuecomment-1704876301
写的不错,很有想法。
个人认为,作者没有实现扩展接口,可能另有考虑。
另外,如果要针对rocketMQ实现链路追踪,需要生产者在发送消息时,通过`properties`增加对应值。在消费者端,使用aop进行处理,也很方便
下面是我的代码示例
`@Slf4j
@Aspect
@Component
public class TraceAspect {
private static final String TRACE_ID = "traceId";
@SneakyThrows
@Around("@annotation(mqTraceID)")
public Object before2(ProceedingJoinPoint joinPoint, MqTraceID
mqTraceID) {
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
if (arg instanceof MessageExt messageExt) {
Map<String, String> properties = messageExt.getProperties();
String traceId = properties.get(TRACE_ID);
if (StringUtils.isBlank(traceId)) {
traceId = generateTraceId();
}
MDC.put(TRACE_ID, traceId);
break;
}
}
try {
return joinPoint.proceed();
} finally {
MDC.clear();
}
}
public static String generateTraceId() {
return UUID.randomUUID().toString().replace("-", "").toLowerCase();
}
}`
消费者
Component
@RocketMQMessageListener(topic = "test-topic", consumerGroup =
"test-consumer-group")
public class BuyRenewCloudDiskMq implements RocketMQListener<MessageExt>{
@MqTraceID
@SneakyThrows
@Override
public void onMessage(MessageExt messageExt) {
}
}
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]