SpringCloud之Sleuth和Zipkin笔记

1. 概念

Sleuth+Zipkin是分布式链路追踪技术中的一种,其中重要的概念包含:

Trace:一次访问的链路
TraceID:链路的唯一标识
Span:链路中节点之间的交互
SpanID:一次交互的唯一标识

Sleuth:通过日志的方式记录踪迹数据
Zipkin:收集Sleuth提供的数据,进行整合,展示数据

2. 快速入门(Sleuth)

2.1 引入依赖

<!--链路追踪-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

2.2 修改yml配置

#分布式链路追踪
logging:
 level:
  org.springframework.web.servlet.DispatcherServlet: debug
  org.springframework.cloud.sleuth: debug

2.3 验证效果
发现打印的日志中有新内容:
在这里插入图片描述
解析:在这里插入图片描述
红框中从左到右依次表示:实例id,traceId,spanId,是否加入zipkin聚合

3. 快速入门(ZipKin服务端)

3.0 注意
以下内容为springboot2.2.x版本之前的实现方式,ZipKin在springboot2.2.x之后需通过下载Zipkin服务端的jar包,用java命令直接启动即可。

3.1 引入依赖

<!--zipkin-server的依赖坐标-->
<dependency>
 <groupId>io.zipkin.java</groupId>
 <artifactId>zipkin-server</artifactId>
 <version>2.12.3</version>
 <exclusions>
<!--排除掉log4j2的传递依赖,避免和springboot依赖的⽇
志组件冲突-->
  <exclusion>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starterlog4j2</artifactId>
  </exclusion>
 </exclusions>
</dependency>
<!--zipkin-server ui界⾯依赖坐标-->
<dependency>
 <groupId>io.zipkin.java</groupId>
 <artifactId>zipkin-autoconfigure-ui</artifactId>
 <version>2.12.3</version>
</dependency>

3.2 添加注解
启动类加入@EnableZipkinServer注解

3.3 修改配置

server:
 port: 9411
management:
 metrics:
  web:
   server:
    auto-time-requests: false # 关闭⾃动检测请求

3.4 验证
访问localhost:9411,可以看到zipkin服务端
在这里插入图片描述

4. 快速入门(ZipKin客户端)

4.1 引入依赖

<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>

4.2 修改配置

spring:
 zipkin:
  base-url: http://127.0.0.1:9411 # zipkin server的请求地址
  sender:
   # web 客户端将踪迹⽇志数据通过⽹络请求的⽅式传送到服务端,另外还有配置
   # kafka/rabbit 客户端将踪迹⽇志数据传递到mq进⾏中转
   type: web
 sleuth:
  sampler:
   # 采样率 1 代表100%全部采集 ,默认0.1 代表10% 的请求踪迹数据会被采集
   # ⽣产环境下,请求量⾮常⼤,没有必要所有请求的踪迹数据都采集分析,对于⽹络包括server端压⼒都是⽐较⼤的,可以配置采样率采集⼀定⽐例的请求的踪迹数据进⾏分析即可
   probability: 1

5. ZipKin数据持久化(mysql)

5.1 执行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`);

5.2 修改配置或启动命令

2.2.x之前版本:
spring:
 datasource:
  driver-class-name: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/zipkin?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
  username: root
  password: 123456
  druid:
   initialSize: 10
   minIdle: 10
   maxActive: 30
   maxWait: 50000
# 指定zipkin持久化介质为mysql
 zipkin:
  storage:
   type: mysql
2.2.x之后版本(jar包版本):

java -jar zipkin-server-2.12.9-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=zipkin --MYSQL_USER=你的mysql账号 --MYSQL_PASS=你的mysql密码

  1. ZipKin数据持久化(elasticsearch版本)
    6.1 修改启动命令

java -jar zipkin-server-2.12.9-exec.jar --STORAGE_TYPE=elasticsearch --ES-HOST=localhost:9200

注意:elasticsearch版本不要太高,建议elasticsearch-6.8.4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值