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

ApplicationReadyEvent监听器中Baggage字段未传播问题

问题解决:Spring Boot WebClient 未传播 Baggage 字段

问题描述

基于Spring Boot 3.4.2开发的演示应用,在ApplicationReadyEvent监听方法中通过BaggageInScope设置自定义Baggage字段baggage1,并使用WebClient发起请求,预期请求会自动携带baggage1头部,但实际未收到该字段。

应用代码:

@SpringBootApplication
@RestController
public class BaggageTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(BaggageTestApplication.class, args);
    }

    private static final Log LOGGER = LogFactory.getLog(BaggageTestApplication.class);

    private final WebClient webClient;
    private final Tracer tracer;

    public BaggageTestApplication(WebClient webClient, Tracer tracer) {
        this.webClient = webClient;
        this.tracer = tracer;
    }

    @RequestMapping("/")
    ResponseEntity<Void> home(@RequestHeader Map<String, String> headers) {
        headers.forEach((key, value) -> LOGGER.info(key + ": " + value));
        return ResponseEntity.ok().build();
    }

    @EventListener
    public void onApplicationEvent(ApplicationReadyEvent event) {
        try (BaggageInScope scope = this.tracer.createBaggageInScope("baggage1", "value1")) {
            webClient.get()
                    .retrieve()
                    .toBodilessEntity()
                    .block();
        }
    }
}

@Configuration
class WebclientConfiguration {
    @Bean
    public WebClient webClient(WebClient.Builder builder) {
        return builder.
                baseUrl("http://localhost:8080")
                .build();
    }
}

配置文件application.yml:

spring:
  application:
    name: baggage-test
management:
  tracing:
    sampling:
      probability: 1.0
    baggage:
      remote-fields: baggage1
      enabled: true
    enabled: true

核心原因

Baggage的传播依赖活跃的Span上下文,而ApplicationReadyEvent监听方法执行时,当前线程默认没有活跃的Trace Span。此时设置的Baggage无法关联到任何Span,导致WebClient的Tracing Instrumentation无法识别并传播该字段。

解决方案

1. 在监听方法中创建并激活Span

修改onApplicationEvent方法,手动创建一个Span并激活,确保Baggage能关联到有效的Trace上下文:

@EventListener
public void onApplicationEvent(ApplicationReadyEvent event) {
    // 创建并启动一个新的Span
    Span span = tracer.nextSpan().name("application-ready-request").start();
    try (Tracer.SpanInScope spanScope = tracer.withSpan(span);
         BaggageInScope scope = this.tracer.createBaggageInScope("baggage1", "value1")) {
        webClient.get()
                .retrieve()
                .toBodilessEntity()
                .block();
    } finally {
        // 结束Span
        span.end();
    }
}

2. 确保依赖配置正确

确认项目已引入必要的Tracing依赖(以OpenTelemetry桥接为例):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>

3. 验证配置有效性

management.tracing.baggage.remote-fields=baggage1配置已正确声明需要远程传播的Baggage字段,无需额外修改。

验证结果

启动应用后,home方法的日志会打印出baggage1: value1的请求头部,说明Baggage已成功传播。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 10:10:06