前言
Zipkin 是一個(gè)開放源代碼分布式的跟蹤系統(tǒng)悲龟,每個(gè)服務(wù)向zipkin報(bào)告計(jì)時(shí)數(shù)據(jù)采转,zipkin會(huì)根據(jù)調(diào)用關(guān)系通過Zipkin UI生成依賴關(guān)系圖受扳。
Zipkin提供了可插拔數(shù)據(jù)存儲(chǔ)方式:In-Memory鞋真、MySql均函、Cassandra以及Elasticsearch螟凭。為了方便在開發(fā)環(huán)境我直接采用了In-Memory方式進(jìn)行存儲(chǔ)虚青,生產(chǎn)數(shù)據(jù)量大的情況則推薦使用Elasticsearch。
基本術(shù)語
- Span:基本工作單元螺男,例如棒厘,在一個(gè)新建的span中發(fā)送一個(gè)RPC等同于發(fā)送一個(gè)回應(yīng)請求給RPC,span通過一個(gè)64位ID唯一標(biāo)識(shí)下隧,trace以另一個(gè)64位ID表示奢人,span還有其他數(shù)據(jù)信息,比如摘要淆院、時(shí)間戳事件何乎、關(guān)鍵值注釋(tags)、span的ID、以及進(jìn)度ID(通常是IP地址)
span在不斷的啟動(dòng)和停止支救,同時(shí)記錄了時(shí)間信息抢野,當(dāng)你創(chuàng)建了一個(gè)span,你必須在未來的某個(gè)時(shí)刻停止它各墨。 - Trace:一系列spans組成的一個(gè)樹狀結(jié)構(gòu)指孤,例如,如果你正在跑一個(gè)分布式大數(shù)據(jù)工程欲主,你可能需要?jiǎng)?chuàng)建一個(gè)trace邓厕。
- Annotation:用來及時(shí)記錄一個(gè)事件的存在,一些核心annotations用來定義一個(gè)請求的開始和結(jié)束
- cs - Client Sent -客戶端發(fā)起一個(gè)請求扁瓢,這個(gè)annotion描述了這個(gè)span的開始
- sr - Server Received -服務(wù)端獲得請求并準(zhǔn)備開始處理它详恼,如果將其sr減去cs時(shí)間戳便可得到網(wǎng)絡(luò)延遲
- ss - Server Sent -注解表明請求處理的完成(當(dāng)請求返回客戶端),如果ss減去sr時(shí)間戳便可得到服務(wù)端需要的處理請求時(shí)間
- cr - Client Received -表明span的結(jié)束引几,客戶端成功接收到服務(wù)端的回復(fù)昧互,如果cr減去cs時(shí)間戳便可得到客戶端從服務(wù)端獲取回復(fù)的所有所需時(shí)間
將Span和Trace在一個(gè)系統(tǒng)中使用Zipkin注解的過程圖形化:
構(gòu)建zipkin服務(wù)端
在spring Cloud為Finchley版本時(shí),如果只需要默認(rèn)的實(shí)現(xiàn)伟桅,則不需要自己構(gòu)建Zipkin Server了敞掘,只需要下載jar即可,下載地址:
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
這里我下載的是zipkin-server-2.12.2-exec.jar
版本的jar包
通過以下命令啟動(dòng)服務(wù)楣铁,默認(rèn)INFO級(jí)別可以不設(shè)置logging
java -jar zipkin-server-2.12.2-exec.jar --logging.level.zipkin2=INFO
服務(wù)啟動(dòng)后默認(rèn)可以通過9411端口訪問zipkin的監(jiān)控頁面
http://127.0.0.1:9411
通過ElasticSearch進(jìn)行存儲(chǔ)
默認(rèn)啟動(dòng)方式會(huì)將日志數(shù)據(jù)存在內(nèi)存中玖雁,一旦服務(wù)重啟會(huì)清空數(shù)據(jù),建議使用es進(jìn)行持久化存儲(chǔ)盖腕。啟動(dòng)示例如下:
STORAGE_TYPE=elasticsearch ES_HOSTS=http://myhost:9200 java -jar zipkin-server-2.12.2-exec.jar
另外還有一些其它可配置參數(shù)
* `ES_HOSTS`: A comma separated list of elasticsearch base urls to connect to ex. http://host:9200.
Defaults to "http://localhost:9200".
* `ES_PIPELINE`: Only valid when the destination is Elasticsearch 5+. Indicates the ingest
pipeline used before spans are indexed. No default.
* `ES_TIMEOUT`: Controls the connect, read and write socket timeouts (in milliseconds) for
Elasticsearch Api. Defaults to 10000 (10 seconds)
* `ES_MAX_REQUESTS`: Only valid when the transport is http. Sets maximum in-flight requests from
this process to any Elasticsearch host. Defaults to 64.
* `ES_INDEX`: The index prefix to use when generating daily index names. Defaults to zipkin.
* `ES_DATE_SEPARATOR`: The date separator to use when generating daily index names. Defaults to '-'.
* `ES_INDEX_SHARDS`: The number of shards to split the index into. Each shard and its replicas
are assigned to a machine in the cluster. Increasing the number of shards
and machines in the cluster will improve read and write performance. Number
of shards cannot be changed for existing indices, but new daily indices
will pick up changes to the setting. Defaults to 5.
* `ES_INDEX_REPLICAS`: The number of replica copies of each shard in the index. Each shard and
its replicas are assigned to a machine in the cluster. Increasing the
number of replicas and machines in the cluster will improve read
performance, but not write performance. Number of replicas can be changed
for existing indices. Defaults to 1. It is highly discouraged to set this
to 0 as it would mean a machine failure results in data loss.
* `ES_USERNAME` and `ES_PASSWORD`: Elasticsearch basic authentication, which defaults to empty string.
Use when X-Pack security (formerly Shield) is in place.
* `ES_HTTP_LOGGING`: When set, controls the volume of HTTP logging of the Elasticsearch Api.
Options are BASIC, HEADERS, BODY
注:zipkin會(huì)在es中創(chuàng)建以zipkin開頭日期結(jié)尾的index赫冬,并且默認(rèn)以天為單位分割,使用該存儲(chǔ)模式時(shí)溃列,zipkin中的依賴信息會(huì)無法顯示劲厌,需要通過zipkin-dependencies
工具包計(jì)算。
zipkin-dependencies生成依賴鏈
zipkin-dependencies
基于spark job
來生成全局的調(diào)用鏈听隐,下載地址:https://github.com/openzipkin/zipkin-dependencies
STORAGE_TYPE=elasticsearch ES_HOSTS=127.0.0.1:9200 java -jar zipkin-dependencies-2.0.8.jar &
下載完成后通過上述命令啟動(dòng)zipkin-dependencies
补鼻,這里要注意的是程序只會(huì)根據(jù)當(dāng)日的zipkin數(shù)據(jù)實(shí)時(shí)計(jì)算一次依賴關(guān)系,并以索引zipkin:dependency-2019-03-14
方式存入es中雅任,然后就退出了风范,因此要做到實(shí)時(shí)更新依賴的話需要自己想辦法實(shí)現(xiàn)周期性執(zhí)行zipkin-dependencies
。
依賴配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
由于已經(jīng)引入了spring-cloud-dependencies
沪么,因此可以直接依賴spring-cloud-starter-zipkin
參數(shù)配置
spring:
zipkin:
base-url: http://127.0.0.1:9411
sleuth:
sampler:
percentage: 1.0
這里的base-url
是zipkin服務(wù)端的地址乌企,percentage
是采樣比例,設(shè)置為1.0時(shí)代表全部強(qiáng)求都需要采樣成玫。Sleuth默認(rèn)采樣算法的實(shí)現(xiàn)是Reservoir sampling加酵,具體的實(shí)現(xiàn)類是PercentageBasedSampler
拳喻,默認(rèn)的采樣比例為: 0.1(即10%)。