基于CentOS搭建基于 ZIPKIN 的数据追踪系统
系统要求:CentOS 7.2 64 位操作系统
配置 Java 环境
安装 JDK
Zipkin 使用 Java8
yum install java-1.8.0-openjdk* -y
安装完成后,查看是否安装成功:
java -version
安装 Zipkin
新建目录
mkdir -p /data/release/zipkin && cd "$_"
下载 Zipkin
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
启动 Zipkin
java -jar zipkin.jar
Zipkin 默认监听 9411 端口, 使用浏览器访问 http://118.89.65.22:9411 即可看到 Zipkin 自带的图形化界面(外网:请确认此CVM有外网IP且安全组已开通9411端口)。
配置 MySQL 数据持久化方案
Zipkin 支持的持久化方案很多,如: Cassandra, MySQL, Elasticsearch。本实验使用 MySQL 5.7 作为数据持久化方案。
安装 MySQL 5.7
使用 Ctrl + C
退出上个步骤的 Java 进程并下载 rmp 包
wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
安装 rpm 包
rpm -Uvh mysql57-community-release-el7-9.noarch.rpm
安装 MySQL
yum install mysql-community-server -y
启动 MySQL 服务
systemctl start mysqld.service
设置 MySQL 密码
获取 root 临时密码(下面命令中的密码是教程为您自动生成的,为了方便实验的进行,不建议使用其它密码。如果设置其它密码,请把密码记住,在后续的步骤会使用到)
grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}'
使用上一步的获得的临时密码登入 MySQL
mysql -uroot -p
设置 MySQL 账户 root 密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Xx$Zipkin2017';
退出 MySQL, 回到 Bash shell
exit;
初始化 Zipkin 数据库
编写初始化脚本
请在 /data/release/zipkin
目录下创建 zipkin_init.sql,参考下面的内容。
1 CREATE TABLE IF NOT EXISTS zipkin_spans ( 2 `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', 3 `trace_id` BIGINT NOT NULL, 4 `id` BIGINT NOT NULL, 5 `name` VARCHAR(255) NOT NULL, 6 `parent_id` BIGINT, 7 `debug` BIT(1), 8 `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL', 9 `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query' 10 ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; 11 12 ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`) COMMENT 'ignore insert on duplicate'; 13 ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`) COMMENT 'for joining with zipkin_annotations'; 14 ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds'; 15 ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames'; 16 ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range'; 17 18 CREATE TABLE IF NOT EXISTS zipkin_annotations ( 19 `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', 20 `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id', 21 `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id', 22 `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1', 23 `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB', 24 `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation', 25 `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp', 26 `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null', 27 `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address', 28 `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null', 29 `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null' 30 ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; 31 32 ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate'; 33 ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans'; 34 ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds'; 35 ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames'; 36 ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces'; 37 ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces'; 38 ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job'; 39 40 CREATE TABLE IF NOT EXISTS zipkin_dependencies ( 41 `day` DATE NOT NULL, 42 `parent` VARCHAR(255) NOT NULL, 43 `child` VARCHAR(255) NOT NULL, 44 `call_count` BIGINT 45 ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; 46 47 ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);
登录 Mysql
mysql -u root --password='Xx$Zipkin2017'
创建 Zipkin 数据库
create database zipkin;
切换数据库
use zipkin;
初始化表及索引
source /data/release/zipkin/zipkin_init.sql
执行以下命令会看到zipkin_annotations
, zipkin_dependencies
, zipkin_spans
三张数据表,说明初始化成功了
show tables;
退出 MySQL, 回到 Bash shell
exit
启动 Zipkin
注: 此处默认使用实验生成的密码
cd /data/release/zipkin STORAGE_TYPE=mysql MYSQL_HOST=localhost MYSQL_TCP_PORT=3306 MYSQL_DB=zipkin MYSQL_USER=root MYSQL_PASS='Xx$Zipkin2017' \ nohup java -jar zipkin.jar &
创建具有数据上报能力的Demo
搭建 NodeJS 环境
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash - yum install nodejs -y
创建Demo目录
创建/data/release/service_a目录
mkdir -p /data/release/service_a && cd "$_"
使用 NPM 安装相关依赖
请在 /data/release/service_a
目录下创建并编辑 package.json,参考下面的内容。
1 { 2 "name": "service_a", 3 "version": "1.0.0", 4 "description": "", 5 "main": "index.js", 6 "scripts": {}, 7 "author": "", 8 "license": "ISC", 9 "dependencies": { 10 "express": "^4.15.3", 11 "zipkin": "^0.7.2", 12 "zipkin-instrumentation-express": "^0.7.2", 13 "zipkin-transport-http": "^0.7.2" 14 } 15 }
安装相关依赖
npm install
创建并编辑 app.js
请在 /data/release/service_a
目录下创建 app.js,参考下面的内容。
示例代码:/data/release/service_a/app.js
1 const express = require('express'); 2 const {Tracer, ExplicitContext, BatchRecorder} = require('zipkin'); 3 const {HttpLogger} = require('zipkin-transport-http'); 4 const zipkinMiddleware = require('zipkin-instrumentation-express').expressMiddleware; 5 6 const ctxImpl = new ExplicitContext(); 7 const recorder = new BatchRecorder({ 8 logger: new HttpLogger( { 9 endpoint: 'http://127.0.0.1:9411/api/v1/spans' 10 }) 11 }); 12 13 const tracer = new Tracer({ctxImpl, recorder}); 14 15 const app = express(); 16 17 app.use(zipkinMiddleware({ 18 tracer, 19 serviceName: 'service-a' 20 })); 21 22 app.use('/', (req, res, next) => { 23 res.send('hello world'); 24 }); 25 26 app.listen(3000, () => { 27 console.log('service-a listening on port 3000!') 28 });
启动服务
node app.js
该服务监听 3000 端口, 使用浏览器访问 http://118.89.65.22:3000 后,看到“hello world” 的文本字样说明服务已经正常工作。
部署完成
查看采集到的追踪数据
大功告成
恭喜您已经完成了搭建基于 ZIPKIN 的数据追踪系统的学习。