概述
sleuth可以将调用日志(Trace +Span)发给Zipkin进行存储、分析。
Zipkin可以将收集到的分布式日志,整合成服务调用链,方便分析及查找问题。
实战
Zipkin
-
下载Zipkin
从官网(https://zipkin.io)下载 Zipkin (有docker 及 jar 两种方式),本示例采用下载 jar 包的方式
-
启动Zipkin
java -jar zipkin-server-2.12.9-exec.jar

-
WEB管理页面
[WEB管理页面URL] http://localhost:9411/

目前还没有日志数据,需要配置sleuth收集追踪日志,并将日志发送给Zipkin。
Zipkin默认使用内存存储数据,重启服务后,数据会丢失。可以配置为持久化的存储方案。
sleuth
-
添加依赖(pom.xml)
<!-- sleuth -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<!-- zipkin client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
-
配置(application.properties)
# sleuth只能收集DEBUG级别日志(这里定义了收集日志的范围)
logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG
# 收集日志的比例,取值范围为0-1(缺省为0.1,即只收集10%的日志): 为了方便验证效果设为1
#spring.sleuth.sampler.rate=1
# zipkin服务器URL (sleuth会将收集到的日志报告给zipkin)
spring.zipkin.baseUrl=http://localhost:9411
查看日志
-
重启服务,访问consumer应用
重新启动相关的服务,使用浏览器访问consumer应用,

-
查看consumer应用的日志

-
通过Zipkin WEB管理页面查看日志分析结果

点击其中一个调用链,可查看详细情况,如下图

调用模拟异常
将provider项目手工停止,再次访问测试服务后,进入Zipkin的WEB管理页面,可查看到调用的过程及状态。

网友评论