zoukankan      html  css  js  c++  java
  • 16-SpringCloud Sleuth

    Sleuth概述

    为什么会出现这个技术?要解决哪些问题?

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

    是什么

    • https://github.com/spring-cloud/spring-cloud-sleuth
    • Spring Cloud Sleuth提供了一套完整的服务跟踪的解决方案
    • 在分布式系统中提供追踪解决方案并且兼容支持了zipkin

    解决

    Sleuth之zipkin搭建安装

    zipkin

    下载

    • zipkin-server-2.14.1-exec.jar

    运行jar

    java -jar zipkin-server-2.14.1-exec.jar

    运行控制台

    http://localhost:9411/zipkin/

    不知道为什么这个Logo出不来~

    术语

    完整的调用链路

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

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

    整个链路的依赖关系如下:

    名词解释

    • Trace:类似于树结构的Span集合,表示一条调用链路,存在唯一标识
    • span:表示调用链路来源,通俗的理解span就是一次请求信息

    Sleuth链路监控展现

    修改8001服务提供者

    cloud-provider-payment8001

    修改POM.xml

    <!--包含了sleuth+zipkin-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>

    修改yml配置

    server:
      port: 8001
    
    spring:
      application:
        name: cloud-payment-service
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
        driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
        url: jdbc:mysql://localhost:3306/my?useUnicode=true&characterEncoding=utf-8&useSSL=false
        username: root
        password: 123456
      zipkin: #<-------------------------------------关键
        base-url: http://localhost:9411
      sleuth: #<-------------------------------------关键
        sampler:
          #采样率值介于 0 到 1 之间,1 则表示全部采集
          probability: 1
    
    eureka:
      client:
        #表示是否将自己注册进Eurekaserver默认为true。
        register-with-eureka: true
        #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
        fetchRegistry: true
        service-url:
    #      defaultZone: http://localhost:7001/eureka
            #将单一地址修改为多地址通过逗号分割
          defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
      instance:
        instance-id: cloud-payment-service8001
        prefer-ip-address: true #添加此处
        #心跳检测与续约时间
        #开发时没置小些,保证服务关闭后注册中心能即使剔除服务
        #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
        lease-renewal-interval-in-seconds: 1
        #Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
        lease-expiration-duration-in-seconds: 2
    mybatis:
      mapperLocations: classpath:mapper/*.xml
      type-aliases-package: com.dance.com.dance.springcloud.entities    # 所有Entity别名类所在包

    修改Controller

    @RestController
    @Slf4j
    public class PaymentController {
        
        .........
    
        @GetMapping("/payment/zipkin")
        public String paymentZipkin() {
            return "hi ,i'am paymentzipkin server fall back,welcome to here, O(∩_∩)O哈哈~";
        }
    }

    修改80服务消费者

    cloue-consumer-order80

    修改POM.xml

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

    修改yml配置

    server:
      port: 80
    
    spring:
      application:
        name: cloud-order-service
      zipkin:
        base-url: http://localhost:9411
      sleuth:
        sampler:
          probability: 1
    
    eureka:
      client:
        #表示是否将自己注册进Eurekaserver默认为true。
        register-with-eureka: true
        #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
        fetchRegistry: true
        service-url:
    #      defaultZone: http://localhost:7001/eureka
          #将单一地址修改为多地址通过逗号分割
          defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

    修改OrderController

    // ====================> zipkin+sleuth
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin(){
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }

    测试

    • 启动Eureka集群
    • 启动8001
    • 启动80
    • 访问80指定接口
    • 启动Zipkin控制台查看

    ok~

    作者:彼岸舞

    时间:202196

    内容关于:Spring Cloud H版

    本文属于作者原创,未经允许,禁止转发

  • 相关阅读:
    对java中接口的简单理解
    jqgrid
    sed跨行匹配替换
    linux 安装 mysql
    mysql 导入或导出(mysqldump)数据
    spring boot slf4j + logback
    原码、反码、补码
    Java线程池(一)
    springboot 多环境配置及打包资源
    springboot自定义yaml配置文件
  • 原文地址:https://www.cnblogs.com/flower-dance/p/15231994.html
Copyright © 2011-2022 走看看