2Zipkin 使用 · SpringCloud微服务实战 · 看云
导航
本节代码地址
GitHub:https://github.com/xuyisu/fw-sping-cloud/tree/master/fw-cloud-register/fw-register-eureka
GitHub:https://github.com/xuyisu/fw-sping-cloud/tree/master/fw-cloud-ribbon/fw-cloud-ribbon-server
GitHub:https://github.com/xuyisu/fw-sping-cloud/tree/master/fw-cloud-gateways/fw-cloud-gateways-gateway-simple
Zipkin 在Spring Cloud 的老版本我们可以根据需要自己搭建Zipkin Server,在新版的Spring Cloud 已经不推荐自己搭建了,我可以使用官方搭建好的Zipkin Server,地址为:https://github.com/openzipkin/zipkin
Zipkin 默认使用内存存储数据,也支持Mysql、elasticsearch等。
1. Zipkin 安装
本节我们使用Zipkin、Mysql结合RabbitMq的方式来演示(RabbitMq 的安装可以参照消息总线章节)
最新的Zipkin Server包已经上传到群里
1.1 Sql 脚本
SQL脚本地址为:https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql-v1/src/main/resources/mysql.sql
新建数据库fwcloud_zipkin并执行上面的sql脚本
1.2 启动 Zipkin
java -jar zipkin-server-2.20.0-exec.jar --zipkin.storage.type=mysql --zipkin.storage.mysql.db=fwcloud_zipkin --zipkin.storage.mysql.username=root --zipkin.storage.mysql.password=123456 --zipkin.storage.mysql.host=localhost --zipkin.storage.mysql.port=3306 --zipkin.collector.rabbitmq.addresses=localhost:5672 --zipkin.collector.rabbitmq.username=fwcloud --zipkin.collector.rabbitmq.password=fwcloud
浏览器输入http://localhost:9411/,就可以看到暂时什么都没有
1.3 改造Spring Cloud
下面我们对以下3个系统进行改造演示
- fw-register-eureka
- fw-cloud-ribbon-server
- fw-gateways-gateway
maven 添加Zipkin和RabbitMq的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
在应用配置application.yml中添加配置
spring:
#zipkin 链路监控时打开
zipkin:
base-url: http://localhost:9411/
service:
name: ${spring.application.name}
#使用默认 http 方式收集 span 需要配置此项
#sender.type=web
#sleuth 使用 rabbitmq 来向 zipkin 发送数据
sender:
type: rabbit
rabbitmq:
host: localhost
port: 5672
username: fwcloud
password: fwcloud
# #设置采样率默认为 0.1 注意之前的版本是percentage 新版本中更换为 probability
sleuth:
sampler:
probability: 1
项目启动完之后,我们在Postman 中输入localhost:8699/user/2可以看到具体的链路调用
可以看到RabbitMQ 消息的发送
数据库的纪录
刷新http://localhost:9411/可以看到一下链路内容
详细内容
链路内容如下
可以看到Zipkin 里的链路并没有包含Eureka,仅仅这一点就没有Skywalking 体验好。
2. 总结
本节主要介绍了Zinkin 的使用,从入门介绍到Zipkin 的安装使用,再到通过整合Spring Cloud 服务展示了实际运行的效果,相信读者已经掌握了这一监控工具。








