springcloud-zipkin

  • 前言

    • Zipkin 是一個開放源代碼分佈式的跟蹤系統,每個服務向 zipkin 報告計時數據,zipkin 會根據調用關係通過 Zipkin UI 生成依賴關係圖。
    • Zipkin 提供了可插拔數據存儲方式:In-Memory、MySql、Cassandra 以及 Elasticsearch 。爲了方便在開發環境我直接採用了 In-Memory 方式進行存儲,生產數據量大的情況則推薦使用 Elasticsearch。
  • 基本術語

    • Span:基本工作單元,例如,在一個新建的 span 中發送一個 RPC 等同於發送一個迴應請求給 RPC,span 通過一個64位ID唯一標識,trace 以另一個64位ID表示,span 還有其他數據信息,比如摘要、時間戳事件、關鍵值註釋(tags)、span 的ID、以及進度ID(通常是IP地址)
      span 在不斷的啓動和停止,同時記錄了時間信息,當你創建了一個 span,你必須在未來的某個時刻停止它。
    • Trace:一系列 spans 組成的一個樹狀結構,例如,如果你正在跑一個分佈式大數據工程,你可能需要創建一個 trace。
    • Annotation:用來及時記錄一個事件的存在,一些核心 annotations 用來定義一個請求的開始和結束
      • cs - Client Sent -客戶端發起一個請求,這個 annotion 描述了這個 span 的開始
      • sr - Server Received -服務端獲得請求並準備開始處理它,如果將其sr減去cs時間戳便可得到網絡延遲
      • ss - Server Sent -註解表明請求處理的完成(當請求返回客戶端),如果ss減去sr時間戳便可得到服務端需要的處理請求時間
      • cr - Client Received -表明 span 的結束,客戶端成功接收到服務端的回覆,如果cr減去cs時間戳便可得到客戶端從服務端獲取回覆的所有所需時間
        將 Span 和 Trace 在一個系統中使用 Zipkin 註解的過程圖形化:
 
 
  • 構建zipkin服務端

    • 在spring Cloud爲Finchley 版本時,如果只需要默認的實現,則不需要自己構建 Zipkin Server了,只需要下載jar即可,下載地址:

https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

  • 這裏我下載的是zipkin-server-2.12.2-exec.jar版本的jar包
  • 通過以下命令啓動服務,默認 INFO 級別可以不設置 logging

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

  • 服務啓動後默認可以通過9411端口訪問zipkin的監控頁面

http://127.0.0.1:9411

  • 通過ElasticSearch進行存儲

    • 默認啓動方式會將日誌數據存在內存中,一旦服務重啓會清空數據,建議使用es進行持久化存儲。啓動示例如下:

STORAGE_TYPE=elasticsearch ES_HOSTS=http://myhost:9200 java -jar zipkin-server-2.12.2-exec.jar

  • 另外還有一些其它可配置參數
* `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中創建以zipkin開頭日期結尾的 index,並且默認以天爲單位分割,使用該存儲模式時,zipkin 中的依賴信息會無法顯示,需要通過zipkin-dependencies工具包計算。
  • zipkin-dependencies生成依賴鏈

STORAGE_TYPE=elasticsearch ES_HOSTS=127.0.0.1:9200 java -jar zipkin-dependencies-2.0.8.jar &

  • 下載完成後通過上述命令啓動zipkin-dependencies,這裏要注意的是程序只會根據當日的zipkin數據實時計算一次依賴關係,並以索引zipkin:dependency-2019-03-14方式存入es中,然後就退出了,因此要做到實時更新依賴的話需要自己想辦法實現週期性執行zipkin-dependencies
  • 依賴配置

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
  • 由於已經引入了spring-cloud-dependencies,因此可以直接依賴spring-cloud-starter-zipkin
  • 參數配置

spring:
  zipkin:
    base-url: http://127.0.0.1:9411
  sleuth:
    sampler:
      percentage: 1.0
  • 這裏的base-url是zipkin服務端的地址,percentage是採樣比例,設置爲1.0(1也可以)時代表全部強求都需要採樣。Sleuth默認採樣算法的實現是Reservoir sampling,具體的實現類是PercentageBasedSampler,默認的採樣比例爲: 0.1(即10%)。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章