有同学提问,使用的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/zipkin7. 验证追踪
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服务器地址是否正确配置。
在使用idea系列的配套软件时,如intellij idea、Php Storm、Web Strom时,在搜索文件时,右上角的file mask可以筛选指定类型的文件。但是有时候可能会输入错误,产生多个记录,而且没办法删除,在以后的开发...
支付宝支付提示“暂时无法获取订单信息,请稍候再试”的解决办法
在开发微信小程序的过程中,生成的是带有logo的小程序二维码,并且这个二维码不是通用的二维码,而是只能用微信打开的专属二维码,如图:遇到这样一个需求,生成的二维码要方形的通用二维码,并且不能带有小程序的logo。这种该怎么实现呢?经过查...
在Mysql中,5.7以下版本是不支持Json对象的,但是可以将数据以json字符串的形式,保存在数据库的text字段中。以下是针对Mysql5.7以下版本json字符串的一些处理方法。#根据键值name查询(需要注意单双引号) sel...
在实际工作中发现,有个编辑器每次保存或者修改后,都会自动在内容顶部和底部增加几行,烦人的很,一直找不到解决办法。后来发现原来是自己代码的问题。解决办法:内容在textarea中,不要换行不要写成:<textarea{content} ...
宝塔系统相关问题解决方法
fastadmin里自定义按钮的btn-dialog,弹出窗口时默认大小,设置了data-area无效,经过研究后,发现了可用的方法,直接上代码不废话:table.on('post-body.bs.table',functi...
1. composer版本更新文档版本号:https://getcomposer.org/download中文网:https://docs.phpcomposer.com/03-cli.html#self-update英文网:https:/...