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

Spring Boot 3 Webflux + Micrometer Tracing在Zipkin中追踪不连贯求助

Spring Boot 3 Webflux微服务追踪链路断裂问题排查

我有三个微服务,多数基于Spring Boot命令式方式构建,它们的追踪信息能正常在Zipkin面板展示。但用Spring Boot 3 + Webflux构建的微服务,在被其他服务发起HTTP请求调用时,相关追踪信息无法和调用方合并到同一链路中——Webflux服务的追踪被单独展示,其他微服务的追踪则同属一条链路。

提供的代码文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>service-abc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-6</name>
    <description>service-abc</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <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-brave</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.reporter2</groupId>
            <artifactId>zipkin-reporter-brave</artifactId>
        </dependency>
        <dependency>
            <groupId>io.opentelemetry</groupId>
            <artifactId>opentelemetry-exporter-zipkin</artifactId>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-instrumentation</artifactId>
            <version>3.1.8</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

配置文件(application.properties)

spring.application.name=service-abc
management.tracing.sampling.probability=1.0
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
server.port=5054
logging.level.org.springframework=debug

主类代码

package com.example.service6;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import reactor.core.publisher.Hooks;

@SpringBootApplication
public class Service6Application {

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

排查与修复步骤

1. 清理依赖冲突

Spring Boot 3已用Micrometer Tracing替代Sleuth,pom.xml中同时引入micrometer-tracing-bridge-brave和spring-cloud-sleuth-instrumentation会导致冲突,直接删除Sleuth依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-instrumentation</artifactId>
    <version>3.1.8</version>
    <scope>compile</scope>
</dependency>

同时删除opentelemetry-exporter-zipkin,因为使用Brave桥接只需要保留zipkin-reporter-brave。

2. 调整上下文传播启用时机

Hooks.enableAutomaticContextPropagation()需在应用启动前调用,否则部分初始化逻辑无法应用上下文传播,修改主类:

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

3. 补充追踪配置项

在Webflux服务的配置文件中添加以下配置,确保头信息解析和传递正常:

# 兼容Sleuth和Micrometer的追踪头格式
management.tracing.propagation.type=B3,W3C
# 开启Webflux请求追踪
spring.webflux.metrics.enabled=true

4. 验证调用方客户端配置

确保调用Webflux服务的命令式服务,其HTTP客户端(RestTemplate/WebClient)已被正确追踪 instrumentation:

  • RestTemplate:使用@LoadBalanced注解注入,或通过RestTemplateBuilder构建,Spring会自动添加追踪拦截器。
  • WebClient:使用WebClient.builder().build()创建实例,避免手动构建,确保Micrometer自动添加追踪头过滤器。

5. 检查追踪头传递

通过日志或调试工具,确认调用方请求携带X-B3-TraceId、X-B3-SpanId等B3格式头,且Webflux服务能正确接收这些头。若头未传递,说明调用方客户端未被正确 instrumentation。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 13:05:09