SpringCloud-链路跟踪

SpringCloud文章系列

  1. SpringCloud
  2. SpringCloud-注册中心
  3. SpringCloud-配置中心
  4. 【当前文章】SpringCloud-链路跟踪
  5. SpringCloud-消息总线
  6. SpringCloud-API网关
  7. SpringCloud-异步消息
  8. SpringCloud-同步调用
  9. SpringCloud-断路降级
  10. SpringCloud-监控管理
  11. SpringCloud-番外篇-临时任务
  12. SpringCloud-番外篇-文档生成
  13. SpringCloud-番外篇-源码解析

接入说明

  1. 目前采用最新的1.xRelease版本:1.5.15.RELEASE
  2. SpringCloud项目可以在start.spring.io下载,不过更方便的应该是通过idea新建项目,建立Spring Initializr项目
  3. 我一般习惯将具体实现服务用具体的服务名,而公共组件用service-xxx来命名,当然还有一些比较固定名字的公共组件
  4. 基于配置中心,请先按照配置中心章节,配置公共配置

Zipkin

1. 新建项目

新建artifactId为zipkin-service的服务

2. 导入依赖

依赖说明

  1. actuator用于暴露监控的接口
  2. eureka是Eureka客户端,注册到注册中心
  3. config是Config客户端,从配置中心拉取配置
  4. zipkin-server + zipkin-autoconfigure-ui是链路追踪客户端,用于监控链路调用
    1. 注意这个依赖属于zipkin的ui,并不是springcloud包下,需要单独引用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<scope>runtime</scope>
</dependency>

3. 编写启动类

打开ZipkinServiceApplication,在class上加入@EnableZipkinServer注解

4. 修改配置

1
2
3
4
5
6
7
8
server.port=9411
spring.application.name=zipkin-service
# public
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=service-config
zipkin.storage.type=mem

zipkin.storage.type默认也是用mem,默认数据存储在内存中,可以用mysql之类的存储去存储收集的信息

5. 启动main

6. 验证

访问 http://localhost:8761/ 看到zipkin-service已经注册了一台
访问 http://localhost:9411/zipkin 看到服务端的UI界面,上面有查找调用链、依赖分析等功能

客户端使用

1. 添加依赖

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

2. 开启注解

无须添加注解

3. 修改配置

1
2
3
4
5
spring.sleuth.scheduled.enabled=false
spring.sleuth.async.enabled=false
spring.sleuth.annotation.enabled=false
spring.sleuth.sampler.percentage=1
spring.zipkin.sender.type=web

scheduled.enabled与async.enabled关闭定时与异步推送数据,开启实时同步,可以更实时的看到数据
sampler.percentage采集率,用于控制多少比例的数据进行采集,默认是0.1,请求10次采集1次,这里设置为1,每一次调用都会采集
zipkin.sender.type设置为web才会直接请求到zipkin-server,否则如果依赖binders会默认是放在kafka中的,所以明确使用web方式调用

扩展

除了通过web同步请求链路信息,还可以采用kafka异步采集信息

zipkin.collector可以采用kafka收集信息,而客户端也可以配置为kafka发送信息

配置略…

zipkin的数据可以采用mysql数据存储

配置略…

trace中的流转

一个是多出了trace信息,同时注意最后一个状态位
日志中[demo-trace1,63c082f6715c8979,63c082f6715c8979,true],true代表上传成功;

如果开启了kafka的日志,则kafka消息中会多出4个trace相关的key,详见异步消息章节

------ 本文结束 ------

版权声明

dawell's Notes by Dawell is licensed under a Creative Commons BY-NC-ND 4.0 International License.
Dawell创作并维护的dawell's Notes博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证
本文首发于dawell's Notes 博客( http://dawell.cc ),版权所有,侵权必究。

坚持原创技术分享,您的支持将鼓励我继续创作!