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

Spring Boot集成Apache Camel无法向Zipkin上报完整调用链路

Spring Boot + Apache Camel 集成Zipkin无法展示完整调用链路的解决方案

问题分析

你的配置存在几个核心问题:依赖版本不兼容、Camel 4.x与Spring Boot 3.x的追踪集成方式错误、HTTP调用未正确传递追踪上下文,导致Zipkin无法串联Camel到下游微服务的完整链路。

具体修正步骤

1. 修复依赖版本不兼容问题

camel-zipkin-starter版本(3.22.2)与全局Camel版本(4.6.0)严重不匹配,会导致追踪组件初始化失败或功能异常。同时需对齐Micrometer与Spring Boot的版本依赖:

修改pom.xml中的相关依赖:

<!-- 移除camel-zipkin-starter的手动版本,由dependencyManagement统一管理 -->
<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-zipkin-starter</artifactId>
</dependency>
<!-- 替换为Spring Boot管理的追踪依赖,避免版本冲突 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tracing</artifactId>
</dependency>
<dependency>
    <groupId>io.zipkin.reporter2</groupId>
    <artifactId>zipkin-reporter-brave</artifactId>
</dependency>

2. 调整配置文件适配Camel 4.x追踪逻辑

Camel 4.x默认使用Micrometer Tracing而非旧版Zipkin组件,更新application.yml配置:

spring:
  application.name: ecommerce-integrations
  zipkin:
    base-url: http://localhost:9411  # 直接配置Spring Boot Zipkin服务地址
server:
  port: 8080
camel:
  servlet:
    mapping:
      context-path: /rest/ecommerce/api/v1/*
  springboot:
    tracing: true  # 启用Camel与Spring追踪体系的集成
  tracing:
    enabled: true
    propagation: w3c  # 使用W3C标准的追踪上下文传递(兼容B3格式)
ecommerce:
  product-service:
    port: 9092
    host: localhost
    url: http://${ecommerce.product-service.host}:${ecommerce.product-service.port}/ecommerce/api/v1/products
  category-service:
    port: 9091
    host: localhost
    url: http://${ecommerce.category-service.host}:${ecommerce.category-service.port}/ecommerce/api/v1/categories
logging:
  level:
    org.apache.camel: DEBUG
    io.micrometer.tracing: DEBUG
    brave: DEBUG
management:
  tracing:
    sampling:
      probability: 1.0  # 全量采样便于调试
    enabled: true

3. 确保HTTP调用传递追踪上下文

你的HTTP调用使用bridgeEndpoint=true会过滤Camel自动添加的追踪上下文Header,需调整路由配置:

@Component
public class FindAllCategoriesRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        
        from("direct:FindAllCategoriesRoute")
        .doTry()
            .routeId("FindAllCategoriesRoute")
            .to("direct:LogRequestRoute")
            .log("Before Calling Backend ==> BODY = ${body} , HEADERS = ${headers}")
            .setHeader(Exchange.HTTP_METHOD, constant(HttpMethod.GET))
            // 移除bridgeEndpoint=true,或添加transferExchange=true确保Header传递
            .toD("{{ecommerce.category-service.url}}")
            .log("After Save Category ==> ${headers.id} ==> BODY = ${body} , HEADERS = ${headers}")
            .to("direct:LogResponseRoute")
        .doCatch(Exception.class)
            .to("direct:RestExceptionHandlingRoute")
        .end();
    }
}

若必须保留bridgeEndpoint=true,需添加transferExchange=true参数确保追踪Header被传递:

.toD("{{ecommerce.category-service.url}}?bridgeEndpoint=true&transferExchange=true")

4. 下游微服务配置检查

确保下游微服务(product-service、category-service)同样配置了Spring Boot追踪和Zipkin依赖,并启用追踪功能,这样它们才能接收并上报追踪数据到Zipkin,形成完整链路。

验证步骤

  1. 启动Zipkin服务(确保端口9411正常运行)
  2. 启动修改后的集成服务和下游微服务
  3. 发起请求触发Camel路由
  4. 访问Zipkin UI(http://localhost:9411),搜索ecommerce-integrations服务,查看完整调用链路

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 07:39:53