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

Spring Boot 3中KafkaTemplate追踪(traceId、spanId、baggage)缺失求助

Spring Boot 3 下 Kafka 生产者链路追踪配置方案

在Spring Boot 3之前,Kafka链路追踪(传递traceId、spanId、baggage等上下文)由Spring Cloud Sleuth实现,通过KafkaFactoryBeanPostProcessor、TraceProducerPostProcessor、TracingConsumerPostProcessor这类后置处理器自动完成配置。但Sleuth被移除后,Spring Boot接管链路追踪功能时,Kafka生产者端的追踪支持并未自动集成,需要手动配置。

解决步骤:

  1. 引入必要依赖
    需将brave-instrumentation-kafka-clients添加到项目类路径中:

    • Maven配置:
      <dependency>
          <groupId>io.zipkin.brave</groupId>
          <artifactId>brave-instrumentation-kafka-clients</artifactId>
      </dependency>
      
    • Gradle配置:
      implementation 'io.zipkin.brave:brave-instrumentation-kafka-clients'
      
  2. 手动配置ProducerFactory
    创建自定义的ProducerFactory Bean,通过Brave的KafkaTracing为生产者添加追踪拦截器:

    @Bean
    public <K, V> ProducerFactory<K, V> kafkaProducerFactory(KafkaProperties kafkaProperties, ObjectProvider<Tracing> tracing) {
        final var factory = new DefaultKafkaProducerFactory<>(kafkaProperties.buildProducerProperties());
        tracing.ifAvailable(t -> {
            final var kafkaTracing = KafkaTracing.create(t);
            factory.addPostProcessor(kafkaTracing::producer);
        });
        return factory;
    }
    

配置完成后,项目中的KafkaTemplate会自动使用这个带有追踪能力的ProducerFactory,实现和原Sleuth一致的Kafka链路追踪上下文传递效果。

内容的提问来源于stack exchange,提问作者François Rosière

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 11:43:17