美文网首页
2019-01-10

2019-01-10

作者: WithMeStudy | 来源:发表于2019-01-10 14:39 被阅读0次

    ZipKin入门教程

    ZipKin介绍

    Zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper的论文设计而来,由 Twitter 公司开发贡献。其主要功能是聚集来自各个异构系统的实时监控数据。

    ZipKin架构

    ZipKin可以分为两部分,一部分是zipkin server,用来作为数据的采集存储、数据分析与展示;zipkin client是zipkin基于不同的语言及框架封装的一些列客户端工具,这些工具完成了追踪数据的生成与上报功能,架构如下:


    ZipKin架构

    Instrumented client和Instrumented server是分布式系统中的服务,通过装备库采集跟踪信息,装备库再调用Transport,把跟踪信息发送给Zipkin收集器(collectors),这些收集器将跟踪数据保存到存储(storage)中。
    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几个概念

    traceId:用来确定一个追踪链的16字符长度的字符串,在某个追踪链中保持不变。
    spanId:区域Id,在一个追踪链中spanId可能存在多个,每个spanId用于表明在某个服务中的身份,也是16字符长度的字符串。
    parentId:在跨服务调用者的spanId会传递给被调用者,被调用者会将调用者的spanId作为自己的parentId,然后自己再生成spanId。
    刚发起调用时traceId和spanId是一致,parentId不存在。被调用者的traceId和调用者的traceId是一致的,被调用者会产生自己的spanId,并且被调用者的parentId是调用者的spanId。
    Span模型几乎完全仿造了Dapper中Span模型的设计,Zipkin中的span主要包含三个数据部分:基础数据(包括traceId、spanId、parentId、name、timestamp和duration,主要用于跟踪树中节点的关联和界面展示)、 Annotation(用来记录请求特定事件相关信息)、BinaryAnnotation(提供一些额外信息,一般以key-value对出现)。
    装备库是实现采集的关键,针对不同语言,不同RPC框架,有不同的装备库实现,目前已有实现列表见zipkin的开源社区,其中Brave是zipkin官方提供的Java的装备库。一个装备库的实现需要考虑如下情况:
    • 实现语言和需要装备服务的语言一致
    • zipkin需要核心数据结构信息记录,包括tracerid,spanid的生成,延迟时间的计算,事件记录,tag记录等
    • 服务之间跟踪信息的传递称为植入,不同RPC接口植入的方式不一样,例如HTTP接口采用B3协议植入
    • 植入的信息包括:Trace Id、Span Id、Parent Id、Sampled、Flags
    • 可支持采样率设置,减少跟踪导致的系统负荷
    • 可调用Transport将跟踪信息传给zipkin
    基于Zipkin提供的装备库, Zipkin可实现调用延时分析与服务依赖关系分析两个基本功能。

    调用链分析例子

    启动4个服务,调用关系如下:brave-webmvc-example服务调用brave-webmvc-example2,brave-webmvc-example2分别调用brave-webmvc-example3和brave-webmvc-example4

    [
      {
        "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"
            }
          }
        ]
      }
    ]
    

    总结

    通过上面的分析,有如下总结:
    第一,在原理上,在每次业务调用发生时,在源头请求中产生一个全局唯一的TraceId,通过网络依次将TraceId在调用过程中透传,每一个调用环节都用将信息记录到Span日志中形成上下文记录,最后通过TraceId将散落在分布式系统上的“孤立”上下文记录联系在一起,重组还原出调用链过程,最后根据业务的需要对调用数据进行分析。

    第二,埋点与数据生成上,以上的实践均采用了基于标注的方案,该方式的缺点是需要代码植入。无处不在的埋点以为这无处不在的代码植入,如何做到对业务系统的透明化,是个巨大的挑战。对于这个问题,大多数的互联网公司得益于自身的高度统一的架构设计,代码的植入只需要通过在中间件植入便可,很好地解决了代码植入对业务部件不透明的问题。

    第三,日志的收集与存储涉及到数据及时性及系统性能需求等问题,根据业务需求,选择最优的存储的方案。

    第四,调用链跟踪数据规范定义与可视化,调用跟踪数据规范为数据结构上的规范定义,通过规范的定义保证生成的日志最终可以被调用跟踪系统进行分析和可视化。

    相关文章

      网友评论

          本文标题:2019-01-10

          本文链接:https://www.haomeiwen.com/subject/czharqtx.html