BaggageField 的值在更新后返回 null。

huangapple go评论108阅读模式
英文:

BaggageField value returns null after update

问题

以下是您提供的内容的中文翻译:

我有以下的Spring Cloud Sleuth配置:

application.yml 文件:

sleuth:
  baggage:
    remote-fields: userId
    correlation-fields: userId

配置类:

@Configuration
public class SleuthConfiguration {

    @Bean
    public BaggageField userIdBaggageField() {
        return BaggageField.create("userId");
    }

    @Bean
    public CurrentTraceContext.ScopeDecorator mdcScopeDecorator() {
        return MDCScopeDecorator.newBuilder()
            .clear()
            .add(CorrelationScopeConfig.SingleCorrelationField
                .newBuilder(userIdBaggageField())
                .flushOnUpdate()
                .build())
            .build();
    }
}

当在控制器中更新BaggageField时,getValue() 方法始终返回 null,尽管我配置了相应的BaggageField以在更新时刷新(.flushOnupdate() 文档):

@RestController
@Slf4j
static class SampleController {

    private final BaggageField userIdBaggageField;

    SampleController(BaggageField userIdBaggageField) {
        this.userIdBaggageField = userIdBaggageField;
    }

    @GetMapping("/sample")
    public void periodes(@RequestParam String userId) {
        log.info("设置BaggageField为 {}", userId);
        this.userIdBaggageField.updateValue(userId);
        log.info("BaggageField的值为 {}", this.userIdBaggageField.getValue());
    }
}

日志:

设置BaggageField为 1234
BaggageField的值为 null

我可能错过了什么?谢谢。

英文:

I have the following Spring Cloud Sleuth configuration:

application.yml file:

  sleuth:
    baggage:
      remote-fields: userId
      correlation-fields: userId

Configuration class:

@Configuration
public class SleuthConfiguration {

    @Bean
    public BaggageField userIdBaggageField() {
        return BaggageField.create("userId");
    }

    @Bean
    public CurrentTraceContext.ScopeDecorator mdcScopeDecorator() {
        return MDCScopeDecorator.newBuilder()
            .clear()
            .add(CorrelationScopeConfig.SingleCorrelationField
                .newBuilder(userIdBaggageField())
                .flushOnUpdate()
                .build())
            .build();
    }
}

When updating the BaggageField in the controller, the getValue() method always returns null, although I configured the corresponding BaggageField to flush on update (.flushOnupdate() documentation):

@RestController
@Slf4j
static class SampleController {

    private final BaggageField userIdBaggageField;

    SampleController(BaggageField userIdBaggageField) {
        this.userIdBaggageField = userIdBaggageField;
    }

    @GetMapping("/sample")
    public void periodes(@RequestParam String userId) {
        log.info("Setting BaggageField to {}", userId);
        this.userIdBaggageField.updateValue(userId);
        log.info("BaggageField value {}", this.userIdBaggageField.getValue());
    }
}

Logs:

Setting BaggageField to 1234
BaggageField value null

What did I possibly miss?

Thanks.

答案1

得分: 0

问题是由于配置的自定义传播器干扰默认的行李传播而引起的。

移除它会修复这个问题。

英文:

The issue was due to a configured custom propagator interfering with default baggage propagation.

Removing it fixes this issue.

huangapple
  • 本文由 发表于 2023年3月7日 00:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定