聲明:本文來源於MLDN培訓視頻的課堂筆記,寫在這里只是為了方便查閱。
1、概念:SpringCloudSleuth
2、具體內容
Sleuth 是一種提供的跟蹤服務,也就是說利用 sleuth 技術可以實現完整的微服務的訪問路徑的跟蹤操作。
2.1、SpringCloudSleuth 簡介
微服務可以將整個的系統拆分為無數個子系統,於是這樣一來就有可能出現幾種可怕的場景:
· 代碼的調試:
|- 你的系統有可能變慢了,於是這個時候就需要去追蹤每一個微服務的執行的速度;
|- 如果現在你的微服務采用了串聯的方式進行了互相調用,那么如何確認某一個微服務出現了問題呢?
· 微服務混合調用:
|- 現在微服務變為了環形調用,那么這些關系該如何描述出來?
在創建微服務的時候一定要有一些合適的開發契約,所有的開發者以及服務的調用者要按照統一的方式進行程序的調用處理, 這樣才可以成為一個優秀的微服務設計。
所以在 SpringCloud 之中提供的 Sleuth 技術就可以實現微服務的調用跟蹤,也就是說它可以自動的形成一個調用連接線,通過這個連接線使得開發者可以輕松的找到所有微服務間關系,同時也可以獲取微服務所耗費的時間, 這樣就可以進行微服務調用狀態的監控以及相應的數據分析。
Span 里面包含有如下內容:
· cs-Client Sent:客戶端發出一個請求,描述的是一個 Span 開始;
· sr-Server Received:服務端接收請求,利用 sr-cs 就屬於發送的網絡延遲;
· ss-Server Sent:服務端發送請求(回應處理),ss-sr 就屬於服務端的消耗時間;
· cr-Client Received:客戶端接收到服務端數據,cr-ss 就表示回復所需要的時間。
2.2、SpringCloudSleuth 基本配置
SpringCloudSleuth 使用的核心組件在於 Twitter 推出的 zipkin 監控組件,所以本次的配置的模塊一定要包含 zipkin 相關配置依賴,本次實現一個基礎的調用邏輯:consumer-zuul-dept。
1、 【microcloud-sleuth-8601】通過“microcloud-provider-company-8101”項目復制得來;
2、 【microcloud-sleuth-8601】修改 pom.xml 配置文件:
· 由於 sleuth 的應用比較復雜,而且也牽扯到埋點的數據分析,本次不使用安全處理模塊:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency>
3、 【microcloud-sleuth-8601】修改 application.yml 配置文件:
server:
port: 8601
spring:
application:
name: microcloud-zipkin-server
4、 【microcloud-sleuth-8601】修改程序啟動類:
package cn.study.microcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import zipkin.server.EnableZipkinServer; @SpringBootApplication @EnableCircuitBreaker @EnableZipkinServer public class Zipkin_8601_StartSpringCloudApplication { public static void main(String[] args) { SpringApplication.run(Zipkin_8601_StartSpringCloudApplication.class, args); } }
5、 修改 hosts 配置文件,追加一個新的主機名稱映射:
127.0.0.1 zipkin.com
6、 【microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001】修改 pom.xml 配置文件,追加 zipkin 相關依賴程序包:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
7、 【microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001】修改 application.yml 配置文件:
spring:
zipkin:
base-url: http://zipkin.com:8601 # 所有的數據提交到此服務之中
sleuth:
sampler:
percentage: 1.0 # 定義抽樣比率,默認為0.1
application:
name: microcloud-consumer-feign
一定要有每一個微服務的名字,這樣會比較好觀察程序的執行軌跡。
8、 依次啟動所有的服務:microcloud-sleuth-8601、microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001;
輸入訪問地址:http://zipkin.com:8601;就可以看到各個微服務之間的調用關系了
2.3、數據采集
現在已經成功的實現了一個 SpringCloudSleuth 基礎操作,但是需要考慮一個實際的問題,現在所有的統計的匯總操作都是記錄在內存之中的,也就是說如果你現在已經關閉了 zipkin 服務端,那么這些統計信息就將消失,很明顯這樣的做法並不符合實際要求,數據應該被記錄下來,而且有可能你很多的微服務要發送大量的數據信息進入,為了解決這種高並發的問題,可以結合消息組件(Stream)進行緩存處理,而且本次為了方便可以將統計的結果保存在數據庫之中(mysql)。
1、 需要創建數據庫腳本,腳本是從官網拷貝下來的直接復制使用即可:
DROP DATABASE IF EXISTS zipkin ; CREATE DATABASE zipkin CHARACTER SET UTF8 ; USE zipkin ; 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, `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' ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`); ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`); ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`); ALTER TABLE zipkin_spans ADD INDEX(`name`); ALTER TABLE zipkin_spans ADD INDEX(`start_ts`); 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`); ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) ; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`); ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`); ALTER TABLE zipkin_annotations ADD INDEX(`a_type`); ALTER TABLE zipkin_annotations ADD INDEX(`a_key`); CREATE TABLE IF NOT EXISTS zipkin_dependencies ( `day` DATE NOT NULL, `parent` VARCHAR(255) NOT NULL, `child` VARCHAR(255) NOT NULL, `call_count` BIGINT ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);
2、 【microcloud-sleuth-8601】修改 pom.xml 配置文件,追加相關的依賴程序包:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> -->
3、 【microcloud-sleuth-8601】修改 application.yml 配置文件:
server:
port: 8601
spring:
rabbitmq:
host: rabbitmq-server
port: 5672
username: studyjava
password: hello
virtual-host: /
datasource:
driver-class-name: org.gjt.mm.mysql.Driver # 配置MySQL的驅動程序類
url: jdbc:mysql://localhost:3306/zipkin # 數據庫連接地址
username: root # 數據庫用戶名
password: mysqladmin # 數據庫連接密碼
initialize: true
application:
name: microcloud-zipkin-server
zipkin:
storage: # 設置zipkin收集的信息通過mysql進行存儲
type: mysql
4、 【microcloud-sleuth-8601】可以打開安全配置項:
<dependency> <groupId>cn.study</groupId> <artifactId>microcloud-security</artifactId> </dependency>
5、 【microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001】修改 pom.xml 配置文件:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> -->
6、 【microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001】修改 application.yml 配置文件:
spring:
rabbitmq:
host: rabbitmq-server
port: 5672
username: studyjava
password: hello
virtual-host: /
同時刪除掉已有的 zipkin.base-url 的配置項。
7、 【microcloud-sleuth-8601】修改啟動程序類的使用注解:
package cn.study.microcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer; import zipkin.server.EnableZipkinServer; @SpringBootApplication @EnableCircuitBreaker @EnableZipkinStreamServer public class Zipkin_8601_StartSpringCloudApplication { public static void main(String[] args) { SpringApplication.run(Zipkin_8601_StartSpringCloudApplication.class, args); } }
8、 此時依次啟動各個微服務之后所有的信息都將被記錄到 MySQL 數據庫之中,這樣即使當前的 zipkin 服務關閉了,那么也可以進行信息的持久化存儲,下次啟動之后依然可以讀取到執行順序。