6

SpringCloud 2020版本教程4:使用spring cloud sleuth+zipkin实现链路追踪

 3 years ago
source link: https://www.fangzhipeng.com/springcloud/2021/04/05/sc-2020-sleuth.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spring Cloud Sleuth+zipkin实现链路追踪

Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可。

微服务架构上通过业务来划分服务的,通过REST调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败。随着业务的不断扩张,服务之间互相调用会越来越复杂。

2279594-dd72907e82f89fd6.png

本小节教程将讲述如何使用sleuth和zipkin来构建微服务的链路追踪。

下载zipkin server并启动

下载地址如下,并启动:

curl -sSL https://zipkin.io/quickstart.sh | bash -s
java -jar zipkin.jar

访问zipkin的ui 界面,地址为localhost:9411

在三个工程provider\consumer\gateway的pom文件加上以下的依赖:

         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>

三个工程的配置文件application.yml加上以下的配置:

spring.zipkin.sender.type: web
#设置采样率默认为 0.1 注意之前的版本是percentage 新版本中更换为 probability
spring.sleuth.sampler.probability: 1
spring.zipkin.base-url: http://localhost:9411/
spring.zipkin.service.name: consumer

zipkin server支持mq方式收集链路信息,同时支持多种数据存储方式,比如es\mysql等,更多收集数据和存储方式见:https://github.com/openzipkin/zipkin/tree/master/zipkin-server

启动三个工程,在浏览器上访问:http://localhost:5000/consumer/hi-feign

访问zipkin server的api ,可以看到服务依赖,如下:

sc2020-13.png

可以看到请求的具体耗时: sc2020-14.png

https://zipkin.io/pages/quickstart.html

https://www.fangzhipeng.com/springcloud/2018/08/09/sc-f9-sleuth.html

https://github.com/forezp/SpringCloudLearning/tree/master/sc-2020-chapter4

本文为原创文章,转载请标明出处。
本文链接:https://www.fangzhipeng.com/springcloud/2021/04/05/sc-2020-sleuth.html
本文出自方志朋的博客


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK