传递自定义 headers 和 Sleuth X-B3* headers 的最佳方式

Best way to pass custom headers along with Sleuth X-B3* headers

根据 README here ,我使用以下配置将 x-vcap-request-idx-vcap-group-id 从一个应用程序传递到另一个应用程序。

@Bean
public Factory propagationFactory() {
    return brave.propagation.ExtraFieldPropagation.newFactory(brave.propagation.B3Propagation.FACTORY,
            "x-vcap-request-id", "x-vcap-group-id");
}

@Bean
public TracingFactoryBean tracing() {
    TracingFactoryBean tracingFactoryBean = new TracingFactoryBean();
    tracingFactoryBean.setPropagationFactory(propagationFactory());
    return tracingFactoryBean;
}

但是,此配置扰乱了默认侦探行为。使用此代码,Sleuth 不再将 TraceIdSpanId 添加到日志

在微服务之间传递自定义 headers 的 best/recommended 方法是什么?

如果您阅读 docs on prefixed fields,您将看到以下部分

A difference from previous versions of Sleuth is that, with Brave, you must pass the list of baggage keys. There are two properties to achieve this. With the spring.sleuth.baggage-keys, you set keys that get prefixed with baggage- for HTTP calls and baggage_ for messaging. You can also use the spring.sleuth.propagation-keys property to pass a list of prefixed keys that are whitelisted without any prefix.

只需设置属性,即可立即使用。阅读项目文档总是好的。

我没用过TracingFactoryBean,但是在TracingBuilder中添加Slf4jCurrentTraceContext帮我解决了。

return Tracing.newBuilder().propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "request-id"))
        .currentTraceContext(Slf4jCurrentTraceContext.create())
        .build();