• 链路追踪Zipkin整合


    原文链接:https://www.jianshu.com/p/f177a5e2917f

    前言

    Zipkin 是一个开放源代码分布式的跟踪系统,每个服务向zipkin报告计时数据,zipkin会根据调用关系通过Zipkin UI生成依赖关系图。

    Zipkin提供了可插拔数据存储方式:In-Memory、MySql、Cassandra以及Elasticsearch。为了方便在开发环境我直接采用了In-Memory方式进行存储,生产数据量大的情况则推荐使用Elasticsearch。

    基本术语

    1. Span:基本工作单元,例如,在一个新建的span中发送一个RPC等同于发送一个回应请求给RPC,span通过一个64位ID唯一标识,trace以另一个64位ID表示,span还有其他数据信息,比如摘要、时间戳事件、关键值注释(tags)、span的ID、以及进度ID(通常是IP地址)
      span在不断的启动和停止,同时记录了时间信息,当你创建了一个span,你必须在未来的某个时刻停止它。
    2. Trace:一系列spans组成的一个树状结构,例如,如果你正在跑一个分布式大数据工程,你可能需要创建一个trace。
    3. 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.png

    构建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生成依赖链

    zipkin-dependencies基于spark job来生成全局的调用链,下载地址: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,这里要注意的是程序只会根据当日的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时代表全部强求都需要采样。Sleuth默认采样算法的实现是Reservoir sampling,具体的实现类是PercentageBasedSampler,默认的采样比例为: 0.1(即10%)。

  • 相关阅读:
    小米面试之股票收益最大
    第三周:同时管理64位和32位版本的Python,并用Pyinstaller打包成exe
    第三周 anaconda的安装
    第二周:python实现线性回归(哑变量回归)的高效方法
    第一周:在python里调用C文件中函数
    13G:神奇的数列
    13F:图像分割
    13E:吃奶酪
    13D:拖延症
    13B:回文子串
  • 原文地址:https://www.cnblogs.com/fswhq/p/13785944.html
Copyright © 2020-2023  润新知