SpringCloudAlibaba学习笔记(三)--链路追踪 Zipkin+Sleuth

SpringCloudAlibaba技术栈中,没有提供自己的链路追踪技术,所以采用Zipkin+Sleuth来实现链路追踪.

Sleuth入门:

SpringCloudAlibaba学习笔记(三)--链路追踪 Zipkin+Sleuth

 

因为进行链路追踪是整条链路,即包含所有服务.所以在父工程pom中添加依赖::

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

接着运行项目,访问服务.会发现日志输出时,多了一些内容:

例如: [shop-product,7fcf4220c6b74f50,5a490e91105a0b07].

说明:

第一个参数是服务名称;

第二个参数是Trace Id,即链路Id,同一条访问线路上的链路Id相同. 访问线路即是实现一个功能所要经过的服务.

第三个参数是:Span Id.表示一个基本的工作单元.是自动生成的.

注: spring cloud Sleuth 3.0以前可能还会多出一个参数,是一个布尔类型的值.表示是否要将该信息输出到 Zipkin 等外部服务中.

Zipkin:

SpringCloudAlibaba学习笔记(三)--链路追踪 Zipkin+Sleuth

SpringCloudAlibaba学习笔记(三)--链路追踪 Zipkin+Sleuth

SpringCloudAlibaba学习笔记(三)--链路追踪 Zipkin+Sleuth

Zipkin也是一个单独的服务,所以我们去github上下载服务端jar包,在本地运行.

下载地址: zipkin/zipkin-server at master · openzipkin/zipkin · GitHub

注意下载jar包: SpringCloudAlibaba学习笔记(三)--链路追踪 Zipkin+Sleuth

下载好后本地运行: 

 java -jar zipkin-server-2.23.8-exec.jar

接着是在父工程中引入依赖::

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>

配置需要进行链路追踪的服务的yml文件:

spring:
  application:
    name: shop-product
      #Zipkin+Sleuth实现链路追踪
      #Zipkin
  zipkin:
        base-url: http://127.0.0.1:9411/  #zipkin的请求地址,将链路追踪信息推送上去
        discovery-client-enabled: false #让Nacos把Zipkin的请求地址当成一个URL,而不是服务名
        #Sleuth
  sleuth:
        sampler:
          probability: 1.0 #采样的百分比  在大量数据下,建议调低

然后启动自己的服务访问试一下,在zipkin后台即可看到链路追踪的信息.

Sleuth+Zipkin实现链路追踪:

SpringCloudAlibaba学习笔记(三)--链路追踪 Zipkin+Sleuth

 

Zipkin数据持久化:

使用MySQL持久化数据:

建数据库表:

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`) COMMENT 'ignore insert on duplicate';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`) COMMENT 'for joining with zipkin_annotations';
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(`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';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces';
 
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`);

启动Zipkin命令:

java -jar zipkin-server-2.23.8-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=123456

使用Elasticsearch持久化:

下载地址:Get Started with Elasticsearch, Kibana, and the Elastic Stack | Elastic

SpringCloudAlibaba学习笔记(三)--链路追踪 Zipkin+Sleuth

 

上一篇:网页头部固定方法


下一篇:Zipkin部署与SpringBoot集成