存储跟踪数据
Zipkin Server默认时间追踪数据信息保存到内存,这种方式不适合生产环境。因为一旦Service关闭重
启或者服务崩溃,就会导致历史数据消失。Zipkin支持将追踪数据持久化到mysql数据库或者存储到
elasticsearch中。这里已mysql为例。
准备数据库
可以从官网找到Zipkin Server持久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, `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客户端和Server之间是使用HTTP请求的方式进行通信(即同步的请求方式),在
网络波动,Server端异常等情况下可能存在信息收集不及时的问题。Zipkin支持与rabbitMQ整合完成异
步消息传输。
加了MQ之后,通信过程如下图所示:
服务端启动
[root@192 ~]# docker pull rabbitmq:3.7.16-management 3.7.16-management: Pulling from library/rabbitmq 7413c47ba209: Pull complete 0fe7e7cbb2e8: Pull complete 1d425c982345: Pull complete 344da5c95cec: Pull complete e3f1cd2e2cf6: Pull complete e265667bcecb: Pull complete 50e866cc6374: Pull complete a293da811d82: Pull complete 27da9da6e876: Pull complete ae131a241fa6: Pull complete d575a37fcce7: Pull complete 8f7af4eb047e: Pull complete Digest: sha256:0c7348631da356d980c8a358f67ca2f71074db81d925b40ef0860ddfb14f419e Status: Downloaded newer image for rabbitmq:3.7.16-management [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE rabbitmq latest 2b5cda43d345 5 days ago 151MB rancher/rancher stable 5ebba94410d8 2 months ago 654MB openzipkin/zipkin latest 12ee1ce53834 2 months ago 157MB rabbitmq 3.7.16-management 3f92e6354d11 6 months ago 177MB [root@192 ~]# docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq 3f92e6354d11 68b7159690f59e350c902ad042799a42ee3c80703f17421242e8775ae9a9a669
重启Zipkin
[root@192 ~]# docker run -d > --restart always > -v /etc/localtime:/etc/localtime:ro > -e MYSQL_USER=root > -e MYSQL_PASS=1qaz@WSX > -e MYSQL_HOST=rm-bp1y5jh79h6b3eh9clo.mysql.rds.aliyuncs.com > -e STORAGE_TYPE=mysql > -e MYSQL_DB=zipkin > -e MYSQL_TCP_PORT=3306 > -e RABBIT_ADDRESSES=192.168.180.137:5672 > --net host > --name zipkin > openzipkin/zipkin b58686a7f42469f4050377ecbf628edf882412c3875227764076202d8f161e51 [root@192 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b58686a7f424 openzipkin/zipkin "/busybox/sh run.sh" 4 seconds ago Up 3 seconds zipkin
客户端配置
(1) 配置依赖
<!--sleuth链路追踪--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <!--zipkin依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> </dependency>
导入 spring-rabbit 依赖,是Spring提供的对rabbit的封装,客户端会根据配置自动的生产消息并发送
到目标队列中
(2) 配置消息中间件rabbit mq地址等信息
spring: zipkin: # base-url: http://192.168.180.137:9411/ sender: #type: web type: rabbit sleuth: sampler: probability: 1.0 rabbitmq: host: 192.168.180.137 port: 5672 username: guest password: guest listener: # 这里配置了重试策略 direct: retry: enabled: true simple: retry: enabled: true
修改消息的投递方式,改为 rabbit即可。
添加 rabbitmq的相关配置
(3) 测试
关闭Zipkin Server,并随意请求连接。打开rabbitmq管理后台可以看到,消息已经推送到rabbitmq。
当Zipkin Server启动时,会自动的从rabbitmq获取消息并消费,展示追踪数据
可以看到如下效果:
请求的耗时时间不会出现突然耗时特长的情况
当 ZipkinServer不可用时(比如关闭、网络不通等),追踪信息不会丢失,因为这些信息会保存在
Rabbitmq服务器上,直到Zipkin服务器可用时,再从Rabbitmq中取出这段时间的信息