操做系統: CentOS 7.2 64位java
Zipkin是一款開源的分佈式實時數據追蹤系統(Distributed Tracking System),基於 Google Dapper的論文設計而來。其主要功能是彙集來自各個異構系統的實時監控數據。node
故障快速定位 經過分析調用鏈,能夠將一次請求的邏輯軌跡完整清晰的展現出來,經過在開發中在業務日誌中添加調用鏈ID,能夠經過調用鏈結合業務日誌快速定位錯誤信息。mysql
服務可用性 經過分析各個環節的平均時延,QPS等信息,能夠找到系統的薄弱環節,對一些模塊作調整,例如數據冗餘、鏈路可用等。sql
性能分析 在調用鏈的各個環節分別添加調用時延,能夠分析系統的性能瓶頸,進行有針對性的優化。shell
安裝數據追蹤軟件Zipkin所須要的JDK環境數據庫
執行如下命令安裝JDKexpress
yum install java-1.8.0-openjdk* -y
複製代碼
安裝完成以後,檢查是否安裝成功npm
java -version
複製代碼
新建存放ZIPkin的目錄並進入此目錄json
mkdir -p /data/release/zipkin && cd "$_"
複製代碼
下載Zipkinapi
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端口是否已開==
Zipkin支持的持久化方案比較多,如: MySQL、 Elasticsearch。咱們使用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
複製代碼
獲取 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;
複製代碼
編寫初始化腳本
在目錄/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數據庫
create database zipkin
複製代碼
切換數據庫
use zipkin
複製代碼
初始化表及索引
source /data/release/zipkin/zipkin_init.sql
複製代碼
檢查是否有生成zipkin_annotations, zipkin_dependencies, zipkin_spans 三張數據表
show tables;
複製代碼
退出 MySQL, 回到 Bash shell
exit;
複製代碼
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 &
複製代碼
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
yum install nodejs -y
複製代碼
建立/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
複製代碼
查看採集到的追蹤數據
使用瀏覽器訪問 http://IP:9411,便可看到剛纔訪問產生的追蹤數據。
微信公衆號ID:jsj201501
微信公衆號名稱:瞎說開發那些事
感謝您的關注和閱讀,但願這篇文章能爲您帶來幫助。
歡迎轉載與分享,也請註明出處。
若是您有須要瞭解的關於Java開發、RPA的等內容,也能夠給我留言或發郵件 (shexd1001@gmail.com)。
本文做者:折(she) 向東
微信號:wxxdong2102
識別如下二維碼,能夠與做者進行更爲深刻的交流。