Fork me on GitHub

搭建基于 ZIPKIN 的数据追踪系统

Zipkin

Zipkin 是一个分布式数据追踪系统,适用于微服务架构下的调用链路数据采集及分析工作。

配置 Java 环境

安装 JDK

Zipkin 使用 Java 8

1
yum install java-1.8.0-openjdk* -y

安装 Zipkin

新建目录

1
mkdir -p /data/release/zipkin && cd "$_"

下载 Zipkin

1
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'

启动 Zipkin

1
java -jar zipkin.jar

Zipkin 默认监听 9411 端口, 使用浏览器访问 http://:9411 即可看到 Zipkin 自带的图形化界面。

配置 MySQL 数据持久化方案

Zipkin 支持的持久化方案很多 ,如: Cassandra,MySQL,Elasticsearch . 本实验使用 MySQL 5.7 作为数据库持久化方案。

### 安装 MySQL 5.7

使用 Ctrl + C 退出上个步骤的 Java 进程并下载 rmp 包。

1
wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

安装 rpm 包

1
rpm -Uvh mysql57-community-release-el7-9.noarch.rpm

安装 MySQL

1
yun install mysql-community-server -y

启动 MySQL 服务

1
systemctl start mysqld.service

设置 MySQL 密码

获取 root 临时密码

1
grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}'

使用上一步的获得的临时密码登入 MySQL

1
mysql -uroot -p

设置 MySQL 账户的 root 密码

1
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Xx$Zipkin2017';

登录 MySQL

1
mysql -u root --password='Xx$Zipkin2017'

创建 Zipkin 数据库

1
create database zipkin;

切换数据库

1
use zipkin;

初始化表及索引

1
source /data/release/zipkin/zipkin_init.sql

执行以下命令会看到zipkin_annotations, zipkin_dependencies, zipkin_spans 三张数据表,说明初始化成功了

1
show tables;

退出 MySQL, 回到 Bash shell

1
exit

启动 Zipkin

注: 此处默认使用实验生成的密码

1
2
3
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 环境

1
2
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
yum install nodejs -y

创建 Demo 目录

创建 /data/release/service_a 目录

1
mkdir -p /data/release/service_a && cd "$_"

使用 NPM 安装相关依赖

请在 /data/release/service_a 目录下创建并编辑package.json, 参考下面的内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"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"
}
}

安装相关依赖

1
npm install

创建并编辑 app.js

请在 /data/release/service_a 目录下创建 app.js ,参考下面的内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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!')
});

启动服务

1
node app.js

该服务监听 3000 端口, 使用浏览器访问 http://:3000 后,看到“hello world” 的文本字样说明服务已经正常工作。

至此,本入门教程已结束,而 Zipkin 的学习只是一个开始,如有兴趣,可尝试搭建一个基于 Kafka + Zookeeper + Elasticsearch 的分布式服务。

坚持原创技术分享,您的支持将鼓励我继续创作
显示 Gitment 评论