SpringBoot集成zipkin实现链路追踪
2021-12-08 本文已影响0人
迦叶_金色的人生_荣耀而又辉煌
上一篇 <<<链路追踪的实现原理
下一篇 >>>Zipkin集成RabbitMQ异步传输,并持久化到数据库中
版本要求
SpringBoot、OpenFeign和Zipkin都要求2.1.*
核心代码
- jar包引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.jgspx</groupId>
<artifactId>openfeign-msg-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.jgspx</groupId>
<artifactId>openfeign-user-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
</dependencies>
- 相关配置
spring:
cloud:
nacos:
discovery:
server-addr: 10.211.55.16:8848
#feign客户端的命名必须使用中划线,不能使用下划线
application:
name: zipkin-user-service
zipkin:
base-url: http://127.0.0.1:9411/
###全部采集
# 抽样率,默认是0.1(90%的数据会被丢弃)
# 这边为了测试方便,将其设置为1.0,即所有的数据都会上报给zipkin
sleuth:
sampler:
probability: 1.0
效果展示
a、成功链路信息:
- 项目中获取链路信息:
request.getHeader("X-B3-TraceId")+",spanid:"+ request.getHeader("X-B3-SpanId")
-
每一个都是服务端,如果调用其他的话会有客户端的链路
b、失败链路信息:
- 错误信息里可以看到具体的错误
"tags": {
"error": "Request processing failed; nested exception is java.lang.ArithmeticException: / by zero",
"http.method": "GET",
"http.path": "/sendmsg",
"mvc.controller.class": "MsgServiceImpl",
"mvc.controller.method": "sendMsg"
},
推荐阅读:
<<<分布式链路监控与追踪产生背景
<<<常用的调用链解决方案
<<<链路追踪的实现原理
<<<Zipkin集成RabbitMQ异步传输,并持久化到数据库中
<<<Zipkin环境搭建