Spring cloud系列十四 分布式鏈路監控Spring Cloud Sleuth

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代碼一直不變

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387078.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387078.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387078.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux源碼編譯安裝程序

一、程序的組成部分 Linux下程序大都是由以下幾部分組成&#xff1a; 二進制文件&#xff1a;也就是可以運行的程序文件 庫文件&#xff1a;就是通常我們見到的lib目錄下的文件 配置文件&#xff1a;這個不必多說&#xff0c;都知道 幫助文檔&#xff1a;通常是我們在linux下用…

selenium用法詳解

selenium用法詳解 selenium主要是用來做自動化測試&#xff0c;支持多種瀏覽器&#xff0c;爬蟲中主要用來解決JavaScript渲染問題。 模擬瀏覽器進行網頁加載&#xff0c;當requests,urllib無法正常獲取網頁內容的時候一、聲明瀏覽器對象 注意點一&#xff0c;Python文件名或者…

haoop筆記

8:00 2019/3/141&#xff1a;什么是hadoop? hadoop是解決大數據問題的一整套技術方案2&#xff1a;hadoop的組成? 核心框架 分布式文件系統 分布式計算框架 分布式資源分配框架 hadoop對象存儲 機器計算3&#xff1a;hadoop 云計算 大數據 微服務 人工智能關系 參見word學習…

Sleuth則是用來共方便的集成Zipkin。

簡述 使用 spring cloud 用到最多的是各種rest服務調用&#xff0c;Twitter的Zipkin 是一種實現分布式跟蹤解決方案&#xff0c;Sleuth則是用來共方便的集成Zipkin。調用跟蹤系統的業務場景 隨著服務的拆分&#xff0c;系統的模塊變得越來越多&#xff0c;不同的模塊可能由不同…

Spring Cloud 中 分布式事務解決方案 -- 阿里GTS的使用

1&#xff1a;依賴引入<!--gts相關--><!--數據庫連接--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>mysql&…

《HTTP 權威指南》筆記:第十五章 實體與編碼

&#xfffc; 如果把 「HTTP 報文」想象為因特網貨運系統的「箱子」,那么「HTTP 實體」就是報文中的實際的「貨物」. 其中,實體又包含了「實體首部」 和 「實體主體」,實體首部用于描述各種參數,實體主體就是原始貨物. 常見的實體首部 實體的大小: Content-Length 定義: 報文的…

Spring Cloud Sleuth進階實戰

為什么需要Spring Cloud Sleuth 微服務架構是一個分布式架構&#xff0c;它按業務劃分服務單元&#xff0c;一個分布式系統往往有很多個服務單元。由于服務單元數量眾多&#xff0c;業務的復雜性&#xff0c;如果出現了錯誤和異常&#xff0c;很難去定位。主要體現在&#xff…

Element表格嵌入復選框以及單選框

1&#xff0c;element 表格嵌入CheckBox 效果圖如下&#xff1a; 2&#xff0c;element結合checkBox實現單選效果如下&#xff1a; html代碼&#xff1a; <template><div><p>shopInfo</p><el-tableref"multipleTable":data"tableDat…

溫故之 “插入排序”

概念&#xff1a;將一個數據插入已經排好序的有序數組中&#xff0c;從而得到一個新的多一個數據的有序數組。 概念理解~~ 將要排序的是一個亂的數組int[] arrays {3, 2, 1, 3, 3}; 在未知道數組元素的情況下&#xff0c;我們只能把數組的第一個元素作為已經排好序的有序數據&…

實驗二3

#include "stdafx.h" #include "stdio.h" int main(int argc, char* argv[]) {int a,b,c; scanf("%d %d %d",&a,&b,&c);if(ab&&bc) printf("等邊三角形");else if((ab&&b!c)||(ac&&c!b)||(bc&a…

webpack來打包你的vue項目,如發現你的vendor.js過大

1.如果你使用了webpack來打包你的vue項目&#xff0c;如發現你的vendor.js過大則可以參考本文的解決方案. 2.造成過大的原因是因為在main.js導入第三庫太多時,webpack合并js時生成了vendor.js(我們習慣把第三方庫放在vendor里面)造成的.如下圖在main.js引用element-ui等第三方…

TF01 簡介

總覽 如何從實體中提取特征&#xff0c;對于很多傳統機器學習算法的性能有巨大影響。 一旦解決了數據表達和特征提取&#xff0c;很多人工智能任務也就解決了90%。 對許多機器學習算法來說&#xff0c;特征提取不是一件簡單的事情。 深度學習解決的核心問題之一就是自動的將簡…

K8s基本概念入門

序言 沒等到風來&#xff0c;綿綿小雨&#xff0c;所以寫個隨筆&#xff0c;聊聊k8s的基本概念。 k8s是一個編排容器的工具&#xff0c;其實也是管理應用的全生命周期的一個工具&#xff0c;從創建應用&#xff0c;應用的部署&#xff0c;應用提供服務&#xff0c;擴容縮容應用…

idea出現找不到實體類

今天經理遇到一個很奇怪的問題&#xff1a; 在使用idea時&#xff0c;就是包真實存在&#xff0c;但是包中的實體類卻無法智能提示&#xff0c;也無法導入成功&#xff1b; 我推薦的解決辦法是重新導入&#xff0c;但是沒有用&#xff0c;經理在網上找了很多解決方式&#xff0…

TF02 入門

計算模型——圖 數據模型——張量 運行模型——會話 TensorFlow計算模型——計算圖 計算圖是TF中最基本的一個概念&#xff0c;TF中的所有計算都會被轉化為計算圖上的結點。 TF是一個通過計算圖的形式來表述計算的編程系統。TF中的每一個計算都是計算圖上的一個節點&#x…

ElasticSearch、Logstash和Kiabana三個開源工具。

一 方案背景 通常&#xff0c;日志被分散的儲存不同的設備上。如果你管理數十上百臺服務器&#xff0c;你還在使用依次登錄每臺機器的傳統方法查閱日志。這樣是不是感覺很繁瑣和效率低下。開源實時日志分析ELK平臺能夠完美的解決日志收集和日志檢索、分析的問題&#xff0c;ELK…