推薦:微服務彙總
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就有鍊路資訊了。
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() {
}
}
}