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

Sleuth 3.1.8+Spring Boot 2.7.11自定义B3 TraceID不生效求助

问题排查与解决方案

1. 先检查B3请求头格式是否合规

Sleuth对B3系列头的格式有严格要求,不符合就会直接忽略并生成新ID:

  • x-b3-traceid:必须是32位十六进制字符串(16位也兼容,但推荐用32位)
  • x-b3-spanid:必须是16位十六进制字符串
  • x-b3-parentspanid:可选,格式和spanid一致

举个正确的示例:

x-b3-traceid: 463ac35c9f6413ad48485a3953bb6124
x-b3-spanid: 0021683323b1e250
x-b3-parentspanid: 7110be7672c5bc1b

2. 确认Sleuth传播配置是否为B3模式

Spring Boot 2.7.x + Sleuth 3.1.x默认启用B3传播,但如果配置被覆盖就会失效,检查配置文件:

  • YAML格式:
spring:
  sleuth:
    propagation:
      type: B3
  • Properties格式:
spring.sleuth.propagation.type=B3

要是配置成了W3C之类的其他类型,自然会忽略B3头。

3. 排查代理/网关是否拦截了请求头

如果API前面有Nginx、Spring Cloud Gateway这类代理,可能会过滤自定义头,得确保代理允许传递x-b3-*系列头:
比如Nginx要加这段配置:

proxy_set_header x-b3-traceid $http_x_b3_traceid;
proxy_set_header x-b3-spanid $http_x_b3_spanid;
proxy_set_header x-b3-parentspanid $http_x_b3_parentspanid;

4. 验证请求头是否真的传到后端

在你的API接口里打印所有请求头,确认x-b3-*头是否存在:

@GetMapping("/your-api-path")
public String testTrace(HttpServletRequest request) {
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String name = headerNames.nextElement();
        System.out.println(name + ": " + request.getHeader(name));
    }
    return "success";
}

要是打印结果里没有这些头,说明Postman的请求头没发送成功,检查拼写(HTTP头大小写不敏感,但尽量统一小写)。

5. 确认Sleuth依赖完整

检查Maven或Gradle依赖是否正确,有没有缺失:

  • Maven:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
    <version>3.1.8</version>
</dependency>
  • Gradle:
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.1.8'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 12:30:03