1.链路追踪简介
-
链路追踪系统
- 分布式应用架构虽然满足了应用横向扩展的需求,但是运维和诊断的过程变得越来越复杂,例如会遇到接口诊断困难、应用性能诊断复杂、架构分析复杂等难题,传统的监控工具并无法满足,分布式链路系统由此诞生
-
核心:将一次请求分布式调用,使用GPS定位串起来,记录每个调用的耗时、性能等日志,并通过可视化工具展示出来
-
注意:AlibabaCloud全家桶还没对应的链路追踪系统,我们使用Sleuth和zipkin(内部使用的鹰眼)
2.链路追踪组件Sleuth
-
什么是Sleuth
-
一个组件,专门用于记录链路数据的开源组件
-
文档:Spring Cloud Sleuth
-
案例及其说明
gen-gateway-service,5894f5583a0845e7,5894f5583a0845e7,true第一个值:spring.application.name的值 第二个值:Sleuth生成的一个ID叫Trace ID,多个Span ID 第三个值:Span Id,基本的工作单元,获取元数据,如发送一个http 第四个值:是否要将该信息输出到zipkin服务中来收集和展示
-
-
项目引入实战
-
各个微服务添加依赖
<!--链路追踪sleuth--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
-
3.链路追踪组件zipkin
-
什么是zipkin
- 官网:OpenZipkin · A distributed tracing system
- 大规模分布式系统的APM工具(Application Performance Management),基于Google Dapper的基础实现,和Sleuth结合可以提供可视化web界面分析调用链路耗时情况
-
同类产品
- 鹰眼(EagleEye)
- CAT
- Twitter开源zipkin,结合Sleuth
- Pinpoint,运用JavaAgent字节码增强技术
-
使用:下载jar包并启动jar,访问地址:http://127.0.0.1:9411/zipkin/
-
zipkin组成:Collector、Storage、Restful API、Web UI组成
4.zipkin+Sleuth整合
-
原理
- Sleuth收集跟踪信息通过http请求发送给zipkin server
- zipkin server进行跟踪信息的存储以及提供Rest API即可
- zipkin UI调用其API接口进行数据展示默认存储是内存,可以用mysql或者elasticsearch等存储
-
实战代码
-
添加依赖
<!--链路追踪可视化工具zipkin--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
-
配置文件
spring:application:name: gen-gateway-servicecloud:# nacos注册中心地址nacos:discovery:server-addr: 114.132.67.61:18848# zipkin配置zipkin:# zipkin地址base-url: http://127.0.0.1:9411/# 开启服务发现discovery-client-enabled: true# sleuth配置sleuth:sampler:#采样百分比,默认0.1即10%,这里测试配置1即100%全部记录;在分布式系统中,过于频繁的采样会影响性能,所以这里配置需采用一个合适的值probability: 1.0
-
5.zipkin持久化mysql
-
创建数据库SQL脚本
-- -- Copyright The OpenZipkin Authors -- SPDX-License-Identifier: Apache-2.0 --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;
-
启动zipkin命令
java -jar zipkin-server-2.22.2-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=127.0.0.1 --MYSQL_TCP_PORT=3306 --MYSQL_DB=gen-zipkin --MYSQL_USER=root --MYSQL_PASS=密码