[英]Inject TraceId in spring sleuth from Kafka message
I am working on multiple microservice architecture ,where entry point is from Kafka. 我正在研究多个微服务架构,其中的切入点来自Kafka。 eg., lets suppose we have three microservice micro1,micro2 and micro3 request in micro1 comes from kafka message queue,and it further communicate with micro2 and micro3 via Rest client. 例如,假设我们在micro1中有三个微服务micro1,micro2和micro3请求来自kafka消息队列,并且它通过Rest客户端进一步与micro2和micro3通信。
Message which is recieved by micro1 contains requestId , which I need to put in place of TraceId in spring sleuth and that should be propogated across all microservice. micro1收到的消息包含requestId,我需要在春季侦探中将其替换为TraceId,并且应在所有微服务中传播该消息。
I have tried this by MDC but in that case traceId is not propogating to other microservices. 我已经通过MDC进行了尝试,但是在那种情况下traceId没有传播到其他微服务。
Is there any other way to implement custom TraceId in sleuth instead of Auto generated? 还有其他方法可以在侦探中实现自定义TraceId而不是自动生成吗?
thanks!! 谢谢!!
I don't think it's a good idea. 我认为这不是一个好主意。 Leave the generated trace id and create another generated field that will be propagated as baggage. 保留生成的跟踪ID,并创建另一个生成的字段,该字段将作为行李传播。
If you really need to change the way the ID is generated then you have to change this bean 如果您确实需要更改ID的生成方式,则必须更改此bean
https://github.com/spring-cloud/spring-cloud-sleuth/blob/v2.0.2.RELEASE/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceAutoConfiguration.java#L113-L133 https://github.com/spring-cloud/spring-cloud-sleuth/blob/v2.0.2.RELEASE/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/ TraceAutoConfiguration.java#L113-L133
@Bean
@ConditionalOnMissingBean
Propagation.Factory sleuthPropagation(SleuthProperties sleuthProperties) {
if (sleuthProperties.getBaggageKeys().isEmpty() && sleuthProperties.getPropagationKeys().isEmpty()) {
return B3Propagation.FACTORY;
}
ExtraFieldPropagation.FactoryBuilder factoryBuilder = ExtraFieldPropagation
.newFactoryBuilder(B3Propagation.FACTORY);
if (!sleuthProperties.getBaggageKeys().isEmpty()) {
factoryBuilder = factoryBuilder
// for HTTP
.addPrefixedFields("baggage-", sleuthProperties.getBaggageKeys())
// for messaging
.addPrefixedFields("baggage_", sleuthProperties.getBaggageKeys());
}
if (!sleuthProperties.getPropagationKeys().isEmpty()) {
for (String key : sleuthProperties.getPropagationKeys()) {
factoryBuilder = factoryBuilder.addField(key);
}
}
return factoryBuilder.build();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.