ZipKin原理學習--ZipKin入門介紹

歸田發表於2018-05-12
ZipKin入門介紹

    Zipkin是一款開源的分散式實時資料追蹤系統(Distributed Tracking System),基於 Google Dapper的論文設計而來,由 Twitter 公司開發貢獻。其主要功能是聚集來自各個異構系統的實時監控資料。分散式跟蹤系統還有其他比較成熟的實現,例如:Naver的Pinpoint、Apache的HTrace、阿里的鷹眼Tracing、京東的Hydra、新浪的Watchman,美團點評的CAT,skywalking等。

ZipKin架構

    ZipKin可以分為兩部分,一部分是zipkin server,用來作為資料的採集儲存、資料分析與展示;zipkin client是zipkin基於不同的語言及框架封裝的一些列客戶端工具,這些工具完成了追蹤資料的生成與上報功能,架構如下:

Zipkin Server主要包括四個模組:
(1)Collector 接收或收集各應用傳輸的資料
(2)Storage 儲存接受或收集過來的資料,當前支援Memory,MySQL,Cassandra,ElasticSearch等,預設儲存在記憶體中。
(3)API(Query) 負責查詢Storage中儲存的資料,提供簡單的JSON API獲取資料,主要提供給web UI使用

(4)Web 提供簡單的web介面

服務追蹤流程如下:

┌─────────────┐ ┌───────────────────────┐  ┌─────────────┐  ┌──────────────────┐
│ User Code   │ │ Trace Instrumentation │  │ Http Client │  │ Zipkin Collector │
└─────────────┘ └───────────────────────┘  └─────────────┘  └──────────────────┘
       │                 │                         │                 │
           ┌─────────┐
       │ ──┤GET /foo ├─▶ │ ────┐                   │                 │
           └─────────┘         │ record tags
       │                 │ ◀───┘                   │                 │
                           ────┐
       │                 │     │ add trace headers │                 │
                           ◀───┘
       │                 │ ────┐                   │                 │
                               │ record timestamp
       │                 │ ◀───┘                   │                 │
                             ┌─────────────────┐
       │                 │ ──┤GET /foo         ├─▶ │                 │
                             │X-B3-TraceId: aa │     ────┐
       │                 │   │X-B3-SpanId: 6b  │   │     │           │
                             └─────────────────┘         │ invoke
       │                 │                         │     │ request   │
                                                         │
       │                 │                         │     │           │
                                 ┌────────┐          ◀───┘
       │                 │ ◀─────┤200 OK  ├─────── │                 │
                           ────┐ └────────┘
       │                 │     │ record duration   │                 │
            ┌────────┐     ◀───┘
       │ ◀──┤200 OK  ├── │                         │                 │
            └────────┘       ┌────────────────────────────────┐
       │                 │ ──┤ asynchronously report span     ├────▶ │
                             │                                │
                             │{                               │
                             │  "traceId": "aa",              │
                             │  "id": "6b",                   │
                             │  "name": "get",                │
                             │  "timestamp": 1483945573944000,│
                             │  "duration": 386000,           │
                             │  "annotations": [              │
                             │--snip--                        │
                             └────────────────────────────────┘

Instrumented client和server是分別使用了ZipKin Client的服務,Zipkin Client會根據配置將追蹤資料傳送到Zipkin  Server中進行資料儲存、分析和展示。

ZipKin幾個概念

    在追蹤日誌中,有幾個基本概念spanId、traceId、parentId

    traceId:用來確定一個追蹤鏈的16字元長度的字串,在某個追蹤鏈中保持不變。

    spanId:區域Id,在一個追蹤鏈中spanId可能存在多個,每個spanId用於表明在某個服務中的身份,也是16字元長度的字串。

    parentId:在跨服務呼叫者的spanId會傳遞給被呼叫者,被呼叫者會將呼叫者的spanId作為自己的parentId,然後自己再生成spanId。

    如下圖:

    剛發起呼叫時traceId和spanId是一致,parentId不存在。


    被呼叫者的traceId和呼叫者的traceId時一致的,被呼叫者會產生自己的spanId,並且被呼叫者的parentId是呼叫者的spanId


安裝

1、下載:

    下載地址:https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec

2、執行:java -jar  zipkin-server-2.8.3-exec.jar

    

3、訪問:zipkin Server 執行後預設的訪問地址:http://localhost:9411


4、呼叫鏈分析

    啟動4個服務,呼叫關係如下:brave-webmvc-example服務呼叫brave-webmvc-example2,brave-webmvc-example2分別呼叫brave-webmvc-example3和brave-webmvc-example4(程式碼地址),鏈路關係如下圖:


右上角JSON節目可以看到4個服務的呼叫資料如下圖:

[
  {
    "traceId": "a4aa11d855699355",
    "id": "a4aa11d855699355",
    "name": "get /start",
    "timestamp": 1526110753393795,
    "duration": 3873359,
    "annotations": [
      {
        "timestamp": 1526110753393795,
        "value": "sr",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110757267154,
        "value": "ss",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      }
    ],
    "binaryAnnotations": [
      {
        "key": "ca",
        "value": true,
        "endpoint": {
          "serviceName": "",
          "ipv6": "::1",
          "port": 64570
        }
      },
      {
        "key": "http.method",
        "value": "GET",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.path",
        "value": "/start",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "mvc.controller.class",
        "value": "HomeController",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "mvc.controller.method",
        "value": "start",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      }
    ]
  },
  {
    "traceId": "a4aa11d855699355",
    "id": "cf49951d471ac7c5",
    "name": "get /foo",
    "parentId": "a4aa11d855699355",
    "timestamp": 1526110753583404,
    "duration": 3650640,
    "annotations": [
      {
        "timestamp": 1526110753583404,
        "value": "cs",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110754327066,
        "value": "sr",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110757234044,
        "value": "cr",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110757235819,
        "value": "ss",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      }
    ],
    "binaryAnnotations": [
      {
        "key": "ca",
        "value": true,
        "endpoint": {
          "serviceName": "",
          "ipv4": "127.0.0.1",
          "port": 64578
        }
      },
      {
        "key": "http.method",
        "value": "GET",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.method",
        "value": "GET",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.path",
        "value": "/foo",
        "endpoint": {
          "serviceName": "brave-webmvc-example",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.path",
        "value": "/foo",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "mvc.controller.class",
        "value": "HomeController",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "mvc.controller.method",
        "value": "foo",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      }
    ]
  },
  {
    "traceId": "a4aa11d855699355",
    "id": "c2c029d693ecc49b",
    "name": "get /bar",
    "parentId": "cf49951d471ac7c5",
    "timestamp": 1526110754397322,
    "duration": 1583187,
    "annotations": [
      {
        "timestamp": 1526110754397322,
        "value": "cs",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110755367168,
        "value": "sr",
        "endpoint": {
          "serviceName": "brave-webmvc-example3",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110755810759,
        "value": "ss",
        "endpoint": {
          "serviceName": "brave-webmvc-example3",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110755980509,
        "value": "cr",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      }
    ],
    "binaryAnnotations": [
      {
        "key": "ca",
        "value": true,
        "endpoint": {
          "serviceName": "",
          "ipv4": "127.0.0.1",
          "port": 64583
        }
      },
      {
        "key": "http.method",
        "value": "GET",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.method",
        "value": "GET",
        "endpoint": {
          "serviceName": "brave-webmvc-example3",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.path",
        "value": "/bar",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.path",
        "value": "/bar",
        "endpoint": {
          "serviceName": "brave-webmvc-example3",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "mvc.controller.class",
        "value": "HomeController",
        "endpoint": {
          "serviceName": "brave-webmvc-example3",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "mvc.controller.method",
        "value": "bar",
        "endpoint": {
          "serviceName": "brave-webmvc-example3",
          "ipv4": "192.168.1.101"
        }
      }
    ]
  },
  {
    "traceId": "a4aa11d855699355",
    "id": "e3968cec8747ce95",
    "name": "get /tar",
    "parentId": "cf49951d471ac7c5",
    "timestamp": 1526110756017988,
    "duration": 1194871,
    "annotations": [
      {
        "timestamp": 1526110756017988,
        "value": "cs",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110757081683,
        "value": "sr",
        "endpoint": {
          "serviceName": "brave-webmvc-example4",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110757212859,
        "value": "cr",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "timestamp": 1526110757222145,
        "value": "ss",
        "endpoint": {
          "serviceName": "brave-webmvc-example4",
          "ipv4": "192.168.1.101"
        }
      }
    ],
    "binaryAnnotations": [
      {
        "key": "ca",
        "value": true,
        "endpoint": {
          "serviceName": "",
          "ipv4": "127.0.0.1",
          "port": 64584
        }
      },
      {
        "key": "http.method",
        "value": "GET",
        "endpoint": {
          "serviceName": "brave-webmvc-example4",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.method",
        "value": "GET",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.path",
        "value": "/tar",
        "endpoint": {
          "serviceName": "brave-webmvc-example4",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "http.path",
        "value": "/tar",
        "endpoint": {
          "serviceName": "brave-webmvc-example2",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "mvc.controller.class",
        "value": "HomeController",
        "endpoint": {
          "serviceName": "brave-webmvc-example4",
          "ipv4": "192.168.1.101"
        }
      },
      {
        "key": "mvc.controller.method",
        "value": "tar",
        "endpoint": {
          "serviceName": "brave-webmvc-example4",
          "ipv4": "192.168.1.101"
        }
      }
    ]
  }
]

5、zipkin client採集

    目前官方提供瞭如下客戶端外掛進行追蹤資料的採集(https://github.com/openzipkin/brave/tree/master/instrumentation)


相關文章