You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Camel使用producerTemplate时Zipkin初始trace id无法透传问题咨询

解决方案

1. 补全ZipkinTracer核心配置

你当前的配置缺少了B3头读取和链路传播的开关,这是手动设置头被覆盖的核心原因,修改配置如下:

@Bean
@ConditionalOnMissingBean(ZipkinTracer.class)
ZipkinTracer zipkin(final CamelContext camelContext) {
    ZipkinTracer zipkin = new ZipkinTracer();
    zipkin.setPort(9411);
    zipkin.setHostName("127.0.0.1");
    zipkin.setRate(1f);
    zipkin.setEndpoint("http://localhost:9411/api/v2/spans");
    // 新增配置:开启B3头读取,优先从消息头获取链路信息而非生成新的
    zipkin.setUseB3Headers(true);
    // 新增配置:开启跨路由/跨组件的链路传播
    zipkin.setPropagationEnabled(true);
    OkHttpSender sender = OkHttpSender.create("http://localhost:9411/api/v2/spans");
    zipkin.setSpanReporter(AsyncReporter.create(sender));
    camelContext.setTracing(true);
    zipkin.init(camelContext);
    return zipkin;
}

2. 构造Exchange时透传完整B3链路头

仅设置X-B3-TraceId不符合B3协议规范,Camel会判定为无效链路信息直接覆盖,需要同时透传以下4个核心头:

  • X-B3-TraceId:全局链路ID
  • X-B3-SpanId:当前父Span的ID
  • X-B3-ParentSpanId:父Span的上级Span ID(如果有)
  • X-B3-Sampled:采样标记(值为0或1)
    你可以直接从当前请求的链路上下文批量获取头,不需要手动拼接,示例代码如下:
// 从当前活跃Span中获取完整B3头
Span currentSpan = tracer.currentSpan();
if (currentSpan != null) {
    TraceContext context = currentSpan.context();
    exchange.getIn().setHeader("X-B3-TraceId", context.traceIdString());
    exchange.getIn().setHeader("X-B3-SpanId", context.spanIdString());
    exchange.getIn().setHeader("X-B3-Sampled", context.sampled() ? "1" : "0");
}
producerTemplate.send(startEndpoint, exchange);

3. 配置队列组件透传自定义头

你使用的队列组件(JMS/RabbitMQ/Kafka等)默认会过滤非标准头,需要单独开启自定义头透传配置:

  • 若使用JMS/ActiveMQ组件:在endpoint参数中添加?mapJmsMessage=true,允许自定义Header透传
  • 若使用RabbitMQ组件:在endpoint参数中添加headerFilterStrategy=#allowB3HeaderFilter,自定义过滤规则放行B3开头的头
  • 若使用Kafka组件:配置headerSerializer支持字符串类型头传输

内容的提问来源于stack exchange,提问作者Justas Tvarijonas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 15:24:02