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

Spring Boot 3.1.2与Zipkin跨服务Trace ID不一致配置咨询

微服务全链路Trace ID不一致问题排查与修复建议

问题场景

使用Spring Boot 3.1.2 + Spring Cloud 2022.0.4搭建微服务集群,集成Eureka Server做服务注册发现、Zipkin做链路追踪时,调用customer服务生成的Trace ID与其他请求不统一,无法实现全链路的统一追踪。

当前配置信息

父模块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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.kemalyuksel</groupId>
    <artifactId>kemalyukselservices</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>kemalyukselservices</name>

    <modules>
        <module>customer</module>
        <module>fraud</module>
        <module>eureka-server</module>
        <module>clients</module>
        <module>notification</module>
    </modules>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <spring.boot.maven.plugin.version>3.1.2</spring.boot.maven.plugin.version>
        <spring.boot.dependencies.version>3.1.2</spring.boot.dependencies.version>
        <spring-cloud.version>2022.0.4</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.dependencies.version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring.boot.maven.plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

docker-compose.yml

services:
  postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: kemalyuksel
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"
    networks:
      - postgres
    restart: unless-stopped
  zipkin:
    image: openzipkin/zipkin
    container_name: zipkin
    ports:
      - 9411:9411
    networks:
      - zipkin

networks:
  postgres:
    driver: bridge
  spring:
    driver: bridge
  zipkin:
    driver: bridge

volumes:
  postgres:
  pgadmin:

customer子模块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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.kemalyuksel</groupId>
        <artifactId>kemalyukselservices</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>customer</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.reporter2</groupId>
            <artifactId>zipkin-sender-urlconnection</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.reporter2</groupId>
            <artifactId>zipkin-sender-okhttp3</artifactId>
        </dependency>

        <dependency>
            <groupId>com.kemalyuksel</groupId>
            <artifactId>clients</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

注意:当前提供的customer服务application.yml实际为notification服务配置

server:
  port: 8082

spring:
  application:
    name: notification
  datasource:
    password: password
    url: jdbc:postgresql://localhost:5432/notification
    username: kemalyuksel
  jpa:
    hibernate:
      ddl-auto: create-drop
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
    show-sql: true


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

management:
  tracing:
    sampling:
      probability: 1.0
    propagation:
      type: b3

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

修复建议

1. 修正服务配置文件的名称错误

将customer服务的application.yml中spring.application.name改为customer,避免服务标识混乱:

spring:
  application:
    name: customer

2. 统一所有微服务的链路追踪配置

所有服务(customer、fraud、notification等)必须保持一致的配置:

  • 确保添加micrometer-tracing-bridge-brave、zipkin-reporter-brave依赖
  • 统一设置management.tracing.propagation.type: b3
  • 开启全采样:management.tracing.sampling.probability: 1.0
  • 显式指定Zipkin地址:
spring:
  zipkin:
    base-url: http://localhost:9411

3. 确保Feign客户端的Trace ID传递正常

  • 所有Feign客户端接口使用@FeignClient注解并指定正确服务名
  • 若自定义Feign配置,不要覆盖默认的追踪拦截器,确保Trace ID能在服务调用间传递

4. 启用日志Trace ID打印

取消注释日志配置,方便排查Trace ID传递问题:

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

5. 排查依赖冲突

通过mvn dependency:tree命令检查所有服务的依赖树,确保micrometer-tracing、brave等链路追踪相关包版本统一,排除冲突依赖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 06:24:52