SpringCloud 服务降级

博客参考学习视频: https://www.bilibili.com/video/BV18E411x7eT?from=search&seid=4388336378730572330

上一篇:SpringCloud服务调用:https://blog.csdn.net/qq_45738810/article/details/109116078

一、Hystrix 断路器

① 概述

1.分布式系统面临的问题

image-20201016002519013

image-20201016002530999

2.是什么

image-20201016002559663

3.能干嘛
  • 服务降级
  • 服务熔断
  • 接近实时的监控
4.官 网 资 料

​ https://github.com/Netflix/Hystrix/wiki/How-To-Use

5.Hystrix 官宣,停更进维

​ https://github.com/Netflix/Hystrix

image-20201016003154348

  • 不在修复Bug
  • 不在接收合并请求
  • 不在发布新版本

② Hystrix 重要概念

1.服务降级

服务器忙, 请稍候再试, 不让客户端等待并立刻返回一个友好提示, fallback

哪些情况会触发降级: 1 程序运行异常、 2 超时、 3 服务熔断触发服务降级 、4 线程池/信号量打满也会导致服务降级

2.服务熔断

​ 类比保险丝达到最大服务访问后, 直接拒绝访问, 拉闸限电, 然后调用服务降级的方法并返回友好提示,就是保险丝。

服务的降级->进而熔断->恢复调用链路

3.服务限流

​ 秒杀高并发等操作, 严禁一窝蜂的过来拥挤, 大家排队, 一秒钟 N 个, 有序进行

③ hystrix 案例

1.构建

新建 cloud-provider-hystrix-payment8001

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>clould</artifactId>
        <groupId>com.oy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-hystrix-payment8001</artifactId>

    <dependencies>
        <!--新增 hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.oy</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency><groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

YML

server:
  port: 8001
eureka:
  client:
    register-with-eureka: true #表示不向注册中心注册自己
    fetch-registry: true #表名自己就是注册中心,职责维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

主启动类

@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
     public static void main(String[] args) {
           SpringApplication.run(PaymentHystrixMain8001.class, args);
      }
}

业务类

​ Service

@Service
public class PaymentService {

    // 成功
    public String paymentInfo_OK(Integer id){
        return "线程池:" + Thread.currentThread().getName() + "paymentInfo_OK,id:"+id+"\t"+"O(∩_∩)O哈哈~";
    }


    // 失败
    public String paymentInfo_TimeOut(Integer id){
        int timeNumber  = 3;
        try {
            TimeUnit.SECONDS.sleep(timeNumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "线程池:" + Thread.currentThread().getName()+ "paymentInfo_TimeOut,id:" + id +"\t" +"┭┮﹏┭┮"+"耗时(秒)" + timeNumber;
    }
}

Controller

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService  paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentService.paymentInfo_OK(id);
        log.info("*************result:" + result);

        return result;
    }

    @GetMapping(value = "/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String result = paymentService.paymentInfo_TimeOut(id);
        log.info("******result:" + result);
        return result;
    }
}

测试:

  • 先启动 eureka 7001, 启动 cloud-provider-hystrix-payment8001

  • 访问:

    http://localhost:8001/payment/hystrix/ok/1

    image-20201016152409512

  • 在测试访问:http://localhost:8001/payment/hystrix/timeout/1,每 次 调 用 耗 费 3 秒 钟

image-20201016152506946

  • 上述 module 均 OK,以上述为根基平台,从正确->错误->降级熔断->恢复
2.高并发测试

Jmeter 压测测试

​ 开 启 Jmeter , 来 20000 个 并 发 压 死 8001 , 20000 个 请 求 都 去 访 问paymentInfo_TimeOut 服务。

image-20201016154613777

  • 再来一个访问:http://localhost:8001/payment/hystrix/ok/1, http://localhost:8001/payment/hystrix/timeout/1

    看演示结果: 两个都在自己转圈圈

  • 为什么会被卡死: tomcat 的默认的工作线程数被打满了。没有多余的线程来分解压力和处理

3.Jmeter 压测结论

​ 上面还是服务提供者 8001 自己测试, 假如此时外部的消费者 80 也来访问,那消费者只能干等, 最终导致消费端 80 不满意, 服务端 8001 直接被拖死

4.看热闹不嫌弃事大, 80 新建加入

新建 cloud-consumer-feign-hystrix-order80

image-20201016155321248

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>clould</artifactId>
        <groupId>com.oy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-hystrix-order80</artifactId>

    <dependencies>
        <!--新增 hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.oy</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency><dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

YML

server:
  port: 80
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka     

主启动类

@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
     public static void main(String[] args) {
           SpringApplication.run(PaymentHystrixMain8001.class, args);
      }
}

业务类

PaymentHystrixService

package com.oy.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Author OY
 * @Date 2020/10/16
 */
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {

    /**
     * 正常访问
     *
     * @param id
     * @return
     */
    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id);

    /**
     * 超时访问
     *
     * @param id
     * @return
     */
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}

Controller

@RestController
@Slf4j
public class OrderHyrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    /**
     * 正常访问
     *
     * @param id
     * @return
     */
    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_OK(id);
        log.info("*****result:" + result);
        return result;
    }

    /**
     * 超时访问
     *
     * @param id
     * @return
     */
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        log.info("*****result:" + result);
        return result;

    }
}

正常测试

  • http://localhost/consumer/payment/hystrix/ok/1

image-20201016163217022

  • http://localhost/consumer/payment/hystrix/timeout/1

image-20201016163241381

注意:测试这个时有可能会报超时错误,如果出现错误只需要在配置文件中配置

ribbon:
  # 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
  # 指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

image-20201016163421515

高并发测试

  • 2W 个线程压 8001,消费端 80 微服务再去访问正常的 OK 微服务 8001 地址

    http://localhost/consumer/payment/hystrix/timeout/1

image-20201016163648968

​ 消费者 80, 要么转圈圈等待、 要么消费端报超时错误

5.故障现象和导致原因
  • 8001 同一层次的接口服务被困死,因为tomcat 线程里面的工作线程已经被挤占完毕
  • 80此时调用 8001,客户端访问的响应缓慢,转圈圈
6.上诉结论

​ 正因为有上述故障或不佳表现, 才有我们的降级/容错/限流等技术诞生

7.如何解决?解决的要求
  • 超时导致服务器变慢(转圈) : 超时不再等待

  • 出错(宕机或程序运行出错) : 出错要有兜底

解决:

  1. 对方服务(8001) 超时了, 调用者(80) 不能一直卡死等待,必须有服务降级。
  2. 对方服务(8001) down 机了, 调用者(80) 不能一直卡死等待, 必须有服务降级。
  3. 对方服务(8001) OK, 调用者(80) 自己出故障或有自我要求(自己的等待时间小于服务提供者) , 自己处理降级

④ 服务降级

1.降低配置: @HystrixCommand
2.8001先从自身找问题

​ 置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,做服务降级 fallback

3.8001fallback

业务类启用

image-20201016212902803

@Service
public class PaymentService {

    // 成功
    public String paymentInfo_OK(Integer id){
        return "线程池:" + Thread.currentThread().getName() + "paymentInfo_OK,id:"+id+"\t"+"O(∩_∩)O哈哈~";
    }

    /**
     * 超时访问,演示降级
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
            @HystrixProperty(name ="execution.isolation.thread.timeoutInMilliseconds",value="3000")
    })
    public String paymentInfo_TimeOut(Integer id){
        int timeNumber  = 5;
        try {
            TimeUnit.SECONDS.sleep(timeNumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "线程池:" + Thread.currentThread().getName()+ "paymentInfo_TimeOut,id:" + id +"\t" +"┭┮﹏┭┮"+"耗时(秒)" + timeNumber;
    }

    public String paymentInfo_TimeOutHandler(Integer id){
        return "o(╥﹏╥)o 调用支付接口超时异常:\t" + "\t当前线程池名字:" + Thread.currentThread().getName();
    }
}

image-20201016213119295

@HystrixCommand报异常后如何处理

一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbckMethod调用类中的指定方法

图示

image-20201016214851342

主启动类激活: 添加新注解**@EnableCircuitBreaker**

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {
     public static void main(String[] args) {
           SpringApplication.run(PaymentHystrixMain8001.class, args);
      }
}
4.80fallback
80 订单微服务, 也可以更好的保护自己, 自己也依样画葫芦进行客户端降级保护。

题外话

切记我 们 自 己 配 置 过 的 热 部 署 方 式 对 java 代 码 的 改 动 明 显 , 但对@HystrixCommand 内属性的修改建议重启微服务

YML

feign:
  hystrix:
    enabled: true # 如果处理自身的容错就开启。开启方式与生产端不一样。

image-20201016215256041

主启动: @EnableHystrix

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class OrderHystrixMain80 {
     public static void main(String[] args) {
           SpringApplication.run(OrderHystrixMain80.class, args);
      }
}

业务类

@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "15000")
})
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
    String result = paymentHystrixService.paymentInfo_TimeOut(id);
    log.info("*****result:" + result);
    return result;
}

public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
    return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}
5.目前方法
  • 每个业务方法对应一个兜底的方法,代码膨胀
  • 统一和自定义的需要分开
6.解决问题

每个方法配置一个? ? ? 膨胀

  1. feign 接口系列
  2. @DefaultProperties(defaultFallback = “”)

image-20201016221217405

说明

image-20201016221245143

controller 配置

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHyrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    /**
     * 正常访问
     *
     * @param id
     * @return
     */
    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_OK(id);
        log.info("*****result:" + result);
        return result;
    }

    /**
     * 超时访问
     *
     * @param id
     * @return
     */
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        log.info("*****result:" + result);
        return result;
    }

    // 兜底方法
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }

    /**
     * 全局fallback
     * @return
     */
    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息,请稍后重试.o(╥﹏╥)o";
    }
}

**和业务逻辑混一起? ? ? 混乱 **

  1. 服务降级,客户端去调用服务端,碰上服务器宕机或关闭

  2. 本次案例服务案例级处理是在客户端80 实现完成的,与服务端 8001 没有关系,只需要为 Feign 客户端定义的接口添加一个服务降级处理的实现类即可实现解耦。

  3. 未来我们要面对的异常

    运行、超时、宕机

  4. 再看我们的业务类 PaymentController

image-20201016230720317

  1. 修改 cloud-consumer-feign-hystrix-order80
  2. 根 据 cloud-consumer-feign-hystrix-order80 已 经 有 的PaymentHystrixService 接 口 , 重 新 新 建 一 个 类
    (PaymentFallbackService) 实现该接口, 统一为接口里面的方法进行异常处理 。
  3. PaymentFallbackService 类 实 现PaymentFeignClientService 接口
@Component
public class PaymentFallbackService implements PaymentHystrixService{
    @Override
    public String paymentInfo_OK(Integer id) {
        return "-----PaymentFallbackService fall back-paymentInfo_OK , (┬_┬)";
    }

    @Override
    public String paymentInfo_TimeOut(Integer id) {
        return "-----PaymentFallbackService fall back-paymentInfo_TimeOut , (┬ _┬)";
    }
}
  1. YML
feign:
  hystrix:
    enabled: true # 如果处理自身的容错就开启。开启方式与生产端不一样。
  1. PaymentFeignClientService 接口
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {

    /**
     * 正常访问
     *
     * @param id
     * @return
     */
    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id);

    /**
     * 超时访问
     *
     * @param id
     * @return
     */
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
  1. 测试
  • 单个 eureka 先启动 7001,PaymentHystrixMain8001 启动

  • 正常测试: http://localhost/consumer/payment/hystrix/ok/1

image-20201016231719706

  • 故意关闭微服务 8001

image-20201016231758903

​ 客户端自己调用提升: 此时服务端 provider 已经 down 了, 但是我们做了服务降级处理, 让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器 。

⑤ 服务熔断

1.断路器

​ 一句话就是家里保险丝

2.熔断是什么

image-20201016232022777

大神论文:https://martinfowler.com/bliki/CircuitBreaker.html

3.实操

修改 cloud-provider-hystrix-payment8001

PaymentService

    @HystrixCommand(fallbackMethod =
            "paymentCircuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //时间范围
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), //失败率达到多少后跳闸
    })
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
    if (id < 0){
        throw new RuntimeException("*****id 不能负数");
     }
    String serialNumber = IdUtil.simpleUUID();
    return Thread.currentThread().getName()+"\t"+"调用成功,流水号: "+serialNumber;
}

public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
    return "id 不能负数, 请稍候再试,(┬_┬)/~~ id: " +id;
}

why 配置这些参数:

image-20201016232453956

PaymentController

@GetMapping("/payment/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
    String result = paymentService.paymentCircuitBreaker(id);
    log.info("*******result:"+result);
    return result;
}

测试

  1. 自测 cloud-provider-hystrix-payment8001

image-20201016232734315

  1. 正确: http://localhost:8001/payment/circuit/1

    image-20201016233026906

  2. 错误: http://localhost:8001/payment/circuit/-1

image-20201016233053779

  1. 一次正确一次错误 trytry
  2. 重点测试: 多次错误,然后慢慢正确, 发现刚开始不满足条件,就算是正确的访问地址也不能进行访问, 需要慢慢的恢复链路
4.原理(小总结)

大神结论

image-20201016233342518

熔断类型

  1. 熔断打开

    请求不再进行调用当前服务, 内部设置时钟一般为 MTTR(平均故障处理时间),当打开时长达到所设时钟则进入熔断状态 。

  2. 熔断关闭

    熔断关闭不会对服务进行熔断。

  3. 熔断半开

    部分请求根据规则调用当前服务, 如果请求成功且符合规则则认为当前服务恢复正常, 关闭熔断 。

官网断路器流程图

  1. 官网步骤

image-20201016233656920

  1. 断路器在什么情况下开始起作用

image-20201016233720377

  1. 断路器开启或者关闭的条件
  • 当满足一定阀值的时候(默认 10 秒内超过 20 个请求次数)

  • 当失败率达到一定的时候(默认 10 秒内超过 50%请求失败)

  • 到达以上阀值, 断路器将会开启

  • 当开启的时候, 所有请求都不会进行转发

  • 一段时间之后(默认是 5 秒), 这个时候断路器是半开状态, 会让其中一个请求进行转发。 如果成功, 断路器会关闭, 若失败, 继续开启。 重复 4 和 5 。

  1. 断路器打开之后

    image-20201016234107457

  2. All 配置

image-20201016234250739

image-20201016234229443

image-20201016234319121

image-20201016234330093

⑥ 服务限流

参考我后面的关于高级篇的 alibaba 的 Sentinel 说明

⑦ hystrix 工作流程

1.hystrix 工作流程

https://github.com/Netflix/Hystrix/wiki/How-it-Works

2.hystrix 工作流程

**官网图例 : **

image-20201017152337133

步骤说明:

image-20201017152658751

⑧ 服务监控 hystrixDashboard

1.概述

image-20201017153028883

2.仪表盘 9001

新建 cloud-consumer-hystrix-dashboard9001

image-20201017153155398

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>clould</artifactId>
        <groupId>com.oy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>

    <dependencies><!--新增 hystrix dashboard-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

YML

server:
  port: 9001

HystrixDashboardMain9001+新注解**@EnableHystrixDashboard**

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardMain9001 {
     public static void main(String[] args) {
           SpringApplication.run(HystrixDashboardMain9001.class, args);
      }
}

所有 Provider 微服务提供类(8001/8002/8003) 都需要监控依赖配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

启动cloud-consumer-hystrix-dashboard9001该微服务后续将监控微服务8001

​ http://localhost:9001/hystrix

image-20201017155956437

3.断路器演示(服务监控hystrixDashboard)

修改cloud-provider-hystrix-payment8001

注意:新版本Hystrix需要在主启动PaymentHystrixMain8001中指定监控路径

    /**
     * 此配置是为了服务监控而配置,与服务容错本身无观,springCloud 升级之后的坑
     * ServletRegistrationBean因为springboot的默认路径不是/hystrix.stream
     * 只要在自己的项目中配置上下面的servlet即可
     * @return
     */
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

image-20201017161411381

  • Unable to connect to Command Metric Stream.
  • 404

监控测试

  • 启动 1 个 eureka 或者 3 个 eureka 集群均可
  • 观察监控窗口

9001 监控 8001

image-20201017162106329

  • 填写监控地址: http://localhost:8001/hystrix.stream

  • 测试地址: http://localhost:8001/payment/circuit/1、http://localhost:8001/payment/circuit/1

  • 上述测试通过: ok

  • 先访问正确地址, 再访问错误地址, 再正确地址, 会发现图示断路器都,是慢慢放开的 。

监控结果,成功

image-20201017162411201

监控结果, 失败

image-20201017162501189

  • 如何看

7 色 1 圈

image-20201017162751506

image-20201017162643326

1 线

image-20201017162702560

整图说明

image-20201017162830940

整图说明 2

image-20201017162858611
搞懂一个才能看懂复杂的

image-20201017163035515

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值