Zipkin 分布式數據追蹤系統


Zipkin 是一個分布式數據追蹤系統,適用於微服務架構下的調用鏈路數據采集及分析工作。

可通過一個 Web 前端輕松的收集和分析數據,例如用戶每次請求服務的處理時間等,可方便的監測系統中存在的瓶頸。

一、配置 Java 環境 安裝 JDK

Zipkin 使用 Java8
yum install java
-1.8.0-openjdk* -y java -version

二、安裝 Zipkin 

1、創建zipkin安裝目錄
mkdir -p /opt/server/zipkin && cd "$_"

2、下載 Zipkin wget
-O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
3、啟動 Zipkin (nohup & 可以進行后台運行 ) java
-jar zipkin.jar

   Zipkin 默認監聽 9411 端口

三、配置 MySQL 數據持久化 

  1、Zipkin 支持的持久化方案很多: Cassandra, MySQL, Elasticsearch。

wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
rpm -Uvh mysql57-community-release-el7-9.noarch.rpm
yum install mysql-community-server -y
systemctl start mysqld.service

設置 mysql 密碼 創建一個zipkin 庫; mysql
-uroot -p
> ALTER USER
'root'@'localhost' IDENTIFIED BY 'passwd';

> create database zipkin;

exit

  2、創建 Zipkin初始化文件  zipkin_init.sql 

     創建了 zipkin_annotations, zipkin_dependencies, zipkin_spans 三張數據表

# cat /opt/server/zipkin/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`);

  3、初始化導入:

mysql -u root --password='passwd'

> use zipkin;
> source /opt/server/zipkin/zipkin_init.sql
> show tables;
> exit

四、重新啟動 zipkin

cd /opt/server/zipkin
STORAGE_TYPE=mysql MYSQL_HOST=localhost MYSQL_TCP_PORT=3306 MYSQL_DB=zipkin MYSQL_USER=root MYSQL_PASS='passwd' \
nohup java -jar zipkin.jar &

五、創建一個dome 示例

  1、搭建 NodeJS 環境

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

  2、創建 /opt/server/service_testing 工作目錄

mkdir -p /opt/server/service_testing 

 

  3、在 /opt/server/service_testing 目錄下創建並編輯 package.json

# cat /opt/server/service_testing/package.json
{
"name": "service_testing", "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" } }

  4、安裝相關依賴

# npm install

  5、創建並編輯 app.js

# cat /opt/server/service_testing/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-testing' })); app.use('/', (req, res, next) => { res.send('hello one'); }); app.listen(3000, () => { console.log('service-testing listening on port 3000!') });

  6、啟動服務 (監聽 3000 端口)http://IP:3000

# node app.js

六、zipkin 訪問 http://IP:9411 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM