聲明:本文來源於MLDN培訓視頻的課堂筆記,寫在這裏只是爲了方便查閱。java
一、概念:SpringCloudSleuthmysql
Sleuth 是一種提供的跟蹤服務,也就是說利用 sleuth 技術能夠實現完整的微服務的訪問路徑的跟蹤操做。spring
微服務能夠將整個的系統拆分爲無數個子系統,因而這樣一來就有可能出現幾種可怕的場景:sql
· 代碼的調試:數據庫
|- 你的系統有可能變慢了,因而這個時候就須要去追蹤每個微服務的執行的速度;緩存
|- 若是如今你的微服務採用了串聯的方式進行了互相調用,那麼如何確認某一個微服務出現了問題呢?安全
· 微服務混合調用:網絡
|- 如今微服務變爲了環形調用,那麼這些關係該如何描述出來?併發
在建立微服務的時候必定要有一些合適的開發契約,全部的開發者以及服務的調用者要按照統一的方式進行程序的調用處理, 這樣才能夠成爲一個優秀的微服務設計。app
因此在 SpringCloud 之中提供的 Sleuth 技術就能夠實現微服務的調用跟蹤,也就是說它能夠自動的造成一個調用鏈接線,經過這個鏈接線使得開發者能夠輕鬆的找到全部微服務間關係,同時也能夠獲取微服務所耗費的時間, 這樣就能夠進行微服務調用狀態的監控以及相應的數據分析。
Span 裏面包含有以下內容:
· cs-Client Sent:客戶端發出一個請求,描述的是一個 Span 開始;
· sr-Server Received:服務端接收請求,利用 sr-cs 就屬於發送的網絡延遲;
· ss-Server Sent:服務端發送請求(迴應處理),ss-sr 就屬於服務端的消耗時間;
· cr-Client Received:客戶端接收到服務端數據,cr-ss 就表示回覆所須要的時間。
SpringCloudSleuth 使用的核心組件在於 Twitter 推出的 zipkin 監控組件,因此本次的配置的模塊必定要包含 zipkin 相關配置依賴,本次實現一個基礎的調用邏輯:consumer-zuul-dept。
一、 【microcloud-sleuth-8601】經過「microcloud-provider-company-8101」項目複製得來;
二、 【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>
三、 【microcloud-sleuth-8601】修改 application.yml 配置文件:
server:
port: 8601
spring:
application:
name: microcloud-zipkin-server
四、 【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); } }
五、 修改 hosts 配置文件,追加一個新的主機名稱映射:
127.0.0.1 zipkin.com
六、 【microcloud-consumer-feign、microcloud-zuul-gateway-950一、microcloud-provider-dept-8001】修改 pom.xml 配置文件,追加 zipkin 相關依賴程序包:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
七、 【microcloud-consumer-feign、microcloud-zuul-gateway-950一、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
必定要有每個微服務的名字,這樣會比較好觀察程序的執行軌跡。
八、 依次啓動全部的服務:microcloud-sleuth-860一、microcloud-consumer-feign、microcloud-zuul-gateway-950一、microcloud-provider-dept-8001;
輸入訪問地址:http://zipkin.com:8601;就能夠看到各個微服務之間的調用關係了
如今已經成功的實現了一個 SpringCloudSleuth 基礎操做,可是須要考慮一個實際的問題,如今全部的統計的彙總操做都是記錄在內存之中的,也就是說若是你如今已經關閉了 zipkin 服務端,那麼這些統計信息就將消失,很明顯這樣的作法並不符合實際要求,數據應該被記錄下來,並且有可能你不少的微服務要發送大量的數據信息進入,爲了解決這種高併發的問題,能夠結合消息組件(Stream)進行緩存處理,並且本次爲了方即可以將統計的結果保存在數據庫之中(mysql)。
一、 須要建立數據庫腳本,腳本是從官網拷貝下來的直接複製使用便可:
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`);
二、 【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> -->
三、 【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
四、 【microcloud-sleuth-8601】能夠打開安全配置項:
<dependency> <groupId>cn.study</groupId> <artifactId>microcloud-security</artifactId> </dependency>
五、 【microcloud-consumer-feign、microcloud-zuul-gateway-950一、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> -->
六、 【microcloud-consumer-feign、microcloud-zuul-gateway-950一、microcloud-provider-dept-8001】修改 application.yml 配置文件:
spring:
rabbitmq:
host: rabbitmq-server
port: 5672
username: studyjava
password: hello
virtual-host: /
同時刪除掉已有的 zipkin.base-url 的配置項。
七、 【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); } }
八、 此時依次啓動各個微服務以後全部的信息都將被記錄到 MySQL 數據庫之中,這樣即便當前的 zipkin 服務關閉了,那麼也能夠進行信息的持久化存儲,下次啓動以後依然能夠讀取到執行順序。