1. 概述
Spring Cloud Sleuth實現對Spring cloud 分布式鏈路監控?
本文介紹了和Sleuth相關的內容,主要內容如下:
- Spring Cloud Sleuth中的重要術語和意義:Span、Trance、Annotation
- Zipkin中圖形化展示分布式鏈接監控數據并說明字段意義
- Spring Cloud集成Sleuth + Zipkin 的代碼demo: Sleuth集成Zipkin, Zipkin數據持久化等
2. 術語
Span?
Span是基本的工作單元。Span包括一個64位的唯一ID,一個64位trace碼,描述信息,時間戳事件,key-value 注解(tags),span處理者的ID(通常為IP)。?
最開始的初始Span稱為根span,此span中span id和 trace id值相同。
Trance?
包含一系列的span,它們組成了一個樹型結構
Annotation?
用于及時記錄存在的事件。常用的Annotation如下
- cs - Client Sent:客戶端發送一個請求,表示span的開始
- sr - Server Received:服務端接收請求并開始處理它。(sr-cs)等于網絡的延遲
- ss - Server Sent:服務端處理請求完成,開始返回結束給服務端。(ss-sr)表示服務端處理請求的時間
- cr - Client Received:客戶端完成接受返回結果,此時span結束。(cr-sr)表示客戶端接收服務端數據的時間
如果一個服務的調用關系如下:?
那么此時將Span和Trace在一個系統中使用Zipkin注解的過程圖形化:?
每個顏色的表明一個span(總計7個spans,從A到G),每個span有類似的信息
Trace Id = X
Span Id = D
Client Sent
- 1
- 2
- 3
此span表示span的Trance Id是X,Span Id是D,同時它發送一個Client Sent事件
spans 的parent/child關系圖形化如下:?
3. 在Zipkin中圖形化展示分布式鏈接監控數據
3.1 spans在zipkin界面的信息解讀
在Zipkin中展示了上圖的跟蹤信息,紅框里是對上圖調用span的跟蹤?
但是你點擊這個trace時,我們只看到4個span?
為什么兩個界面顯示的span數量不同,一個是7,一個4.
- 1 個 spans:來自servier1的接口http:/start 被調用,分別是Server Received (SR) 和 Server Sent (SS) annotations.
- 2 個 spans:來自 service1 調用service2 的 http:/foo 接口。service1 端有兩個span,分別為 Client Sent (CS) 和 Client Received (CR) annotations。service2 端也有兩個span,分別為Server Received (SR) 和Server Sent (SS) 。物理上他有2個span,但是從邏輯上說這個他們組成一個RPC調用的span。
- 2個span:來自 service2 調用 service3 的 http:/bar 接口,service2 端有兩個span,分別為Client Sent (CS) 和 Client Received (CR) annotations。service3 端也有兩個span,分別為Server Received (SR) 和Server Sent (SS) 。物理上他有2個span,但是從邏輯上說它們都是同一個RPC調用的span。
- 2個span:來自 service2 調用 service4 的 http:/bar 接口,service2 端有兩個span,分別為Client Sent (CS) 和 Client Received (CR) annotations。service4 端也有兩個span,分別為Server Received (SR) and Server Sent (SS) 。物理上他有2個span,但是從邏輯上說這個它們都是同一個RPC調用的span。
所以我們計算物理spans有7個:
- 1個來自 http:/start 被請求
- 2個來自 service1調用service2
- 2個來自 service2調用service3
- 2個來自 service2調用service4.
從邏輯上說,我們只看到4個span:
- 1個來自 service1 的接口http:/start 被請求
- 3個來自 服務之前的RCP接口調用
3.2. Zipkin可視化錯誤
如果調用鏈路中發生接口調用失敗,zipkin會默認使用紅色展示信息,如下圖:?
點擊紅色的span,可以看到詳細的失敗信息:?
4. Spring Cloud集成Sleuth + Zipkin
本節演示在Spring Cloud中集成Sleuth并將鏈路監控數據傳送到Zipkin,使用Zipkin展示數據,并配置mysql進行數據持久化
4.1. 相關工程
cloud-registration-center?
注冊中心
cloud-service-zipkin?
提供測試服務接口,對外提供有兩個接口:?
- 調用成功后,馬上返回一個結果:?http://127.0.0.1:17602//zipkin/simple?
- 調用成功后,服務sleep 5s后再返回:?http://127.0.0.1:17602//zipkin/sleep
cloud-consumer-zipkin?
消費服務?
此服務會通過Feign調用cloud-service-zipkin里提供的兩個接口(/zipkin/sleep和/zipkin/simple),我們訪問如下URL會調用的對應的接口:?
http://127.0.0.1:17603//zipkin/simple?
http://127.0.0.1:17603//zipkin/sleep
以上3個服務的代碼比較簡單,這里代碼略,可以自己看github里的代碼。下文中只列出和Sleuth和Zipkin相關的配置。
cloud-dashboard-zipkin?
配置Zipkin服務,提供可視化鏈路監控?
Zipkin首頁:http://127.0.0.1:17601/zipkin/
4.2. 在工程中使用sleuth+zipkin+http配置
在cloud-service-zipkin和cloud-consumer-zipkin中啟動sleuth,sleuth會收集spans信息,并使用http異步地將spans 信息發送到Zipkin,然后Zipkin展示信息
cloud-service-zipkin和cloud-consumer-zipkin配置Sleuth+Zipkin?
2個工程中為了集成Sleuth和Zipkin,需要在普通工程基礎上增加如下配置:
- pom.xml增加sleuth和zipkin相關的jar
<!-- sleuth配置 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<!-- 引入zipkin -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- application-zipkin.yml?
Zipkin+Sleuth配置參數:?- spring.zipkin.baseUrl:指定cloud-dashboard-zipkin的服務地址,本例子中使用真實的IP。在新的版本spring-cloud-sleuth-core-1.3.0.RELEASE中,可以實現通過服務名稱進行訪問
- spring.sleuth.sampler.percentage:設置采樣率,為了測試設置100%采集
spring:zipkin:enabled: true# zipkkin dashboard的地址:通過真實IP地址訪問baseUrl: http://localhost:17601/# 通過cloud-dashboard-zipkin注冊到注冊中心的服務名稱訪問,本版本(spring-cloud-sleuth-core-1.2.5.RELEASE)不支持,需要從spring-cloud-sleuth-core-1.3.0.RELEASE開始支持這個功能# 配置如下:# baseUrl: http://cloud-dashboard-zipkin/sleuth:sampler:# 默認值為0.1f,現在為了測試設置100%采集percentage: 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
cloud-dashboard-zipkin?
配置zipkin服務
- pom.xml
<!-- spring-cloud-starter-zipkin -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!-- zipkin 界面-->
<dependency><groupId>io.zipkin.java</groupId><artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<!-- zipkin 服務類-->
<dependency><groupId>io.zipkin.java</groupId><artifactId>zipkin-server</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 配置參數?
bootstrap-zipkin-http.yml
# port
server:port: 17601spring:application:# 本服務注冊到注冊到服務器的名稱, 這個名稱就是后面調用服務時的服務標識符name: cloud-dashboard-zipkin
eureka:client:serviceUrl:# 服務器注冊/獲取服務器的zonedefaultZone: http://127.0.0.1:10761/eureka/# defaultZone: http://192.168.21.3:10761/eureka/,http://192.168.21.4:10761/eureka/instance:prefer-ip-address: true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- application-zipkin-http.yml?
關閉本工程的推送到zipkin服務的功能
spring:zipkin:enabled: false
- 1
- 2
- 3
啟動類:?
@EnableZipkinServer:注解此類為Zipkin服務
@EnableEurekaClient // 配置本應用將使用服務注冊和服務發現
@SpringBootApplication
@EnableZipkinServer // 啟動Zipkin服務
public class ZipkinDashboardCloudApplication {public static void main(String[] args) {args = new String[1];args[0] = "--spring.profiles.active=zipkin-http";SpringApplication.run(ZipkinDashboardCloudApplication.class, args);}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
4.3. 測試
啟動zipkin服務,提供可視化鏈路監控?
Zipkin訪問地址:?http://127.0.0.1:17601/zipkin/
我們演示鏈路跟蹤,訪問以下兩個請求:?
http://127.0.0.1:17603/zipkin/simple?:正常方法?
http://127.0.0.1:17603/zipkin/sleep?:訪問超時
進入zipkin,訪問成功為藍色,失敗為紅部。同時顯示各個服務花費的時間?
點擊藍色記錄,進入詳細界面?
可知整個接口花費約19ms,cloud-zipkin-service的業務執行15ms,cloud-zipkin-consumer訪問cloud-zipkin-service的 網絡延遲為2ms,返回結果給cloud-zipkin-service的網絡延遲是4s?
點擊cloud-zipkin-service得到更精確的數據
4.4. Zipkin數據持久化
默認情況,zipkin存儲記錄到內存,如果服務重啟,則所有記錄丟失。為了保證持久性,zipkin支持Mysql、Elasticsearch、Cassandra存儲。我們演示為zipkin配置MYSQL數據庫,其它兩種配置類似,本文略?
pom.xml為cloud-dashboard-zipkin引入新jar
<!-- zipkin 存儲到數據庫需要引入此類 -->
<dependency><groupId>io.zipkin.java</groupId><artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
</dependency><!--保存到數據庫需要如下依賴-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>6.0.6</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
配置參數:?
application-zipkin-http.yml:?
配置啟動時會根據classpath:/mysql.sql初始化數據庫
spring:zipkin:enabled: false# 配置mysqldatasource:schema: classpath:/mysql.sql# url: jdbc:mysql://127.0.0.1/testurl: jdbc:mysql://127.0.0.1:3306/test?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8username: rootpassword: root
# Switch this on to create the schema on startup:initialize: truecontinueOnError: truesleuth:enabled: false
zipkin:storage:type: mysql
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
重啟服務,服務成功后,查看數據庫,自動生成數據庫?
如此數據庫配置完畢,所有的spans信息存儲到數據庫中,重啟服務,也不會丟失記錄
4.5 在工程中使用sleuth+zipkin+ Spring Cloud Stream配置
本版本(spring-cloud-sleuth-core-1.2.5.RELEASE)支持集成spring-cloud-sleuth-stream,但是在新版本從spring-cloud-sleuth-core-1.3.0.RELEASE開始不支持spring-cloud-sleuth-stream。推薦直接使用通過集成RabbitMQ 或 Kafka。關于集成RabbitMQ 或 Kafka,關鍵是引入spring-rabbit 或 spring-kafka依賴包。默認的目的名稱為zipkin.?
對于這個的使用demo。這里暫時略
5. 代碼
以上的詳細的代碼見下面?
github代碼,請盡量使用tag v0.11,不要使用master,因為我不能保證master代碼一直不變