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

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

所属分类:软件开发 来源: 丁老师原创 更新时间:2025-10-09 08:27 浏览: 1091 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服务器地址是否正确配置。



相关文章

如何使用Android Studio构建发布...

有同学咨询,想知道怎么样使用Android Studio来构建Flutter应用项目,今天丁老师来教你。在Android Studio中,选择顶部菜单的Build-Flutter-Build APk,就可以生成Flutter项目的APK...

EditPlus正则匹配备份

匹配a链接正则:<a href="[^"]+"匹配数字替换,如替换sql语句的(2,'a','b')为(2,2,'a','b')查找项:...

My97DatePicker在iframe页...

问题描述:在使用iframe的框架系统中,左侧菜单,右侧为iframe页面区域,由左侧的菜单控制右侧的iframe链接显示页面。每个页面都由my97datepicker,在刷新网页首次打开右侧页面内容时,触发WdatePicker显示日...

做Windows桌面软件用哪种程序语言好?

最近有同学咨询丁老师,公司要做一款设备的管理系统,是要安装在Windows上的桌面客户端软件,这种Windows桌面软件,用什么程序架构开发比较好、比较稳定呢?针对这个问题,丁老师做一下介绍。Windows桌面客户端软件发展已久,自从W...

推荐文章

Javascript复制对象改变原值的解决办...

在进行vue开发中,经常会遇到复制对象,赋值新对象后,原对象值被改变的问题,举个例子:let old={ "a":1, "b":2 } let new=old; new['c']=3;...

您的nahimic驱动程序似乎已经过期的解决...

打开电脑的Nahimic软件,提示“您的nahimic驱动程序似乎已经过期,无法启动”,然后需要点击链接更新驱动,但点击进去后,也找不到什么驱动。经过研究发现解决办法:1.使用SDI安装,即Snappy Driver Installer 1...

Uniapp开发小程序内的图片扫码

在使用uniapp开发的小程序内,打开图片,长按没有弹出菜单,无法扫码,此时只需在图片上加入show-menu-by-longpress即可完整代码:<image :src="picture" mode="...

Python3.12提示windows No...

在使用python3.12时突然遇到提示windows No module named distutils,研究了一下,把解决办法分享出来。1.安装 setuptools,它现在也提供 distutils;2.从第三方源(如系统软件包)载入...