SpringCloud 整合Sleuth/Zipkin/SkyWalking分布式链路追踪
链路追踪:指一次任务的开始到结束,期间调用的所有系统及耗时都可以完整的记录下来。
一、Sleuth
1. 功能
A. 链路追踪:查看一个请求经过了哪些服务及服务之间调用关系;
B. 性能分析:查看每个采样请求的耗时情况,对耗时长的进行处理;
C. 数据分析,优化链路:对服务频繁调用、并发高的进行业务优化;
D. 可视化错误:对于程序未捕获的异常,结合zipkin查看。
2. 概念
A. Span:一次单独的调用链称为span,是基本工作单位,有Span ID和父Span ID;
B. Trace:由一些列span组成的树状结构,是一次完整的链路,有Trace ID;
C. Annotation:用来记录一个事件的存在,定义一个请求的开始和结束;
Client Send(cs):记录请求的开始;
Server Received(sr):服务端获取请求并准备开始处理;
Server Send(ss):请求服务端处理完成;
Client Received(cr):客户端收到服务端的回复时间。
3. pom.xml Maven依赖
<!-- Sleuth分布式链路追踪 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
4. application.yml 文件配置
spring: sleuth: sampler: # 收集数据百分比,默认0.1(现网配置大小) probability: 1.0
二、Zipkin
1. 组件
A. Collector(收集器):处理从外部系统发送过来的跟踪信息,将这些信息转化为Zipkin内部处理的Span格式,以支持后续的存储、分析和展示等功能;
B. Storage(存储):处理收集器接收到的跟踪信息,默认存储在内存中,还可以储存在MySQL、ElasticSearch中;
C. Web UI:提供WEB界面,展示调用链和服务依赖关系;
D. Restful API:为WEB界面提供查询存储的数据的接口。
2. 服务端部署
B. 访问地址:http://localhost:9411/zipkin
3. 追踪数据存储
A. 追踪数据默认存储在内存中,这种会导致服务一旦重启,数据就会丢失,所以需要替换为持久化方式存储,如MySQL;
B. MySQL方式存储:zipkin.sql
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
4. pom.xml Maven依赖
<!-- Zipkin链路追踪收集 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
或者
<!-- Sleuth分布式链路追踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<!-- Sleuth整合Zipkin界面展示 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
5. application.yml 文件配置
spring: zipkin: # zipkin是否开启,默认开启 enabled: true # 服务端地址 base-url: http://localhost:9411 sender: # 数据传输方式,web以HTTP报文形式向服务器发送 type: web
可参考:zipkin官方源码
三、SkyWalking