【问题标题】:spring sleuth baggage propagation not working春季侦探行李传播不起作用
【发布时间】:2023-04-06 17:22:02
【问题描述】:

我第一次使用 Sleuth 需要一些帮助。我目前正在使用侦探 2.2.3.RELEASE。 我的要求是我想传播 2 个字段产品 id 和产品类型,以便我可以从其他微服务中读取这两个值,因为我正在使用行李传播。

1. In one of the microservice in rest API my code is something like this,
BaggageField PRODUCT_ID = BaggageField.create("x-df-product-id");
PRODUCT_ID.updateValue(productId); // I have value of productId that value I want to propogate

BaggageField PRODUCT_TYPE = BaggageField.create("x-df-product-type");
PRODUCT_TYPE.updateValue(productType); // I have value of productType that value I want to propogate

Tracing.newBuilder().propagationFactory(
            BaggagePropagation.newFactoryBuilder(B3Propagation.FACTORY)
            .add(SingleBaggageField.remote(PRODUCT_ID))
            .add(SingleBaggageField.remote(PRODUCT_TYPE))
            .build());

In another microservice, I am reading the values like this:

String baggageProductId=BaggageField.getAllValues().entrySet().stream.filter(e ->"PRODUCT_ID".equalIgnoreCase(e.getKey())).map(Map.Entry::getValue).findFirst().orElse(null);

String baggageProducType=BaggageField.getAllValues().entrySet().stream.filter(e ->"PRODUCT_TYPE".equalIgnoreCase(e.getKey())).map(Map.Entry::getValue).findFirst().orElse(null);

in application.yml these are the entries:
sleuth:
 baggage:
   remoteFields:
    -x-df-product-id
    -x-df-product-type

but I am getting both values baggageProductId and baggageProducType as null.

I am not using span anywhere is that a problem? not sure how can I read these values. can you please help me?

【问题讨论】:

    标签: spring spring-cloud spring-cloud-sleuth


    【解决方案1】:

    更新: 我像这样更新了我的代码,它工作正常,但是......需要一些帮助。

    1. application.yml 更改为

    侦探: 行李: 行李钥匙:

    -x-df-product-id
    
    -x-df-product-type
    

    log.slf4.whitelisted-mdc-keys:

    -x-df-product-id
    
    -x-df-product-type
    

    2.在 Microservice-1 中我有以下代码

    BaggageField baggageFieldProductId = BaggageField.getByName("x-df-product-id"); baggageFieldProductId.getValue();

    BaggageField baggageFieldProductType = BaggageField.getByName("x-df-product-type"); baggageProductType=baggageFieldProductType.getValue();

    我在每个微服务的每个 application.yml 文件中插入了相同的条目。 之后,我可以在微服务 1、微服务 2、微服务 3 中检索这些值 但在微服务 4 中获得空值。不知道为什么?代码检索的逻辑到处都是一样的。

    唯一的区别是我的微服务 1,2,3 spring boot 版本是 2.2.8 而微服务 4 spring boot 版本是 2.3.6。

    需要帮助。真的不知道为什么我得到空值。谢谢!!!

    【讨论】:

      【解决方案2】:

      你必须在你applicaiton.properties进行配置

      spring.sleuth.propagation-keys[0]= x-df-product-id
      spring.sleuth.propagation.tag.whitelisted-keys[0]=x-df-product-id
      spring.sleuth.log.slf4j.whitelisted-mdc-keys[0]= x-df-product-id
      

      此源代码 org.springframework.cloud.sleuth.autoconfig.TraceBaggageConfiguration

      【讨论】:

        猜你喜欢
        • 2020-10-18
        • 2021-11-03
        • 2023-02-14
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-22
        • 2015-02-27
        相关资源
        最近更新 更多