【spring cloud 第五篇】Hystrix 熔断器

梦里梦外; 2022-12-07 15:22 287阅读 0赞

文章目录

    • 1 Hystrix 介绍
      • 1.1 简介
      • 1.2 hystrix设计原则
      • 1.3 hystrix 工作机制
    • 2 RestTemplate和Ribbon 上使用熔断器的demo
      • 2.1 pom.xml添加 hystrix依赖
      • 2.2 EurekaRibbonClientApplication启动类
      • 2.3 新建RibbonServiceImpl类
      • 2.4 编写 RibbonController
      • 2.5 查看结果
    • 3 使用 hystrix dashboard监控熔断器状态
      • 3.1 hystrix dashboard简介
      • 3.2 集成hystrix dashboard遇到的坑
      • 3.3 集成hystrix dashboard 的demo

1 Hystrix 介绍

1.1 简介

分布式系统中,服务于服务之间的调用错综复杂,当某个服务出现故障,会导致依赖它的服务远程调用时线程阻塞。
hystrix提供了熔断器功能,可以阻止分布式系统中出现联动的故障。
hystrix 是通过隔离服务访问点阻止联动故障,从而提高整个分布式系统的高可用;
在这里插入图片描述
较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。
在这里插入图片描述
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。

1.2 hystrix设计原则

  • 1 快速失败(如果一个服务出现故障,则调用该服务的请求快速失败,而不是线程等待)
  • 2 提供回退[fallback]方案(在请求发生故障时,提供设定好的回退方案)
  • 3 提供熔断机制,防止故障扩散到其他服务
  • 4 提供熔断器监控组件 hystrix dashboard,实时监控熔断器状态

1.3 hystrix 工作机制

  • 当服务的某个API失败次数在一定时间内小于设置的阈值时,熔断器处于- 关闭状态,该API正常提供服务。
  • 失败次数 > 设定阈值,hystrix判定API接口出现故障,打开熔断器
  • 一定时间后出于半打开状态,处理一定数量请求,失败,则熔断器继续打开;成功,则关闭熔断器【具有自我修复】
    在这里插入图片描述
    该图片来自 https://blog.csdn.net/qq\_25484147/article/details/83375225 (有趣的灵魂配图)

2 RestTemplate和Ribbon 上使用熔断器的demo

在【spring cloud 第三篇】ribbon 负载均衡文章 1.2 @LoadBalanced结合RestTemplate实现负载均衡 的基础上进行重构

修改eureka-ribbon-client服务如下:

  • pom.xml添加 hystrix依赖

在这里插入图片描述

2.1 pom.xml添加 hystrix依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <parent>
  4. <artifactId>eureka-master</artifactId>
  5. <groupId>com.qgg</groupId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>eureka-ribbon-client</artifactId>
  10. <dependencies>
  11. <!--eureka依赖-->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-eureka</artifactId>
  15. <version>1.3.1.RELEASE</version>
  16. </dependency>
  17. <!--ribbon 负载均衡依赖-->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-ribbon</artifactId>
  21. <version>1.3.1.RELEASE</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <!--hystrix依赖-->
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-hystrix</artifactId>
  31. <version>1.3.1.RELEASE</version>
  32. </dependency>
  33. </dependencies>
  34. </project>

2.2 EurekaRibbonClientApplication启动类

在EurekaRibbonClientApplication启动类上添加 @EnableHystrix ,开启hystrix熔断器功能

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableHystrix // 开启hystrix熔断器功能
  4. public class EurekaRibbonClientApplication {
  5. @Bean
  6. @LoadBalanced
  7. RestTemplate restTemplate() {
  8. return new RestTemplate();
  9. }
  10. public static void main(String[] args) {
  11. SpringApplication.run(EurekaRibbonClientApplication.class, args);
  12. }
  13. }

2.3 新建RibbonServiceImpl类

  • 新建RibbonServiceImpl类
  • 新建getPortForHystrix方法,
  • 在getPortForHystrix方法上添加@HystrixCommand注解,指定fallbackMethod(回退)方法
  • getPortError回退方法必须跟getPortForHystrix的入参和返回参数一样

    @Service
    public class RibbonServiceImpl {

    1. @Autowired
    2. private RestTemplate restTemplate;
    3. /** * @HystrixCommand 的fallbackMethod指定回退的方法 * @param name * @return */
    4. @HystrixCommand(fallbackMethod = "getPortError")
    5. public String getPortForHystrix(String name) {
    6. String result = restTemplate.getForObject("http://eureka-client-4/getPort/"+name,String.class);
    7. return result;
    8. }
    9. public String getPortError(String name) {
    10. String result = "获取端口出错了-hystrix.test》:"+name;
    11. return result;
    12. }

2.4 编写 RibbonController

  1. @RestController
  2. @RequestMapping("/myRibbon")
  3. public class RibbonController {
  4. @Autowired
  5. private RibbonServiceImpl ribbonServiceImpl;
  6. @RequestMapping(value = "/getPortForHystrix/{name}")
  7. public String getPortForHystrix(@PathVariable String name) {
  8. String result = ribbonServiceImpl.getPortForHystrix(name);
  9. return result;
  10. }
  11. }

2.5 查看结果

  • 启动 eureka-server 和 eureka-client,再启动eureka-ribbon-client
  • 访问http://localhost:8761/ 如下图:在这里插入图片描述
  • 访问http://localhost:8766/myRibbon/getPortForHystrix/恰恰 ,返回正常
  • 【奇怪现象:debug模式调试的时候都是返回:获取端口出错了-hystrix.test》:恰恰】

    恰恰-8763

  • 关闭eureka-client服务继续访问 http://localhost:8766/myRibbon/getPortForHystrix/恰恰 结果返回如下:

    获取端口出错了-hystrix.test》:恰恰

  • 当被调用的服务不可用时,会开启熔断器,进入fallback方法,线程不在阻塞

3 使用 hystrix dashboard监控熔断器状态

3.1 hystrix dashboard简介

Hystrix Dashboard 是监控hystrix的熔断器状态的一个组件,提供了数据监控和油耗的图形化展示界面。

3.2 集成hystrix dashboard遇到的坑

  • 原来使用spring-cloud-dependencies的版本Dalston.SR1 ,对应的spring-boot-starter-parent的版本是1.5.2,在集成hystrix dashboard后,访问http://localhost:8766/hystrix.stream 一直打印ping…,访问http://localhost:8766/hystrix 也一直显示在loading中,刷新页面还报错
  • 找了一些资料说是版本问题,在升级spring-cloud和spring-boot对应的版本兼容也遇到一些坑,查找spring-cloud和spring-boot的版本对应关系:访问 https://spring.io/ —> projects —> spring-cloud – doc,再仔细找找就能找到在这里插入图片描述
  • 将spring-cloud-dependencies版本跟换成了Finchley.SR3,spring-boot-starter-parent版本换成了2.0.4.RELEASE
    在这里插入图片描述
  • eureka-server服务中的 spring-cloud-starter-eureka-server 从1.3.1升级到1.4.3;eureka-client和eureka-ribbon-client服务中的 spring-cloud-starter-eureka从1.3.1升级到1.4.3,spring-cloud-starter-ribbon也是从1.3.1升级到1.4.3,包括spring-cloud-starter-hystrix和spring-cloud-starter-hystrix-dashboard都是使用1.4.3的

3.3 集成hystrix dashboard 的demo

在本章第2节的基础上进行重构

  • 在pom文件中添加Actuator起步依赖,hystrix-dashboard起步依赖和hystrix起步依赖

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-hystrix</artifactId>
    4. <version>1.4.3.RELEASE</version>
    5. <!--<version>1.3.1.RELEASE</version>-->
    6. </dependency>
    7. <dependency>
    8. <groupId>org.springframework.cloud</groupId>
    9. <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    10. <version>1.4.3.RELEASE</version>
    11. <!--<version>1.3.1.RELEASE</version>-->
    12. </dependency>
    13. <dependency>
    14. <groupId>org.springframework.boot</groupId>
    15. <artifactId>spring-boot-starter-actuator</artifactId>
    16. </dependency>
  • 在程序启动类添加 @EnableHystrixDashboard // 开启监控熔断器状态

  • 同时要注入 ServletRegistrationBean 实例,不然在访问 http://localhost:8766/hystrix.stream是404

    @SpringBootApplication
    @EnableEurekaClient
    @EnableHystrix // 开启hystrix熔断器功能
    @EnableHystrixDashboard // 开启监控熔断器状态
    public class EurekaRibbonClientApplication {

    1. @Bean
    2. @LoadBalanced
    3. RestTemplate restTemplate() {
    4. return new RestTemplate();
    5. }
    6. @Bean
    7. public ServletRegistrationBean getServlet() {
    8. HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    9. ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    10. registrationBean.setLoadOnStartup(1);
    11. registrationBean.addUrlMappings("/hystrix.stream");
    12. registrationBean.setName("HystrixMetricsStreamServlet");
    13. return registrationBean;
    14. }
    15. public static void main(String[] args) {
    16. SpringApplication.run(EurekaRibbonClientApplication.class, args);
    17. }

    }

  • 一次启动工程 eureka-server,eureka-client,eureka-ribbon-client,在浏览器访问http://localhost:8766/hystrix.stream,在调用http://localhost:8766/myRibbon/getPortForHystrix/熔断监控测试,打印熔断器的数据指标如下![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FpYXppbGlwaW5n_size_16_color_FFFFFF_t_70_pic_center 6]

  • 再访问http://localhost:8766/hystrix,依次填写http://localhost:8766/hystrix.stream、2000、xxx(这个随意填写)
    在这里插入图片描述
  • 点击“monitor” ,显示了熔断器的各种指标,各指标含义可查看官方文档^^
    在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,287人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring cloud 熔断Hystrix

    在微服务的体系下,服务之间是与依赖关系的!服务的调用中也存在着各种可能的问题,如果一个服务处理缓慢,可能会导致服务被阻塞住,导致整个系统被拖慢,甚至是崩溃。这种情况下可以