环境说明
操作系统: CentOS 7.2 64位
1. Zipkin简介
zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper的论文设计而来。其主要功能是聚集来自各个异构系统的实时监控数据。
2. 应用场景
-
故障快速定位
通过分析调用链,可以将一次请求的逻辑轨迹完整清晰的展示出来,通过在开发中在业务日志中添加调用链ID,可以通过调用链结合业务日志快速定位错误信息。 -
服务可用性
通过分析各个环节的平均时延,QPS等信息,可以找到系统的薄弱环节,对一些模块做调整,例如数据冗余、链路可用等。 -
性能分析
在调用链的各个环节分别添加调用时延,可以分析系统的性能瓶颈,进行有针对性的优化。
3. 安装JDK
安装数据追踪软件Zipkin所需要的JDK环境
执行以下命令安装JDK
yum install java-1.8.0-openjdk* -y
安装完成之后,检查是否安装成功
java -version
4. 安装Zipkin
新建存放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://IP:9411 即可看到 Zipkin 自带的图形化界面。如果没有看到Zipkin的图形化界面,请确保外网的9411端口是否已开==
3. 安装mysql
Zipkin支持的持久化方案比较多,如: MySQL、 Elasticsearch。我们使用MySQL 5.7来作为持久化方案。
1. 下载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
2. 设置MySQL密码
获取 root 临时密码
grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}'
使用上一步的获得的临时密码登入 MySQL
mysql -uroot -p
更改MySQL的root密码为dettRoot$123
ALTER USER 'root'@'localhost' IDENTIFIED BY 'dettRoot$123';
退出MySQL,回到Bash shell
exit;
3. 初始化Zipkin数据库脚本
编写初始化脚本
在目录/data/release/zipkin下创建脚本文件zipkin_init.sql
touch zipkin_init.sql
然后添加如下脚本代码:
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,
`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'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`) COMMENT 'ignore insert on duplicate';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`) COMMENT 'for joining with zipkin_annotations';
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(`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';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces';
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
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);
登录MySQL
mysql -u root --password='dettRoot$123'
创建Zipkin数据库
创建Zipkin数据库
切换数据库
use zipkin
初始化表及索引
source /data/release/zipkin/zipkin_init.sql
检查是否有生成zipkin_annotations, zipkin_dependencies, zipkin_spans 三张数据表
show tables;
退出 MySQL, 回到 Bash shell
exit;
5. 启动 Zipkin
cd /data/release/zipkin
STORAGE_TYPE=mysql MYSQL_HOST=localhost MYSQL_TCP_PORT=3306 MYSQL_DB=zipkin MYSQL_USER=root MYSQL_PASS='dettRoot$123'
nohup java -jar zipkin.jar &
6. 创建具有数据上报能力的Demo
1. 搭建 NodeJS 环境
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
yum install nodejs -y
2. 创建Demo目录
创建/data/release/service_a目录
mkdir -p /data/release/service_a && cd "$_"
使用NPM安装相关依赖,在 /data/release/service_a 目录下创建并编辑 package.json,参考下面的内容。
touch package.json
package.json
{
"name": "service_a",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.3",
"zipkin": "^0.7.2",
"zipkin-instrumentation-express": "^0.7.2",
"zipkin-transport-http": "^0.7.2"
}
}
安装相关依赖
npm install
创建并编辑 app.js
在 /data/release/service_a 目录下创建 app.js,参考下面的内容。
touch app.js
APP.js
const express = require('express');
const {Tracer, ExplicitContext, BatchRecorder} = require('zipkin');
const {HttpLogger} = require('zipkin-transport-http');
const zipkinMiddleware = require('zipkin-instrumentation-express').expressMiddleware;
const ctxImpl = new ExplicitContext();
const recorder = new BatchRecorder({
logger: new HttpLogger( {
endpoint: 'http://127.0.0.1:9411/api/v1/spans'
})
});
const tracer = new Tracer({ctxImpl, recorder});
const app = express();
app.use(zipkinMiddleware({
tracer,
serviceName: 'service-a'
}));
app.use('/', (req, res, next) => {
res.send('hello world');
});
app.listen(3000, () => {
console.log('service-a listening on port 3000!')
});
启动服务
node app.js
7. 部署完成
查看采集到的追踪数据
使用浏览器访问 http://IP:9411,即可看到刚才访问产生的追踪数据。
附1 . 关于微信公众号
微信公众号ID:jsj201501
微信公众号名称:瞎说开发那些事
感谢您的关注和阅读,希望这篇文章能为您带来帮助。
欢迎转载与分享,也请注明出处。
如果您有需要了解的关于Java开发、RPA的等内容,也可以给我留言或发邮件 (shexd1001@gmail.com)。
公众号二维码附2 . 关于本文作者
本文作者:折(she) 向东
微信号:wxxdong2102
识别以下二维码,可以与作者进行更为深入的交流。
1524906118(1).png
网友评论