原文链接:https://www.jianshu.com/p/f177a5e2917f
Zipkin 是一个开放源代码分布式的跟踪系统,每个服务向zipkin报告计时数据,zipkin会根据调用关系通过Zipkin UI生成依赖关系图。
Zipkin提供了可插拔数据存储方式:In-Memory、MySql、Cassandra以及Elasticsearch。为了方便在开发环境我直接采用了In-Memory方式进行存储,生产数据量大的情况则推荐使用Elasticsearch。
在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
默认启动方式会将日志数据存在内存中,一旦服务重启会清空数据,建议使用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
基于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%)。
原文:https://www.cnblogs.com/fswhq/p/13785944.html