Spring Cloud Sleuth + Twitter Zipkin(开源了)
阿里巴巴 “鹰眼”
大众点评 “CAT”
美团 “Mtrace”
京东 “Hydra”
新浪 “Watchman”
Apache skywalking(开源了)
一、介绍
二、搭建Sleuth + Zipkin
1.在需要链路追踪的服务端引入依赖
<!--链路追踪--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
2.为每一个微服务都修改application.yml配置文件,修改日志级别
# 分布式链路追踪 logging: level: org.springframework.web.servlet.DispatcherServlet: debug org.springframework.cloud.sleuth: debug
启动项目发送请求
[服务名称,TraceID,SpanID,有没有发送到聚合服务器(true发送了,false没有)]
3.结合Zipkin展示追踪数据
Zipkin包括Zipkin Server 和 Zipkin Client两部分,Zipkin Server是一个单独的服务,Zipkin Client就是具体的微服务
4.创建Zipkin Server端
创建一个单独的SpringBoot服务,不需要配置注册中心
依赖
<dependencies> <!--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-starter-log4j2</artifactId> </exclusion> </exclusions> </dependency> <!--zipkin-server ui界⾯依赖坐标--> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.12.3</version> </dependency> </dependencies>
启动类
application.yml配置文件
server: port: 9411 management: metrics: web: server: auto-time-requests: false # 关闭自动检测请求
启动服务访问http://localhost:9411/zipkin/
5.Zipkin Client构建(在具体的微服务中修改)
pom文件中添加Zipkin依赖
<!--zipkin client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
application.yml中添加对Zipkin Server的引用
spring: zipkin: base-url: http://127.0.0.1:9411 # zipkin server请求地址 sender: # web 客户端将数据通过网络请求的方式传送到服务端 # kafa/rabbit 客户端将踪迹日志数据传递到mq进行中转 type: web sleuth: sampler: probability: 1 # 采样率 1代表100%全部采集,默认0.1
启动服务发送调用请求
以上链路追踪的信息都是存储在内存当中的,Zipkin Server服务重启之后就没有了
6.链路追踪数据持久化到mysql中
git上zipkin官方自带的sql
CREATE TABLE IF NOT EXISTS zipkin_spans (
`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If nonzero, this means the trace uses 128 bit traceIds instead of64 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 microsused for endTs query and to implement TTL' ,`duration` BIGINT COMMENT 'Span.duration(): micros usedfor minDuration and maxDuration query' ,PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET =utf8COLLATE 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 'forgetTraces 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 'forgetTraces ordering and range' ;CREATE TABLE IF NOT EXISTS zipkin_annotations (`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If nonzero, this means the trace uses 128 bit traceIds instead of64 bit' ,`trace_id` BIGINT NOT NULL COMMENT 'coincides withzipkin_spans.trace_id' ,`span_id` BIGINT NOT NULL COMMENT 'coincides withzipkin_spans.id' ,`a_key` VARCHAR ( 255 ) NOT NULL COMMENT'BinaryAnnotation.key or Annotation.value if type == -1' ,`a_value` BLOB COMMENT 'BinaryAnnotation.value(), whichmust 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 whenBinary/Annotation.endpoint is null' ,`endpoint_ipv6` BINARY( 16 ) COMMENT 'Null whenBinary/Annotation.endpoint is null, or no IPv6 address' ,`endpoint_port` SMALLINT COMMENT 'Null whenBinary/Annotation.endpoint is null' ,`endpoint_service_name` VARCHAR ( 255 ) COMMENT 'Null whenBinary/Annotation.endpoint is null') ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET =utf8COLLATE utf8_general_ci;ALTER TABLE zipkin_annotations ADD UNIQUEKEY(`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 withzipkin_spans' ;ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`,`trace_id`) COMMENT 'for getTraces/ByIds' ; ALTER TABLE zipkin_annotations ADDINDEX(`endpoint_service_name`) COMMENT 'for getTraces andgetServiceNames' ;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 =utf8COLLATE utf8_general_ci;
在Zipkin Server服务pom文件中引入相关依赖
<!--zipkin针对mysql持久化的依赖--> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-storage-mysql</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--操作数据库需要事务控制--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.7</version> </dependency>
修改Zipkin Server服务的application.yml配置文件
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
启动类中注入事务管理器
启动之后,然后服务之间调用,每次请求记录都会记录到mysql数据库里