简体   繁体   English

从Kafka消息在春季侦探中注入TraceId

[英]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.

相关问题 如何使用 opentelemetry 在 Spring sleuth 中设置自定义 traceId - How to set custom traceId in Spring sleuth with opentelemetry Spring Cloud Sleuth:将traceId传播到其他spring应用程序 - Spring Cloud Sleuth: Propagate traceId to other spring application Spring 侦探没有在 spring 调度程序内创建新的 traceid - Spring sleuth not creating new traceid inside a spring scheduler Spring 云侦探 3.0.1 使用 logback 在日志中生成 traceid 和 spanid - Spring cloud sleuth 3.0.1 generate traceid & spanid in logs using logback Spring Cloud Sleuth中每个新的HTTP请求都具有不同的traceId - different traceId every new http request in spring cloud sleuth Sleuth 不会在 Spring Boot 应用程序中打印 spanId、traceId - Sleuth doesnt print spanId,traceId in spring boot application 哪个记录 X-B3-SpanId 或 SpanId? X-B3-TraceId 还是 TraceId? (春天的侦探) - Which to log X-B3-SpanId or SpanId? X-B3-TraceId or TraceId? (Spring sleuth) Spring Interceptor 与 Spring sleuth 3.xx 的 afterCompletion 方法具有不同的 traceId - Spring Interceptor has different traceId for afterCompletion method with Spring sleuth 3.x.x Spring Cloud Sleuth v2.2.3 如何将 TraceId 传播到另一个线程 - Spring Cloud Sleuth v2.2.3 how to propagate TraceId to another thread Spring-Cloud-Sleuth 在模式布局中启用 MDC 属性以记录 TraceId - Spring-Cloud-Sleuth enable MDC properties in pattern layout to log TraceId
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM