2Zipkin 使用 · SpringCloud微服务实战 · 看云

导航

本节代码地址


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脚本
1e39b5b5cef95e3838fbc3aea552525f_MD5.png

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/,就可以看到暂时什么都没有
194f2c1554b7e62fcda9692a992303e6_MD5.png

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

c4955e6deff95a953a9c57812c7c9fd9_MD5.png

项目启动完之后,我们在Postman 中输入localhost:8699/user/2可以看到具体的链路调用
d828f67615919f3e1e81856117972c75_MD5.png

可以看到RabbitMQ 消息的发送
55ba07dc5254b3eb5d5cf4e9ed397f91_MD5.png

数据库的纪录
218cca68b4c050fbcd58f6a957261086_MD5.png

刷新http://localhost:9411/可以看到一下链路内容
34e32154edd1373f927c78fcf06811ea_MD5.png

详细内容
587bf21c865a9071703291a7ec219ffe_MD5.png

链路内容如下
f1e959587a2913541b988144cb35e0db_MD5.png

可以看到Zipkin 里的链路并没有包含Eureka,仅仅这一点就没有Skywalking 体验好。

2. 总结

本节主要介绍了Zinkin 的使用,从入门介绍到Zipkin 的安装使用,再到通过整合Spring Cloud 服务展示了实际运行的效果,相信读者已经掌握了这一监控工具。