spring boot +spring cloud + spring cloud alibaba入门搭建(六)整合sleuth+zipkin,链路追踪

本文介绍了如何在Spring Boot和Spring Cloud项目中整合Sleuth与Zipkin,实现链路追踪。通过在cloud-user和cloud-product模块引入相关依赖,并配置相关设置,成功启动Zipkin Server后,可以清晰地查看到请求的调用链路,有助于理解与排查问题。
摘要由CSDN通过智能技术生成

1.上一编我们已经学习gateway网关统一入口了,这编我们整合sleuth+zipkin,链路追踪,方便我们快速知道一个请求的所有调用链。

2.在我们的cloud-user和cloud-product 的pom.xml 引入

<!--链路追踪 Sleuth-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

这两个的依赖包

两个配置文件都增加一下的配置

zipkin:
  base-url: http://localhost:9411
  discovery-client-enabled: false
sleuth:
  sampler:
    percentage: 1.0

最后结果类似:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yaml
    #sentinel:
      #transport:
        #dashboard: localhost:8080
        #port: 8719
  application:
    name: user
  zipkin:
    base-url: http://localhost:9411
    discovery-client-enabled: false
  sleuth:
    sampler:
      percentage: 1.0

feign:
  hystrix:
    enabled: true

###设置feign客户端超时时间
###SpringCloud feign 默认开启支持ribbon
ribbon:
  ###指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。
  ReadTimeout: 5000
  ###指的是建立连接后从服务器读取到可用资源所用的时间。
  ConnectTimeout: 5000
  ###当前实例的重试次数
  MaxAutoRetries: 2
  ###切换实例的最大次数
  MaxAutoRetriesNextServer: 1

hystrix:
  command:
    default:
      execution:
        timeout:
          enable: true
        isolation:
          strategy: SEMAPHORE
          thread:
            ###断路器的超时时间一定要配置,不然只要sleep 1秒就都超时了,
            ###而且要比ribbon 的重试次数*时间 大
            timeoutInMilliseconds: 20000

logging:
  level:
    root: INFO
    org.springframework.web.servlet.DispatcherServlet: DEBUG
    org.springframework.cloud.sleuth: DEBUG

引入了依赖以后,

下载zipkin的server端,启动

java -jar zipkin-server-2.12.9-exec.jar
启动我们的服务,调用接口,
可以看到product控制台打印了,
2021-08-27 18:09:38.197 DEBUG [product,a22030d75da440c0,38bcd57bf7700eaa,true] 21780 --- [nio-8989-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/product/getInfo", parameters={},
user控制台打印了,
2021-08-27 18:09:40.239 DEBUG [user,a22030d75da440c0,a22030d75da440c0,true] 29816 --- [nio-8990-exec-8] o.s.web.servlet.DispatcherServlet        : Completed 200 OK
 
可以看到他们的traceId 是一样的,spanId是不一样的,
上面的结果大概是
【微服务名称 , traceId, spanid, 是否将链路的追踪结果输出到第三方平台】
再去看看我们的 http://localhost:9411/
能看到我们的请求的链路都在这里,
成功的

失败的

 

一目了然。 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值