美文网首页Spring Cloud
spring cloud 2.x版本 Sleuth+Zipkin

spring cloud 2.x版本 Sleuth+Zipkin

作者: maomaov5 | 来源:发表于2019-11-12 18:10 被阅读0次

    前言

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3
    

    本文基于前两篇文章eureka-server、eureka-client、eureka-ribbon和spring-gateway的实现。
    参考

    概述

    前一篇文章讲述了Sleuth链路追踪的http的日志收集的搭建过程,这篇文章补充一下rabbitmq日志收集机制的搭建过程。
    http方式存在的问题,如果应用服务与zipkin服务端网络不通或者闪断的情况下,这种情况是无法正常收集的,而且zipkin默认是将数据存储在内存当中的,如果服务端重启或者宕机,数据就会丢失。
    rabbitmq的方式完美的解决了这种问题(用消息中间件的好处我这里就不过多阐述)。

    1. 对原有应用进行改造

    1.1 增加pom依赖

    在eureka-client、eureka-ribbon、spring-gateway对应的pom.xml增加如下依赖:

    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
    </dependency>
    

    说明:
    最新官网已经不推荐使用spring-cloud-sleuth-stream,而推荐使用spring-rabbit

    • 引用官网原文:spring-cloud-sleuth-stream is deprecated and incompatible with these destinations.

    1.2 修改application.yml文件

    修改eureka-client、eureka-ribbon、spring-gateway应用对应的application.yml配置文件,参考如下配置:

    spring:
      sleuth:
        sampler:
          probability: 1
        web:
          enabled: true
      zipkin:
        sender:
          type: rabbit
      rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
    

    要删除http方式的zipkin.base-url: http://localhost:9411/ 配置。

    1.3 启动服务

    启动rabbitmq和zipkin服务,这里需要说明的是,启动zipkin服务输入如下命令启动:

      1. $ java -jar zipkin.jar --zipkin.collector.rabbitmq.addresses=localhost
        默认guest账户。
      1. $ java -jar zipkin.jar --zipkin.collector.rabbitmq.addresses=localhost --zipkin.collector.rabbitmq.username=xxx --zipkin.collector.rabbitmq.password=xxx,启动自定义用户名和密码。

    然后顺序启动eureka-client、eureka-ribbon、spring-gateway应用服务。
    访问http://localhost:8100/ribbon/sayHello, 可以多刷新几次,然后我们可以访问http://localhost:9411/zipkin,点击查询可以到和http方式相同的结果,如下图所示:

    这时候我们访问rabbitmq的后台管理地址:http://localhost:15672, 可以在管理页面中的Queues中看到如下显示:

    ,说明zipkin服务帮我创建了一个名称为zipkin的Queues。而Exchanges则默认使用的是AMQP default,如下图所示:

    至此,Sleuth+ZipKin的rabbitmq的方式收集就搭建完成。

    1.4 小结

    Spring cloud Sleuth分布式链路追踪不仅支持rabbitmq,还支持kafaka,实现过程和rabbitmq完全相同,只有修改对应的依赖包和配置就可以了,这里就不在演示。

    代码地址

    gitHub地址


    <center><font color=red>《Srping Cloud 2.X小白教程》目录</font></center>


    • 写作不易,转载请注明出处,喜欢的小伙伴可以关注公众号查看更多喜欢的文章。
    • 联系方式:4272231@163.com

    相关文章

      网友评论

        本文标题:spring cloud 2.x版本 Sleuth+Zipkin

        本文链接:https://www.haomeiwen.com/subject/kibqictx.html