推荐:微服务汇总
Spring Cloud整合Sleuth,当请求完成后,Zipkin没有链路信息
首先要导入Sleuth和Zipkin的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
其实这个依赖就包括了sleuth
以及sleuth-zipkin
这两个依赖,按Ctrl点进去,就可以发现:
<dependencies>
<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>
</dependencies>
还需要写一些配置:
spring:
application:
name: order
zipkin:
base-url: http://127.0.0.1:9411
sleuth:
sampler:
probability: 1
spring:
application:
name: product
zipkin:
base-url: http://127.0.0.1:9411
sleuth:
sampler:
probability: 1
上面这两个配置是我Order服务和Product服务的部分配置信息,因为在下单时,Order服务需要通过Feign组件来请求Product服务的接口,这里主要看和Zipkin和Sleuth相关的配置就可以了。
然后发现用Postman进行下单测试时,发现Zipkin没有链路信息,如下图。
后面通过查阅博客,发现少了一个配置,就是多个服务的请求信息用什么方式传给Zipkin,所以还需要配置:
再用Postman进行下单测试,Zipkin就有链路信息了。
可以阅读一下ZipkinSenderProperties
类源码:
package org.springframework.cloud.sleuth.zipkin2.sender;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("spring.zipkin.sender")
public class ZipkinSenderProperties {
private ZipkinSenderProperties.SenderType type;
public ZipkinSenderProperties() {
}
public ZipkinSenderProperties.SenderType getType() {
return this.type;
}
public void setType(ZipkinSenderProperties.SenderType type) {
this.type = type;
}
public static enum SenderType {
RABBIT,
KAFKA,
WEB;
private SenderType() {
}
}
}
可以发现,当没有设置type
时,type
就会为null
,也可以看到,我这个版本目前有三种方式,rabbit
、kafka
这两种消息队列,以及通过web
方式。