1、介绍

image.png

In a distributed environment, inevitably some of the many service dependencies will fail. Hystrix is a library that helps you control the interactions between these distributed services by adding latency tolerance and fault tolerance logic. Hystrix does this by isolating points of access between the services, stopping cascading failures across them, and providing fallback options, all of which improve your system’s overall resiliency. —[摘自官方]

在分布式环境中,许多服务依赖中的一些不可避免地会失败。 Hystrix 是一个库,它通过添加延迟容错和容错逻辑来帮助您控制这些分布式服务之间的交互。 Hystrix 通过隔离服务之间的访问点、阻止它们之间的级联故障并提供回退选项来实现这一点,所有这些都提高了系统的整体弹性。

2、服务雪崩

在微服务之间进行服务调用是由于某一个服务故障,导致级联服务故障的现象,称为雪崩效应。雪崩效应描述的是提供方不可用,导致消费方不可用并将不可用逐渐放大的过程。

举例: 如存在如下调用链路
image.png
此时,Service A的流量波动很大,流量经常会突然性增加!那么在这种情况下,就算Service A能扛得住请求,Service B和Service C未必能扛得住这突发的请求。此时,如果Service C因为抗不住请求,变得不可用。那么Service B的请求也会阻塞,慢慢耗尽Service B的线程资源,Service B就会变得不可用。紧接着,Service A也会不可用,这一过程如下图所示:
image.png

3、服务熔断

“熔断器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控,某个异常条件被触发,直接熔断整个服务。向调用方法返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方法无法处理的异常,就保证了服务调用方的线程不会被长时间占用,避免故障在分布式系统中蔓延,乃至雪崩。如果目标服务情况好转则恢复调用。服务熔断是解决服务雪崩的重要手段。

image.png

4、服务降级

服务压力剧增的时候根据当前的业务情况及流量对一些服务和页面有策略的降级,以此环节服务器的压力,以保证核心任务的进行。同时保证部分甚至大部分任务客户能得到正确的相应。也就是当前的请求处理不了了或者出错了,给一个默认的返回。

通俗: 关闭系统中边缘服务 保证系统核心服务的正常运行 称之为服务降级
如: 双12 淘宝 删除地址 确认收货 删除订单 节省cpu 内存

image.png

5、降级和熔断总结

5.1 共同点

目的很一致,都是从可用性可靠性着想,为防止系统的整体缓慢甚至崩溃,采用的技术手段; 最终表现类似,对于两者来说,最终让用户体验到的是某些功能暂时不可达或不可用; 粒度一般都是服务级别,当然,业界也有不少更细粒度的做法,比如做到数据持久层(允许查询,不允许增删改); 自治性要求很高,熔断模式一般都是服务基于策略的自动触发,降级虽说可人工干预,但在微服务架构下,完全靠人显然不可能,开关预置、配置中心都是必要手段;

5.2 异同点

触发原因不太一样,服务熔断一般是某个服务(下游服务)故障引起,而服务降级一般是从整体负荷考虑; 管理目标的层次不太一样,熔断其实是一个框架级的处理,每个微服务都需要(无层级之分),而降级一般需要对业务有层级之分(比如降级一般是从最外围服务开始)

5.3 总结

熔断必会触发降级,所以熔断也是降级一种,区别在于熔断是对调用链路的保护,而降级是对系统过载的一种保护处理

6、服务熔断的实现

6.1 pom.xml

  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"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.5.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.morrow</groupId>
  12. <artifactId>spring-cloud-consul-products-hystrix-6018</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>spring-cloud-consul-products-hystrix-6018</name>
  15. <description>spring-cloud-consul-products-hystrix-6018 project for Spring Boot</description>
  16. <properties>
  17. <java.version>11</java.version>
  18. <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.projectlombok</groupId>
  27. <artifactId>lombok</artifactId>
  28. <optional>true</optional>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. <!--引入consul依赖-->
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  39. </dependency>
  40. <!-- 健康度监控-->
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-actuator</artifactId>
  44. </dependency>
  45. <!--引入hystrix-->
  46. <dependency>
  47. <groupId>org.springframework.cloud</groupId>
  48. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  49. </dependency>
  50. </dependencies>
  51. <!--全局管理springcloud版本,并不会引入具体依赖-->
  52. <dependencyManagement>
  53. <dependencies>
  54. <dependency>
  55. <groupId>org.springframework.cloud</groupId>
  56. <artifactId>spring-cloud-dependencies</artifactId>
  57. <version>${spring-cloud.version}</version>
  58. <type>pom</type>
  59. <scope>import</scope>
  60. </dependency>
  61. </dependencies>
  62. </dependencyManagement>
  63. <build>
  64. <plugins>
  65. <plugin>
  66. <groupId>org.springframework.boot</groupId>
  67. <artifactId>spring-boot-maven-plugin</artifactId>
  68. <configuration>
  69. <excludes>
  70. <exclude>
  71. <groupId>org.projectlombok</groupId>
  72. <artifactId>lombok</artifactId>
  73. </exclude>
  74. </excludes>
  75. </configuration>
  76. </plugin>
  77. </plugins>
  78. </build>
  79. </project>

6.2 yml 文件

  1. server:
  2. port: 6108
  3. spring:
  4. application:
  5. name: products
  6. cloud:
  7. consul:
  8. host: localhost #注册consul服务的主机
  9. port: 8500 #注册consul服务的端口号
  10. discovery:
  11. # register-health-check: true # 开启 关闭consul 服务的健康检查[不推荐]
  12. service-name: ${spring.application.name} #指定注册的服务名称 默认就是应用名
  13. heartbeat:
  14. enabled: true #心跳机制

6.3 ProductController 文件

  1. @RestController
  2. @Slf4j
  3. public class ProductController {
  4. //服务熔断
  5. @GetMapping("/product/break")
  6. @HystrixCommand(fallbackMethod = "testBreakFall" )
  7. public String testBreak(int id){
  8. log.info("接收的商品id为: "+ id);
  9. if(id<=0){
  10. throw new RuntimeException("数据不合法!!!");
  11. }
  12. return "当前接收商品id: "+id;
  13. }
  14. public String testBreakFall(int id){
  15. return "当前数据不合法: "+id;
  16. }
  17. }

6.4 @EnableCircuitBreaker 注解

  1. package com.morrow.springcloudconsulproductshystrix6018;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
  5. @SpringBootApplication
  6. @EnableCircuitBreaker //用来开启断路器
  7. public class SpringCloudConsulProductsHystrix6018Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringCloudConsulProductsHystrix6018Application.class, args);
  10. }
  11. }

image.png
image.png

7、 服务降级的实现

7.1 断路器打开条件

官网: https://cloud.spring.io/spring-cloud-netflix/2.2.x/reference/html/#circuit-breaker-spring-cloud-circuit-breaker-with-hystrix

A service failure in the lower level of services can cause cascading failure all the way up to the user. When calls to a particular service exceed circuitBreaker.requestVolumeThreshold (default: 20 requests) and the failure percentage is greater than circuitBreaker.errorThresholdPercentage (default: >50%) in a rolling window defined by metrics.rollingStats.timeInMilliseconds (default: 10 seconds), the circuit opens and the call is not made. In cases of error and an open circuit, a fallback can be provided by the developer. —摘自官方

1、 当满足一定的阀值的时候(默认10秒内超过20个请求次数) 2、 当失败率达到一定的时候(默认10秒内超过50%的请求失败) 3、到达以上阀值,断路器将会开启 4、当开启的时候,所有请求都不会进行转发 5、 一段时间之后(默认是5秒),这个时候断路器是半开状态,会让其中一个请求进行转发。如果成功,断路器会关闭,若失败,继续开启。重复4和5。

image.png