首页 > 原创文章 > 软件开发 > 查看文章

关于使用Zipkin与RabbitMQ的追踪集成

所属分类:软件开发 来源: 丁老师原创 更新时间:2025-10-09 08:27 浏览: 2566 IP属地: 深圳

有同学提问,使用的Spring Boot框架,有两个微服务,为了实现追踪,使用了<artifactId>spring-cloud-sleuth-zipkin</artifactId>以及zipkin。A服务是生产者,通过RabbitMQ broker发送消息。B服务是消费者,使用了@RabbitListener。想实现将traceId(包含span详情)从A服务传递到B服务,但无法将zipkin与RabbitMQ集成并实现追踪传播。这个该怎么实现呢?

要实现Spring Boot微服务通过RabbitMQ传递Zipkin追踪信息(traceId、spanId等),需要正确配置Sleuth和Zipkin,并在消息中携带追踪上下文。

以下是详细步骤:

1. 添加依赖
在两个服务的pom.xml中添加以下依赖:

<!-- Spring Cloud Sleuth + Zipkin -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

<!-- RabbitMQ -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2. 配置文件
A服务(生产者)和B服务(消费者)的application.yml配置相同:

spring:
    application:
        name: 服务名# A服务填"service-a",B服务填"service-b"
    rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
sleuth:
    sampler:
        probability: 1.0# 开发环境全量采样,生产环境可调整
    baggage:
        remote-fields: x-b3-traceid, x-b3-spanid, x-b3-parentspanid, x-b3-sampled# 传递的追踪字段
        correlation-fields: x-b3-traceid, x-b3-spanid
    zipkin:
        base-url: http://localhost:9411# Zipkin服务器地址

3. 生产者(A服务)代码:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String sendMessage() {
        String message = "Hello from Service A";
        //发送消息到RabbitMQ,Sleuth会自动在消息头中添加追踪信息
        rabbitTemplate.convertAndSend("test-exchange", "test.routing.key", message);
        return "Message sent: " + message;
    }
}

4. 消费者(服务 B)代码

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import brave.Tracer;

@Component
public class Consumer {

    //注入Tracer可手动获取当前追踪信息
    private final Tracer tracer;

    public Consumer(Tracer tracer) {
        this.tracer = tracer;
    }

    @RabbitListener(queues = "test-queue")
    public void receiveMessage(String message) {
        //消费消息时,Sleuth会自动从消息头中提取追踪信息
        String traceId = tracer.currentSpan().context().traceIdString();
        String spanId = tracer.currentSpan().context().spanIdString();
        System.out.println("Received message: " + message);
        System.out.println("TraceId: " + traceId + ", SpanId: " + spanId);
    }
}

5. RabbitMQ配置(两个服务都需要)

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    @Bean
    public Queue queue() {
        return new Queue("test-queue", true);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange("test-exchange");
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("test.routing.key");
    }
}

6. 启动Zipkin服务器
使用 Docker 启动 Zipkin:

docker run -d -p 9411:9411 openzipkin/zipkin

7. 验证追踪
1.启动RabbitMQ、Zipkin、A服务和B服务。
2.访问A服务的接口:http://localhost:服务A端口/send。
3.查看B服务的控制台,会输出与A服务相同的traceId。
4.访问Zipkin控制台(http://localhost:9411),可看到完整的调用链路。

关键原理
1.Spring Cloud Sleuth会自动在RabbitMQ消息头中添加X-B3-TraceId、X-B3-SpanId等追踪字段。
2.消费者接收消息时,Sleuth会从消息头中提取这些字段,实现追踪上下文的传递。
3.无需手动处理追踪信息,Sleuth与 RabbitMQ的集成是自动完成的。

如果仍有问题,可检查:
1.依赖版本是否兼容(可改为使用Spring Cloud最新稳定版)。
2.RabbitMQ是否正常运行,消息能否正常发送/接收。
3.Zipkin服务器地址是否正确配置。



相关文章

Array and string offse...

有同学咨询,在迁移网站时,出现了错误提示: Array and string offset access syntax with curly braces is no longer supported这是怎么回事呢?该同学的网站是PHP...

订单管理系统的设计与制作

订单管理系统是商业运营中较多的应用,包含商业订单的录入、编辑、查询、跟进、支付,以及对应财务资金的统筹和管理,下面是丁老师设计的订单管理系统功能架构,以功能模块的形式,分享出来给大家。一、商家管理商家管理是订单系统中的基础信息,可以理解...

Html网页调用打开微信小程序白屏的问题解决...

最近好多学员在问,自己开发的小程序,需要用html网页打开,按照网上的教程,没有一个成功的,希望丁老师帮助下。我说没问题,然后去网上看了一下,关于使用h5打开微信小程序这个问题,全部都是只言片语,没有一篇完整的内容,大家把各种代码东拼西...

Sql Server还原文件无法为该请求检索...

有同学咨询,在使用Sql Server还原数据库时,提示“无法为该请求检索数据”,这是怎么回事,如何解决呢?如图:问题复现1.使用之前备份的数据库文件恢复数据库(.bak文件)。2.选择数据库,选择要还原的文件,点击确定进行恢复还原,提...

推荐文章

Tortoise Svn出现No Path ...

在日常开发过程中,突然发现svn无法提交和更新,出现“no path or pathfile specified on the command line”的错误,网上搜了一圈百度 google 以及stack overflow都没找到真正能...

性价比最高的内网穿透方案

内网穿透,是指外网可以访问内网,没有固定IP的服务器,所以需要中间件来进行通讯,目前稳定成熟的中间件软件,要么价格太贵,要么就是只能使用标准服务,不能完全的定制化。那么,到底有没有一个性价比最优的方案呢?回答是肯定的,答案就是使用云服务器+...

Mysql 1267 Illegal mix...

这个问题,用普通sql语句查询,好解决,把表的字符编码改为对应的就可以了。 但在使用视图查询时,比如select 'aaa' as type from xxx; select * from view where type=...

FastAdmin后台列表默认分页数量由10...

没那么多废话,直接上修改方法public\assets\js\require-table.jspageSize: Config.pagesize || localStorage.getItem("pagesize") |...