鏈路追蹤工具Zipkin整合

前言

Zipkin 是一個開放源代碼分布式的跟蹤系統(tǒng),每個服務(wù)向zipkin報告計時數(shù)據(jù),zipkin會根據(jù)調(diào)用關(guān)系通過Zipkin UI生成依賴關(guān)系圖。

Zipkin提供了可插拔數(shù)據(jù)存儲方式:In-Memory、MySql、Cassandra以及Elasticsearch。為了方便在開發(fā)環(huán)境我直接采用了In-Memory方式進行存儲,生產(chǎn)數(shù)據(jù)量大的情況則推薦使用Elasticsearch。

基本術(shù)語

  1. Span:基本工作單元,例如,在一個新建的span中發(fā)送一個RPC等同于發(fā)送一個回應(yīng)請求給RPC,span通過一個64位ID唯一標(biāo)識,trace以另一個64位ID表示,span還有其他數(shù)據(jù)信息,比如摘要、時間戳事件、關(guān)鍵值注釋(tags)、span的ID、以及進度ID(通常是IP地址)
    span在不斷的啟動和停止,同時記錄了時間信息,當(dāng)你創(chuàng)建了一個span,你必須在未來的某個時刻停止它。
  2. Trace:一系列spans組成的一個樹狀結(jié)構(gòu),例如,如果你正在跑一個分布式大數(shù)據(jù)工程,你可能需要創(chuàng)建一個trace。
  3. Annotation:用來及時記錄一個事件的存在,一些核心annotations用來定義一個請求的開始和結(jié)束
    • cs - Client Sent -客戶端發(fā)起一個請求,這個annotion描述了這個span的開始
    • sr - Server Received -服務(wù)端獲得請求并準(zhǔn)備開始處理它,如果將其sr減去cs時間戳便可得到網(wǎng)絡(luò)延遲
    • ss - Server Sent -注解表明請求處理的完成(當(dāng)請求返回客戶端),如果ss減去sr時間戳便可得到服務(wù)端需要的處理請求時間
    • cr - Client Received -表明span的結(jié)束,客戶端成功接收到服務(wù)端的回復(fù),如果cr減去cs時間戳便可得到客戶端從服務(wù)端獲取回復(fù)的所有所需時間
      將Span和Trace在一個系統(tǒng)中使用Zipkin注解的過程圖形化:
zipkin.png

構(gòu)建zipkin服務(wù)端

在spring Cloud為Finchley版本時,如果只需要默認的實現(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包

通過以下命令啟動服務(wù),默認INFO級別可以不設(shè)置logging

java -jar zipkin-server-2.12.2-exec.jar --logging.level.zipkin2=INFO

服務(wù)啟動后默認可以通過9411端口訪問zipkin的監(jiān)控頁面

http://127.0.0.1:9411

通過ElasticSearch進行存儲

默認啟動方式會將日志數(shù)據(jù)存在內(nèi)存中,一旦服務(wù)重啟會清空數(shù)據(jù),建議使用es進行持久化存儲。啟動示例如下:

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會在es中創(chuàng)建以zipkin開頭日期結(jié)尾的index,并且默認以天為單位分割,使用該存儲模式時,zipkin中的依賴信息會無法顯示,需要通過zipkin-dependencies工具包計算。

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 &

下載完成后通過上述命令啟動zipkin-dependencies,這里要注意的是程序只會根據(jù)當(dāng)日的zipkin數(shù)據(jù)實時計算一次依賴關(guān)系,并以索引zipkin:dependency-2019-03-14方式存入es中,然后就退出了,因此要做到實時更新依賴的話需要自己想辦法實現(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時代表全部強求都需要采樣。Sleuth默認采樣算法的實現(xiàn)是Reservoir sampling,具體的實現(xiàn)類是PercentageBasedSampler,默認的采樣比例為: 0.1(即10%)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 原來今年我三姐回家過年,不知道二姐回不回家,他家離得更近,應(yīng)該是要回去的,四姐仍在廣東過年,我哥過完年以后,也要回...
    魂歸瀟湘閱讀 160評論 0 0
  • 今天早上來上班的時候,居然看到一輛摩拜,就在我非常想要找摩拜的時候,他就出現(xiàn)在我的面前,當(dāng)時他是沒有上鎖的,然后我...
    Richard1015閱讀 348評論 0 1
  • 前言:這是一篇轉(zhuǎn)載文,確實寫的不錯,今天分享給大家,希望大家能集百家之長,學(xué)到更多有用的知識~ 摘要:這是一篇20...
    強哥科技興閱讀 513評論 0 0
  • “有幸”生活圈里面遇見幾個愛折騰的人。 對于我們這種被21世紀(jì)“快餐文化”、“碎片化”影響的一代人,讓我看到超過三...
    小弟桑pc閱讀 3,225評論 0 0

友情鏈接更多精彩內(nèi)容