SpringCloud 入門實戰(12)--Zipkin(2)--安裝使用

Zipkin 的介紹參見上一篇文章:SpringCloud 入門實戰(11)--Zipkin 使用一(Zipkin 簡介)。本文主要介紹 Zipkin 的基本使用,文中所使用到的軟件版本:Zipkin 2.23.二、Spring Boot 2.3.11.RELEASE、Spring Cloud Hoxton.SR八、jdk1.8.0_181。html

一、Zipkin 安裝

1.一、下載應用包

 在能連外網的機器上執行:java

curl -sSL https://zipkin.io/quickstart.sh | bash -s

執行命令完成後,就會下載 Zipkin 的應用包:zipkin.jar。mysql

1.二、存儲方式

Zipkin 能夠把數據保存在內存、MySQL、Cassandra、Elasticsearch 中,詳細的配置說明可參考官方說明:https://github.com/openzipkin/zipkin/tree/master/zipkin-server。git

1.2.一、數據保存在內存中

參數:github

MEM_MAX_SPANS:保存的最大 span 的數量,超過了會把最先的 span 刪除spring

啓動命令例子:sql

MEM_MAX_SPANS=1000000 java -Xmx1G -jar zipkin.jar

1.2.二、數據保存在 MySQL 中

該種存儲方式已不推薦使用,在數據量大的時候,查詢較慢。數據庫

數據庫初始化腳本:https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql-v1/src/main/resources/mysql.sqlexpress

--
-- 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 License
-- 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.sql

參數:apache

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.三、採集方式

1.3.一、HTTP Collector

HTTP 方式默認開啓,經過 POST /api/v1/spans 和 POST /api/v2/spans 地址接受數據;相關配置以下:

環境變量 屬性 描述
COLLECTOR_HTTP_ENABLED zipkin.collector.http.enabled 是否啓用 HTTP 方式採集數據,默認 true

1.3.二、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

二、Zipkin 使用

這裏主要介紹在 Spring Cloud 中集成 Zipkin;使用起來仍是很方便的,只需引入相關依賴並配置 Zipkin 的地址便可,而後就會把請求的監控數據發往 Zipkin。

2.一、Http 方式發送數據到 Zipkin

2.1.一、引入依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

2.1.二、application.yml

配置 Zipkin 地址

spring:
  zipkin:
    base-url: http://10.40.100.60:9411/
    enabled: true

2.二、Kafka 方式發送數據到 Zipkin

啓動 Kafka 並建立 Topic:zipkin;配置 Zipkin 的服務端從 Kafka 接受數據;參見 1.3.2

2.2.一、引入依賴

<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.二、application.yml

spring:
  zipkin:
    enabled: true
    sender:
      type: kafka
    kafka:
      topic: zipkin
  kafka:
    bootstrap-servers: 10.40.100.69:9092

2.三、監控 MySQL 操做

這裏假設在 2.1 Spring Cloud 項目的基礎再進一步對 MySQL 的操做進行監控。

2.3.一、引入依賴

<dependency>
    <groupId>io.zipkin.brave</groupId>
    <artifactId>brave-instrumentation-mysql8</artifactId>
    <version>5.13.3</version>
</dependency>

2.3.二、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

三、測試

前臺訪問 scdemo-client 服務,在 scdemo-client 中又調用 scdemo-server 服務,在 scdemo-server 執行了兩次查詢 MySQL 的操做。

相關文章
相關標籤/搜索