鏈路追蹤:指一次任務的開始到結束,期間調用的所有系統及耗時都可以完整的記錄下來。
一、Sleuth
1. 功能
A. 鏈路追蹤:查看一個請求經過了哪些服務及服務之間調用關系;
B. 性能分析:查看每個采樣請求的耗時情況,對耗時長的進行處理;
C. 數據分析,優化鏈路:對服務頻繁調用、並發高的進行業務優化;
D. 可視化錯誤:對於程序未捕獲的異常,結合zipkin查看。
2. 概念
A. Span:一次單獨的調用鏈稱為span,是基本工作單位,有Span ID和父Span ID;
B. Trace:由一些列span組成的樹狀結構,是一次完整的鏈路,有Trace ID;
C. Annotation:用來記錄一個事件的存在,定義一個請求的開始和結束;
Client Send(cs):記錄請求的開始;
Server Received(sr):服務端獲取請求並准備開始處理;
Server Send(ss):請求服務端處理完成;
Client Received(cr):客戶端收到服務端的回復時間。
3. pom.xml Maven依賴
<!-- Sleuth分布式鏈路追蹤 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
4. application.yml 文件配置
spring: sleuth: sampler: # 收集數據百分比,默認0.1(現網配置大小) probability: 1.0
二、Zipkin
1. 組件
A. Collector(收集器):處理從外部系統發送過來的跟蹤信息,將這些信息轉化為Zipkin內部處理的Span格式,以支持后續的存儲、分析和展示等功能;
B. Storage(存儲):處理收集器接收到的跟蹤信息,默認存儲在內存中,還可以儲存在MySQL、ElasticSearch中;
C. Web UI:提供WEB界面,展示調用鏈和服務依賴關系;
D. Restful API:為WEB界面提供查詢存儲的數據的接口。
2. 服務端部署
B. 訪問地址:http://localhost:9411/zipkin

3. 追蹤數據存儲
A. 追蹤數據默認存儲在內存中,這種會導致服務一旦重啟,數據就會丟失,所以需要替換為持久化方式存儲,如MySQL;
B. MySQL方式存儲:zipkin.sql
CREATE TABLE IF NOT EXISTS zipkin_spans ( `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', `trace_id` BIGINT NOT NULL, `id` BIGINT NOT NULL, `name` VARCHAR(255) NOT NULL, `remote_service_name` VARCHAR(255), `parent_id` BIGINT, `debug` BIT(1), `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL', `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query', PRIMARY KEY (`trace_id_high`, `trace_id`, `id`) ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds'; ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames'; ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames'; ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range'; CREATE TABLE IF NOT EXISTS zipkin_annotations ( `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id', `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id', `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1', `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB', `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation', `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp', `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null', `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address', `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null', `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null' ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate'; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans'; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds'; ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames'; ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values'; ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values'; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job'; CREATE TABLE IF NOT EXISTS zipkin_dependencies ( `day` DATE NOT NULL, `parent` VARCHAR(255) NOT NULL, `child` VARCHAR(255) NOT NULL, `call_count` BIGINT, `error_count` BIGINT, PRIMARY KEY (`day`, `parent`, `child`) ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
4. pom.xml Maven依賴
<!-- Zipkin鏈路追蹤收集 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
或者
<!-- Sleuth分布式鏈路追蹤 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<!-- Sleuth整合Zipkin界面展示 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
5. application.yml 文件配置
spring: zipkin: # zipkin是否開啟,默認開啟 enabled: true # 服務端地址 base-url: http://localhost:9411 sender: # 數據傳輸方式,web以HTTP報文形式向服務器發送 type: web
可參考:zipkin官方源碼
三、SkyWalking
