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

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

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



相关文章

EditPlus正则匹配备份

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

微信小程序开发生成普通二维码打开小程序的方法

在开发微信小程序的过程中,生成的是带有logo的小程序二维码,并且这个二维码不是通用的二维码,而是只能用微信打开的专属二维码,如图:遇到这样一个需求,生成的二维码要方形的通用二维码,并且不能带有小程序的logo。这种该怎么实现呢?经过查...

织梦dedecms防止木马漏洞的方法

织梦dedecms,很多站长起初建设网站都是织梦dedecms。因为程序非常符合seo优化,但是随着用的人越来越多,特别是一些精通织梦的人就想着搞一搞别人用织梦做的站,所以织梦出现了很多安全漏洞问题,该如何设置网站安全防护呢?

Uniapp picker 多列数据更新后不...

在uniapp中,使用picker multiSelector多列联动选择时,单独更新某列数据后,在前端没有显示,之前的写法:this.data_list[1]=[]; var aaa=[]; aaa.push(1); aaa.push...

推荐文章

小程序/APP定制开发价格标准价目表

类目价格标准备注商城小程序(小型)10-20万小型B2C/B2b商城,满足基本商城的所有功能商城小程序(中型)30-60万中型B2C/B2b商城,拥有类似淘宝、京东、拼多多的全部基本功能,可对接物流、仓储系统,支持高并发,安全性和稳定性有一...

IntelliJ/phpstorm/webs...

在使用jet brains的ide开发代码时,经常性的会输入单引号或双引号,ide默认有对于引号的自动完成功能,即输入一个引号,自动显示两个,这一点其实做的很不智能,因为在使用时经常会有显示出3个的情况,那么怎么关闭这个功能呢?file-s...

CI框架页面添加全局变量的方法

CI框架是一款很小却又非常强大的PHP程序框架,非常适合用来搭建中小型网站以及扩展程序。有同学问到,在使用CI框架时,比如要对全站的标题、公司信息、电话等设置一个全局变量,不用每个页面都写死,该怎么办呢?丁老师来教你:1.打开页面的控制器文...

抖音小程序开发的流程是什么?

开发抖音小程序是一个涉及多个步骤的过程,需要准备相应的资料和工具。以下是开发抖音小程序需要准备的资料和步骤:1. 入驻抖音开发平台:首先,你需要在抖音开放平台注册一个开发者账号,并完成主体认证和对公认证。如果未完成认证,你可以在沙盒工具中创...

当前在线
免费咨询