• SpringCloud Sleuth分布式请求链路跟踪


    SpringCloud Sleuth分布式请求链路跟踪

    1、概念

    Git官网地址:https://github.com/spring-cloud/spring-cloud-sleuth

    官网地址:https://spring.io/projects/spring-cloud-sleuth

    微服务框架中,一个由客户端发起的请求在后端系统中会经过多个不同的的服务节点调用来协同产生最后的请求结果,每一个前段请求都会形成一条复杂的分布式服务调用链路,链路中的任何一环出现高延时或错误都会引起整个请求最后的失败。

    Spring Cloud Sleuth提供了一套完整的服务跟踪的解决方案,在分布式系统中提供追踪解决方案并且兼容支持了zipkin

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M5gkdnLu-1667339413115)(image/93、追踪链路.png)]

    2、搭建链路监控步骤

    2.1、zipkin

    官方地址:https://zipkin.io/

    下载地址:https://gitcode.net/mirrors/openzipkin/zipkin?utm_source=csdn_github_accelerator

    在这里插入图片描述

    通过命令启动

    需要定位到当前目录下

    java -jar zipkin-server-2.23.18-exec.jar
    
    • 1

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hK5FdwwK-1667339413116)(image/95、启动zipkin.png)]

    浏览器访问:http://127.0.0.1:9411/

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jnLJKbUT-1667339413117)(image/96、zipkin首页.png)]

    监控流程

    一条链路通过Trace Id唯一标识,Span标识发起的请求信息,各span通过parent id 关联起来

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K2u6GhUO-1667339413117)(image/97、zipkin执行流程.png)]

    整个链路的依赖关系:服务一 调用 服务二 ,服务二又分别调用服务三或四

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YZBMoFxA-1667339413118)(image/98、链路依赖.png)]

    2.2、服务提供者

    2.2.1、修改cloud-provider-payment8001服务提供者模块

    1、引入pom.xml依赖
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-zipkinartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2、添加YAMLA配置文件
    spring:
      application:
        name: cloud-payment-service # 指定程序名称
      zipkin: # 链路跟踪
        base-url: http://localhost:9411 # 跟踪后展示的地址
        sleuth:
          sampler:
            #采样率值介于 0 到 1 之间,1 则表示全部采集,一般采用0.5
            probability: 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    3、业务类实现

    在8001PaymentController控制器上面添加有个简单的接口,方便链路进行跟踪

    /**
     * 请求链路跟踪测试
     * @return
     */
    @GetMapping("/zipkin")
    public String paymentZipkin()
    {
        return "hi ,i'am paymentzipkin server fall back,welcome to atguigu,O(∩_∩)O哈哈~";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    4、启动项目
    1. 启动7001
    2. 启动8001

    2.3、服务消费者(调用方)

    2.3.1、修改cloud-consumer-order80消费者模块

    2.3.2、引入依赖

    与提供者一样

    2.3.3、添加YAML配置文件

    spring:
      application:
        name: cloud-order-service # 程序名称
      zipkin: # 链路跟踪
        base-url: http://localhost:9411 # 监控展示地址
        sleuth:
          sampler:
            probability: 1 # 采样率
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.3.4、添加控制器

    // ====================> zipkin+sleuth
    
    /**
     * 调用链路跟踪服务接口
     * @return
     */
    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.3.5、启动测试

    1. 之前启动了7001、8001

    2. 启动当前项目80

    3. 页面访问:http://localhost/consumer/payment/zipkin

      多点击几次产生链路,然后查看链路

    4. 查看链路跟踪

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-axv4FGVv-1667339413118)(image/99、查看链路.png)]

    5. 链路详情

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bcduV7Qv-1667339413118)(image/100、链路详情.png)]

  • 相关阅读:
    17.在springboot中集成redis的实例(RedisTemplate,StringRedisTemplate)
    DFT specification file & string
    Express 5 快速入门 - 静态文件
    HTML+CSS+JavaScript仿京东购物商城网站 web前端制作服装购物商城 html电商购物网站
    【Swift 60秒】12 - Arrays vs Sets vs Tuples
    TinyWebServer学习笔记-threadpool
    《Autosar_MCAL高阶配置》总目录_培训教程持续更新中...
    LogBack的日志报错解决办法 org.xml.sax.SAXNotRecognizedException
    2022.11.16 英语背诵
    组件-ulog
  • 原文地址:https://blog.csdn.net/baidu_39378193/article/details/127644692