环境:

jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3

摘要说明:

Zipkin:Zipkin <https://zipkin.io/>
是一个分布式追踪系统。它有助于收集解决微服务架构中的延迟问题所需的时序数据。它管理这些数据的收集和查找。Zipkin的设计基于 Google Dapper
<http://research.google.com/pubs/pub36356.html>实现。应用程序用于向Zipkin报告时序数据。
Zipkin用户界面还提供了一个依赖关系图,显示每个应用程序有多少跟踪请求。
如果要解决延迟问题或错误,可以根据应用程序,跟踪长度,注释或时间戳对所有跟踪进行筛选或排序。
选择跟踪后,您可以看到每个跨度所需的总跟踪时间百分比,从而可以识别问题应用程序。

更多内容请查阅官网 <https://zipkin.io/>;

步骤:

1.创建zipkinServer服务

创建一个新的zipkinServer子项目,搭建一个zipkinServer服务器;
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <parent> <groupId>pers.cc</groupId>
<artifactId>springCloud</artifactId> <version>0.0.1-SNAPSHOT</version>
</parent> <artifactId>zipkinServer</artifactId> <name>全链路追踪监控</name>
<description>zipkinServer</description> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency>
<groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId>
<version>2.9.4</version> </dependency> <dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.9.4</version>
</dependency> </dependencies> </project>
2.配置zipkinServer服务

使用@EnableZipkinServer注解开启zipkin配置:
import org.springframework.boot.SpringApplication; import
org.springframework.boot.autoconfigure.SpringBootApplication; import
zipkin.server.internal.EnableZipkinServer; @SpringBootApplication
@EnableZipkinServer public class ZipkinServerApplication { public static void
main(String[] args) { SpringApplication.run(ZipkinServerApplication.class,
args); } } public static void main(String[] args) {
SpringApplication.run(ZipkinServerApplication.class, args); } }
配置application.properties
#配置服务及端口(默认9411) spring.application.name=zipkin-server server.port=9411
#去除控制台异常 management.metrics.web.server.auto-time-requests=false
3.配置sleuthRibbon(sleuth服务消费者)和sleuthEurekaDiscovery(服务提供至)子项目

pom.xml添加spring-cloud-starter-zipkin
<!-- 添加sleuth全链路追踪监控依赖 --> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
配置application.properties
#指定zipkin中心 spring.zipkin.base-url=http://localhost:9411
4.实践

先后启动

 

zipkin中心(zipkinServer):zipkin-server(9411)

服务注册中心(eurekaServer):eureka-server(1001)

服务提供者(sleuthEurekaDiscovery):sleuth-eureka-client(2101)

服务消费者(sleuthRibbon):sleuth-ribbon-consumer(3101)

多次调用http://localhost:3101/infoLog-2;

再访问zipkin中心:http://localhost:9411/zipkin/;点击Find Traces;



点击蓝色条可进入详情:



5.抽样配置

在客户端配置spring.application.sleuth.sampler.probability:默认为0.1;表示调用10次才会上传一次;

 
#指定抽样比例 spring.sleuth.sampler.probability=0.5
可以观察日志true表示上传成功:
2018-07-02 17:08:09.142 INFO
[sleuth-ribbon-consumer,1bb9844c33135a5e,1bb9844c33135a5e,false] 7776 ---
[nio-3101-exec-5] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:09.370 INFO
[sleuth-ribbon-consumer,a668db0b14877584,a668db0b14877584,false] 7776 ---
[nio-3101-exec-7] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:09.638 INFO
[sleuth-ribbon-consumer,dd549e99c53cacaf,dd549e99c53cacaf,true] 7776 ---
[nio-3101-exec-9] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:26.656 INFO
[sleuth-ribbon-consumer,596669c8c06daae0,596669c8c06daae0,true] 7776 ---
[nio-3101-exec-2] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:26.869 INFO
[sleuth-ribbon-consumer,27e8fdf8cfdbb8d6,27e8fdf8cfdbb8d6,true] 7776 ---
[nio-3101-exec-4] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:27.097 INFO
[sleuth-ribbon-consumer,6b171dbc2cabde6e,6b171dbc2cabde6e,false] 7776 ---
[nio-3101-exec-6] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:27.433 INFO
[sleuth-ribbon-consumer,3dd4b2d43d3dbe3b,3dd4b2d43d3dbe3b,true] 7776 ---
[nio-3101-exec-8] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:28.079 INFO
[sleuth-ribbon-consumer,768078a9de38f453,768078a9de38f453,true] 7776 ---
[io-3101-exec-10] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:28.436 INFO
[sleuth-ribbon-consumer,ba9111b2d767a737,ba9111b2d767a737,true] 7776 ---
[nio-3101-exec-2] c.t.s.s.controller.TestLogController : ------infoLog-2------
2018-07-02 17:08:28.934 INFO
[sleuth-ribbon-consumer,fb2ef82867df9c71,fb2ef82867df9c71,false] 7776 ---
[nio-3101-exec-4] c.t.s.s.controller.TestLogController : ------infoLog-2------
6.源码地址

github地址:https://github.com/cc6688211/springCloud.git
<https://github.com/cc6688211/springCloud.git>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信