SpringCloud 入門實戰(12)--Zipkin(2)--安裝使用
Zipkin 的介紹參見上一篇文章:SpringCloud 入門實戰(11)--Zipkin 使用一(Zipkin 簡介)。本文主要介紹 Zipkin 的基本使用,文中所使用到的軟體版本:Zipkin 2.23.2、Spring Boot 2.3.11.RELEASE、Spring Cloud Hoxton.SR8、jdk1.8.0_181。
1、Zipkin 安裝
1.1、下載應用包
在能連外網的機器上執行:
curl -sSL https://zipkin.io/quickstart.sh | bash -s
執行命令完成後,就會下載 Zipkin 的應用包:zipkin.jar。
1.2、儲存方式
Zipkin 可以把資料儲存在記憶體、MySQL、Cassandra、Elasticsearch 中,詳細的配置說明可參考官方說明:https://github.com/openzipkin/zipkin/tree/master/zipkin-server。
1.2.1、資料儲存在記憶體中
引數:
MEM_MAX_SPANS:儲存的最大 span 的數量,超過了會把最早的 span 刪除
啟動命令例子:
MEM_MAX_SPANS=1000000 java -Xmx1G -jar zipkin.jar
1.2.2、資料儲存在 MySQL 中
該種儲存方式已不推薦使用,在資料量大的時候,查詢較慢。
資料庫初始化指令碼:https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql-v1/src/main/resources/mysql.sql
-- -- Copyright 2015-2019 The OpenZipkin Authors -- -- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except -- in compliance with the License. You may obtain a copy of the License at -- -- http://www.apache.org/licenses/LICENSE-2.0 -- -- Unless required by applicable law or agreed to in writing, software distributed under the Licensemysql.sql-- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -- or implied. See the License for the specific language governing permissions and limitations under -- the License. -- 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;
引數:
MYSQL_DB:MySQL 資料庫名,預設為 zipkin
MYSQL_USER:使用者名稱
MYSQL_HOST:主機地址
MYSQL_TCP_PORT:埠
MYSQL_MAX_CONNECTIONS:最大連線資料,預設 10
MYSQL_USE_SSL:是否使用ssl,需要 javax.net.ssl.trustStore 和 javax.net.ssl.trustStorePassword, 預設 false
MYSQL_JDBC_URL:自己設定 JDBC 的 url
啟動命令例子:
MYSQL_USER=root MYSQL_PASS=123456 MYSQL_HOST=10.40.96.32 MYSQL_TCP_PORT=3306 java -jar zipkin.jar
1.3、採集方式
1.3.1、HTTP Collector
HTTP 方式預設開啟,通過 POST /api/v1/spans 和 POST /api/v2/spans 地址接受資料;相關配置如下:
環境變數 | 屬性 | 描述 |
COLLECTOR_HTTP_ENABLED | zipkin.collector.http.enabled | 是否啟用 HTTP 方式採集資料,預設 true |
1.3.2、Kafka Collector
當KAFKA_BOOTSTRAP_SERVERS 不為空時該方式生效;相關配置如下:
環境變數 | 對應 kafka 消費者配置 | 描述 |
COLLECTOR_KAFKA_ENABLED | 是否啟用 Kafka 方式採集資料,預設 true | |
KAFKA_BOOTSTRAP_SERVERS | bootstrap.servers | Kafk 地址 |
KAFKA_GROUP_ID | group.id | 消費者所屬消費者組,預設 zipkin |
KAFKA_TOPIC | 主題,多個以逗號分隔,預設 zipkin | |
KAFKA_STREAMS | 消費者的執行緒數,預設 1 |
啟動命令例子:
KAFKA_BOOTSTRAP_SERVERS=127.0.0.1:9092 java -jar zipkin.jar
2、Zipkin 使用
這裡主要介紹在 Spring Cloud 中整合 Zipkin;使用起來還是很方便的,只需引入相關依賴並配置 Zipkin 的地址即可,然後就會把請求的監控資料發往 Zipkin。
2.1、Http 方式傳送資料到 Zipkin
2.1.1、引入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
2.1.2、application.yml
配置 Zipkin 地址
spring: zipkin: base-url: http://10.40.100.60:9411/ enabled: true
2.2、Kafka 方式傳送資料到 Zipkin
啟動 Kafka 並建立 Topic:zipkin;配置 Zipkin 的服務端從 Kafka 接受資料;參見 1.3.2
2.2.1、引入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
2.1.2、application.yml
spring: zipkin: enabled: true sender: type: kafka kafka: topic: zipkin kafka: bootstrap-servers: 10.40.100.69:9092
2.3、監控 MySQL 操作
這裡假設在 2.1 Spring Cloud 專案的基礎再進一步對 MySQL 的操作進行監控。
2.3.1、引入依賴
<dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-instrumentation-mysql8</artifactId> <version>5.13.3</version> </dependency>
2.3.2、jdbc url 中增加引數
在 jdbc url 中新增攔截器和服務名:
queryInterceptors=brave.mysql8.TracingQueryInterceptor&exceptionInterceptors=brave.mysql8.TracingExceptionInterceptor&zipkinServiceName=myDatabaseService
例如,配置如下的資料來源:
spring: datasource: druid: primary: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://10.40.94.23:3306/test?useUnicode=true&characterEncoding=UTF-8&queryInterceptors=brave.mysql8.TracingQueryInterceptor&exceptionInterceptors=brave.mysql8.TracingExceptionInterceptor&zipkinServiceName=myDB username: root password: 123456 initialSize: 2 minIdle: 2 maxActive: 5 validationQuery: SELECT 1 testWhileIdle: true testOnBorrow: true testOnReturn: false maxWait: 6000 filters: wall,slf4j
3、測試
前臺訪問 scdemo-client 服務,在 scdemo-client 中又呼叫scdemo-server 服務,在 scdemo-server 執行了兩次查詢 MySQL 的操作。