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

Spring Boot 3.0.0版本下Sleuth无法生成TraceID/SpanID问题求助

Spring Boot 3.0.0 下 Sleuth/Zipkin 无法打印TraceID/SpanID及数据不传递问题解决

1. 核心问题:版本不兼容

Spring Boot 3.0.0 对应的 Spring Cloud 版本是 2022.0.x(代号Kilburn),你当前使用的2021.0.4(Jubilee)仅适配Spring Boot 2.7.x系列,版本不匹配会直接导致Sleuth核心追踪功能失效。

2. 依赖调整(替换Sleuth为Micrometer Tracing)

Spring Boot 3.x开始,Spring Cloud Sleuth进入维护模式,官方推荐使用Micrometer Tracing替代,搭配Brave实现Zipkin链路追踪。修改你的项目依赖:

Maven示例:

<!-- 移除旧的spring-cloud-starter-sleuth依赖 -->
<!-- <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency> -->

<!-- 添加Micrometer Tracing和Zipkin依赖 -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

Gradle示例:

// 移除旧依赖
// implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'

// 添加新依赖
implementation 'io.micrometer:micrometer-tracing-bridge-brave'
implementation 'org.springframework.cloud:spring-cloud-starter-zipkin'

同时升级Spring Cloud版本到2022.0.x,比如在Maven的dependencyManagement中配置:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2022.0.4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3. 日志配置:打印TraceID和SpanID

在application.properties或logback-spring.xml中配置日志格式,加入traceId和spanId变量:

方式1:application.properties配置

logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]

方式2:logback-spring.xml配置

在日志编码器的pattern中添加追踪ID字段:

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [${spring.application.name},%X{traceId:-},%X{spanId:-}] --- [%t] %c{1} : %m%n</pattern>
    </encoder>
</appender>

4. 验证Zipkin数据传递

你的现有application.properties配置在Spring Boot 3.x下仍兼容,保持不变即可:

spring.application.name=sleuthpoc2
spring.zipkin.base-url=http://localhost:9411/
spring.sleuth.sampler.probability=1.0
spring.zipkin.sender.type=WEB

建议将Zipkin Server升级到2.24.x及以上稳定版本,避免潜在的兼容性问题。

5. 额外检查项

  • 确保项目使用Java 17及以上版本(Spring Boot 3.x强制要求);
  • 微服务间调用必须通过Spring Cloud封装的客户端(RestTemplate/WebClient/OpenFeign),这些客户端会自动注入追踪上下文;
  • 若使用自定义HTTP客户端,需手动添加Brave的Tracing拦截器来传递TraceID。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 05:46:09