文章目录
SpringCloud从入门到放弃 06 ——Hystrix断路器
一、概述
1.分布式系统面临的问题
复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败
服务雪崩
多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其它的微服务,这就是所谓的“扇出”。如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的“雪崩效应”.
对于高流量的应用来说,单一的后端依赖可能会导致所有服务器上的所有资源都在几秒钟内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障。这些都表示需要对故障和延迟进行隔离和管理,以便单个依赖关系的失败,不能取消整个应用程序或系统。
所以,
通常当你发现一个模块下的某个实例失败后,这时候这个模块依然还会接收流量,然后这个有问题的模块还调用了其他的模块,这样就会发生级联故障,或者叫雪崩。
2.Hystrix简介
官网:https://github.com/Netflix/Hystrix/wiki/How-To-Use
目前已经停更进维
Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
3.Hystrix能干嘛
1.服务降级:
不让客户端等待并立刻返回一个友好提示——Fallback(如:服务器忙,请稍后再试)
Fallback相当于是降级操作. 对于查询操作, 我们可以实现一个fallback方法, 当请求后端服务出现异常的时候, 可以使用fallback方法返回的值. fallback方法的返回值一般是设置的默认值或者来自缓存.告知后面的请求服务不可用了,不要再来了
发生服务降级的情况:
- 程序运行异常
- 超时
- 服务熔断触发服务降级
- 线程池/信号量打满也会导致服务降级
2.服务熔断
当扇出链路的微服务的异常条件阈值被触发,就是直接熔断整个服务,不是等到此服务超时(类似于保险丝熔断就直接拉闸了),然后调用服务降级的方法并快速返回友好提示,当检测到该节点微服务响应正常后恢复调用链路
3.服务限流
通过线程池+队列的方式,通过信号量的方式。比如商品评论比较慢,最大能同时处理10个线程,队列待处理5个,那么如果同时20个线程到达的话,其中就有5个线程被限流了,其中10个先被执行,另外5个在队列中
二、Hystrix 使用
一、构建
-
新建cloud-provider-hystrix-payment8001
-
写pom.xml
<dependencies>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--web-->
<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><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.avgrado.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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>
- 写application.yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
# 单机版
#defaultZone: http://localhost:7001/eureka # 入驻的服务注册中心地址
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7002.com:7003/eureka
- 主启动类
package com.avgrado.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixApplication8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixApplication8001.class,args);
}
}
- 业务类编写
sevice:
package com.avgrado.springcloud.service;
public interface PaymentHystrixService {
String paymentInfo_OK(Integer id);
String paymentInfo_Timeout(Integer id);
}
serviceImpl:
package com.avgrado.springcloud.service.impl;
import com.avgrado.springcloud.service.PaymentHystrixService;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class PaymentHystrixServiceImpl implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "线程池:"+Thread.currentThread().getName()+"paymentInfo_OK,id: "+id+"\t"+"O(∩_∩)O";
}
@Override
public String paymentInfo_Timeout(Integer id) {
try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }
return "线程池:"+Thread.currentThread().getName()+"paymentInfo_TimeOut,id: "+id+"\t"+"O(∩_∩)O,耗费5秒";
}
}
contorller:
package com.avgrado.springcloud.controller;
import com.avgrado.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("payment")
@Slf4j
public class PaymentHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id)
{
String result = paymentHystrixService.paymentInfo_OK(id);
log.info("****result: "+result);
return result;
}
@GetMapping("/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) throws InterruptedException
{
String result = paymentHystrixService.paymentInfo_Timeout(id);
log.info("****result: "+result);
return result;
}
}
- 正常测试
可以看到访问 OK 的方法速度是很快的,访问timeout 的方法是延时了5秒,访问正常
二、高并发测试
1.高并发测试 (只测试服务提供方):
.配置线程组
配置请求
启动Jmeter后再访问
访问 timeout 方法:可以看到,访问时间已经超出了之前设置的5秒延时的时间
访问 ok 方法 :可以看到,相比没有做高并发压测的时候满了很多,访问时转圈
原因:tomcat的默认的工作线程数被打满 了,没有多余的线程来分解压力和处理访问 ok 方法的请求
此时还只是服务提供者8001自己测试,假如此时外部的消费者80也来访问,那消费者只能干等,最终导致消费端80不满意,服务端8001直接被拖死
2.高并发测试 (通过服务消费方测试)
建moudle : cloud-consumer-feign-hystrix-order80
写pom.xml
<?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>mscloud</artifactId>
<groupId>com.avgrado.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-feign-hystrix-order80</artifactId>
<dependencies>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.avgrado.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--web-->
<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>
写application.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,http://eureka7002.com:7003/eureka
主启动类
package com.avgrado.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class OrderHystrixApplication80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixApplication80.class,args);
}
}
业务类
service:
package com.avgrado.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;
@Component
@FeignClient(value="CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
controller:
package com.avgrado.springcloud.controller;
import com.avgrado.springcloud.service.PaymentHystrixService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("order")
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id)
{
String result = paymentHystrixService.paymentInfo_OK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id)
{
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
}
- 启动消费方服务,正常测试下服务消费方:可以看到访问响应速度非常快
- 通过Jmeter 模拟高并发测试
用http://localhost/consumer/payment/hystrix/ok/31 对服务提供方的 OK 服务进行访问,和之前一样,无法迅速访问该服务,如果压力测试中的线程数更多的时候,很可能会造成超时错误,出现以下错误提示::
从消费方的控制台可以看到也是提示调用超时
原因:8001同一层次的其它接口服务被困死,因为tomcat线程池里面的工作线程已经被挤占完毕,没有多余的线程来分解压力和处理访问 ok 方法的请求了,所以会导致访问变慢甚至卡死
正是因为出现了这种现象,所以我们才需要服务降级、容错、服务限流等技术
如何解决:
- 超时导致服务器变慢:超时不再等待
- 出错(宕机或者程序运行出错):出错要有兜底
方案:
对方服务(8001)超时了,调用者(80)不能一直卡死等待,必须有服务降级
对方服务(8001)宕机了,调用者(80)不能一直卡死等待,必须有服务降级
对方服务(8001)OK,调用者(80)自己出故障或有自我要求(自己的等待时间小于服务提供者),自己处理降级
三、服务降级(Fallback)
1.降级配置:
用 @HystrixCommand 注解做降级配置,在服务提供方自身找问题,设置自身调用超时时间的峰值,在峰值内可以正常运行,超过了峰值需要有兜底的方法处理,用作服务降级
2.实现在服务提供方上 超时/异常 情况下的处理(降级配置)
- 业务类上启用
package com.avgrado.springcloud.service.impl;
import com.avgrado.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class PaymentHystrixServiceImpl implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "线程池:"+Thread.currentThread().getName()+"paymentInfo_OK,id: "+id+"\t"+"O(∩_∩)O";
}
@Override
@HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler",commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMillisSeconds",value="3000")
})
public String paymentInfo_Timeout(Integer id) {
try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }
return "线程池:"+Thread.currentThread().getName()+"paymentInfo_TimeOut,id: "+id+"\t"+"O(∩_∩)O,耗费5秒";
}
public String paymentInfo_TimeoutHandler(Integer id) {
return "/(ㄒoㄒ)/调用支付接口超时或异常:\t"+ "\t当前线程池名字" + Thread.currentThread().getName();
}
}
- 主启动类上启用
在主启动类上添加 @EnableHystirx 注解
package com.avgrado.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class PaymentHystrixApplication8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixApplication8001.class,args);
}
}
- 测试(超时情况):
由于设置的超时时间是3秒,而调用的方法是延时了5秒,所以会触发超时的 fallback ,如下图,在经过3秒钟后就直接执行配置的 fallback 方法
- 测试(程序异常情况):
paymentInfo_Timeout 方法中增加一行代码: int a = 10/0 ;模拟程序运行出错时的情况
@Override
@HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler",commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")
})
public String paymentInfo_Timeout(Integer id) {
int a = 10/0;
try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }
return "线程池:"+Thread.currentThread().getName()+"paymentInfo_TimeOut,id: "+id+"\t"+"O(∩_∩)O,耗费5秒";
}
浏览器输入 http://localhost:8001/payment/hystrix/timeout/3 访问测试,如下图:和测试超时情况不同,此时访问并不需要等待3秒钟再出现 fallback 方法的友好提示,而是直接返回
由上述两个测试可以看出:
当前服务不可用或者超时,做服务降级,兜底的方案都是paymentInfo_TimeOutHandler
注意:既然服务的提供方可以进行降级保护,那么服务的消费方,也可以更好的保护自己,也可以对自己进行降级保护,也就是说Hystrix服务降级既可以放在服务端(服务提供方),也可以放在客户端(服务消费方),而且通常是用 客户端做服务降级
3.实现在服务消费方的 超时/异常 情况下的处理(降级配置)
- cloud-consumer-feign-hystrix-order80 服务application.yml 配置文件修改,增加 feign.hystrix.enabled=true 配置
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,http://eureka7002.com:7003/eureka
feign:
hystrix:
enabled: true
- 主启动类上增加 @EnableHystrix 注解
package com.avgrado.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class OrderHystrixApplication80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixApplication80.class,args);
}
}
浏览器输入 http://localhost/order/consumer/payment/hystrix/timeout/31 访问测试,如下图:在等待2秒钟后执行了 fallback 方法中的内容
注意:配置的fallback 方法和访问的方法参数列表要一致
否则访问则会报错:
com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found: paymentInfo_TimeOutHandler([class java.lang.Integer])
at com.netflix.hystrix.contrib.javanica.utils.MethodProvider$FallbackMethodFinder.doFind(MethodProvider.java:190) ~[hystrix-javanica-1.5.18.jar:1.5.18]
目前存在的问题一:每个业务方法对应一个 fallback 的方法,代码膨胀
解决方法:配置统一的 fallback 方法,除了个别重要核心业务有专属,其它普通的可以通过 @DefaultProperties(defaultFallback = “”) 注解跳转到统一的处理结果页面
修改后的代码如下:
package com.avgrado.springcloud.controller;
import com.avgrado.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.cloud.netflix.hystrix.HystrixProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("order")
@DefaultProperties(defaultFallback = "paymentGlobalFallbackMethod",commandProperties = {
@HystrixProperty(name="execution.isolation.timeoutInMilliseconds",value="2000")
})
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id)
{
String result = paymentHystrixService.paymentInfo_OK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand
/* @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")
})*/
public String paymentInfo_TimeOut(@PathVariable("id") Integer id)
{
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
public String paymentInfo_TimeOutHandler(@PathVariable("id") Integer id){
return "我是消费者80,对方系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}
public String paymentGlobalFallbackMethod(){
return "这是统一的 fallback 处理方法";
}
}
切记无论是否配置了定制服务降级方法,都要在其服务上加入注解 @HystrixCommand,一定不能去掉了这个注解
测试:
目前存在的问题二:我们将服务降级方法和业务逻辑混合在了一起,这会导致代码混乱,业务逻辑不清晰
解决方法:可以为 cloud-consumer-feign-hystrix-order80 客户端(服务消费方)定义的接口添加一个服务降级处理的实现类即可实现解耦,根据 cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentHystrixFallbackService)实现该接口,统一为接口里面的方法进行异常处理
代码如下:
PaymentHystrixService :
package com.avgrado.springcloud.service;
import com.avgrado.springcloud.service.impl.PaymentHystrixFallbackService;
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;
@Component
@FeignClient(value="CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentHystrixFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
PaymentHystrixFallbackService
package com.avgrado.springcloud.service.impl;
import com.avgrado.springcloud.service.PaymentHystrixService;
import org.springframework.stereotype.Component;
@Component
public class PaymentHystrixFallbackService implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "服务调用失败,提示来自:cloud-consumer-feign-order80";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
return "服务调用失败,提示来自:cloud-consumer-feign-order80";
}
}
OrderHystrixController(去除之前单独配置的 fallback 相关注解和方法) :
package com.avgrado.springcloud.controller;
import com.avgrado.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.cloud.netflix.hystrix.HystrixProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("order")
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id)
{
String result = paymentHystrixService.paymentInfo_OK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id)
{
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
/*public String paymentInfo_TimeOutHandler(@PathVariable("id") Integer id){
return "我是消费者80,对方系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}*/
}
测试:
关闭8001服务提供方服务,模拟服务器宕机,可以发现在服务访问出现错误时,访问了配置的PaymentHystrixFallbackService 类中的 fallback 方法
四、服务熔断
熔断机制:熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务出错不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回错误的响应信息。
当检测到该节点微服务调用响应正常后,恢复调用链路。
在Spring Cloud框架里,熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况,当失败的调用到一定阈值,缺省是5秒内20次调用失败,就会启动熔断机制。熔断机制的注解是 @HystrixCommand。详细解释可以参考 服务熔断机制
案例
- 修改服务提供方 cloud-provider-hystrix-payment8001 接口实现类PaymentHystrixServiceImpl中添加代码
@Override
@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(Integer id) {
if(id < 0)
{
throw new RuntimeException("******id 不能负数");
}
String serialNumber = IdUtil.simpleUUID();
return Thread.currentThread().getName()+"\t"+"调用成功,流水号: " + serialNumber;
}
public String paymentCircuitBreaker_fallback(Integer id)
{
return "id 不能负数,请稍后再试,/(ㄒoㄒ)/~~ id: " +id;
}
@HystrixCommand 注解中配置的属性含义是:
在10秒时间内请求10次,如果有6次是失败的,就触发熔断器。
PaymentHystrixController中增加如下方法
@GetMapping("/hystrix/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
String result = paymentService.paymentCircuitBreaker(id);
log.info("****result: "+result);
return result;
}
PaymentHystrixServiceImpl 中添加的方法上 @HystrixCommand 注解中配置熔断机制的参数,配置的参数含义如下:
属性名 | 属性含义 | 默认值 |
---|---|---|
circuitBreaker.enabled | 是否开启断路器 | true |
circuitBreaker.requestVolumeThreshold | 请求次数 | 20 |
circuitBreaker.sleepWindowInMilliseconds | 时间窗口期 | 5000(毫秒) |
circuitBreaker.errorThresholdPercentage | 失败率阈值 | 50 |
更多详细配置可直接参考 com.netflix.hystrix.HystrixCommandProperties 源码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.netflix.hystrix;
import com.netflix.hystrix.strategy.properties.HystrixDynamicProperty;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty;
import com.netflix.hystrix.strategy.properties.HystrixProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class HystrixCommandProperties {
private static final Logger logger = LoggerFactory.getLogger(HystrixCommandProperties.class);
static final Integer default_metricsRollingStatisticalWindow = 10000;
private static final Integer default_metricsRollingStatisticalWindowBuckets = 10;
private static final Integer default_circuitBreakerRequestVolumeThreshold = 20;
private static final Integer default_circuitBreakerSleepWindowInMilliseconds = 5000;
private static final Integer default_circuitBreakerErrorThresholdPercentage = 50;
private static final Boolean default_circuitBreakerForceOpen = false;
static final Boolean default_circuitBreakerForceClosed = false;
private static final Integer default_executionTimeoutInMilliseconds = 1000;
private static final Boolean default_executionTimeoutEnabled = true;
private static final HystrixCommandProperties.ExecutionIsolationStrategy default_executionIsolationStrategy;
private static final Boolean default_executionIsolationThreadInterruptOnTimeout;
private static final Boolean default_executionIsolationThreadInterruptOnFutureCancel;
private static final Boolean default_metricsRollingPercentileEnabled;
private static final Boolean default_requestCacheEnabled;
private static final Integer default_fallbackIsolationSemaphoreMaxConcurrentRequests;
private static final Boolean default_fallbackEnabled;
private static final Integer default_executionIsolationSemaphoreMaxConcurrentRequests;
private static final Boolean default_requestLogEnabled;
private static final Boolean default_circuitBreakerEnabled;
private static final Integer default_metricsRollingPercentileWindow;
private static final Integer default_metricsRollingPercentileWindowBuckets;
private static final Integer default_metricsRollingPercentileBucketSize;
private static final Integer default_metricsHealthSnapshotIntervalInMilliseconds;
private final HystrixCommandKey key;
private final HystrixProperty<Integer> circuitBreakerRequestVolumeThreshold;
private final HystrixProperty<Integer> circuitBreakerSleepWindowInMilliseconds;
private final HystrixProperty<Boolean> circuitBreakerEnabled;
private final HystrixProperty<Integer> circuitBreakerErrorThresholdPercentage;
private final HystrixProperty<Boolean> circuitBreakerForceOpen;
private final HystrixProperty<Boolean> circuitBreakerForceClosed;
private final HystrixProperty<HystrixCommandProperties.ExecutionIsolationStrategy> executionIsolationStrategy;
private final HystrixProperty<Integer> executionTimeoutInMilliseconds;
private final HystrixProperty<Boolean> executionTimeoutEnabled;
private final HystrixProperty<String> executionIsolationThreadPoolKeyOverride;
private final HystrixProperty<Integer> executionIsolationSemaphoreMaxConcurrentRequests;
private final HystrixProperty<Integer> fallbackIsolationSemaphoreMaxConcurrentRequests;
private final HystrixProperty<Boolean> fallbackEnabled;
private final HystrixProperty<Boolean> executionIsolationThreadInterruptOnTimeout;
private final HystrixProperty<Boolean> executionIsolationThreadInterruptOnFutureCancel;
private final HystrixProperty<Integer> metricsRollingStatisticalWindowInMilliseconds;
private final HystrixProperty<Integer> metricsRollingStatisticalWindowBuckets;
private final HystrixProperty<Boolean> metricsRollingPercentileEnabled;
private final HystrixProperty<Integer> metricsRollingPercentileWindowInMilliseconds;
private final HystrixProperty<Integer> metricsRollingPercentileWindowBuckets;
private final HystrixProperty<Integer> metricsRollingPercentileBucketSize;
private final HystrixProperty<Integer> metricsHealthSnapshotIntervalInMilliseconds;
private final HystrixProperty<Boolean> requestLogEnabled;
private final HystrixProperty<Boolean> requestCacheEnabled;
protected HystrixCommandProperties(HystrixCommandKey key) {
this(key, new HystrixCommandProperties.Setter(), "hystrix");
}
protected HystrixCommandProperties(HystrixCommandKey key, HystrixCommandProperties.Setter builder) {
this(key, builder, "hystrix");
}
protected HystrixCommandProperties(HystrixCommandKey key, HystrixCommandProperties.Setter builder, String propertyPrefix) {
this.key = key;
this.circuitBreakerEnabled = getProperty(propertyPrefix, key, "circuitBreaker.enabled", builder.getCircuitBreakerEnabled(), default_circuitBreakerEnabled);
this.circuitBreakerRequestVolumeThreshold = getProperty(propertyPrefix, key, "circuitBreaker.requestVolumeThreshold", builder.getCircuitBreakerRequestVolumeThreshold(), default_circuitBreakerRequestVolumeThreshold);
this.circuitBreakerSleepWindowInMilliseconds = getProperty(propertyPrefix, key, "circuitBreaker.sleepWindowInMilliseconds", builder.getCircuitBreakerSleepWindowInMilliseconds(), default_circuitBreakerSleepWindowInMilliseconds);
this.circuitBreakerErrorThresholdPercentage = getProperty(propertyPrefix, key, "circuitBreaker.errorThresholdPercentage", builder.getCircuitBreakerErrorThresholdPercentage(), default_circuitBreakerErrorThresholdPercentage);
this.circuitBreakerForceOpen = getProperty(propertyPrefix, key, "circuitBreaker.forceOpen", builder.getCircuitBreakerForceOpen(), default_circuitBreakerForceOpen);
this.circuitBreakerForceClosed = getProperty(propertyPrefix, key, "circuitBreaker.forceClosed", builder.getCircuitBreakerForceClosed(), default_circuitBreakerForceClosed);
this.executionIsolationStrategy = getProperty(propertyPrefix, key, "execution.isolation.strategy", builder.getExecutionIsolationStrategy(), default_executionIsolationStrategy);
this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadTimeoutInMilliseconds(), default_executionTimeoutInMilliseconds);
this.executionTimeoutEnabled = getProperty(propertyPrefix, key, "execution.timeout.enabled", builder.getExecutionTimeoutEnabled(), default_executionTimeoutEnabled);
this.executionIsolationThreadInterruptOnTimeout = getProperty(propertyPrefix, key, "execution.isolation.thread.interruptOnTimeout", builder.getExecutionIsolationThreadInterruptOnTimeout(), default_executionIsolationThreadInterruptOnTimeout);
this.executionIsolationThreadInterruptOnFutureCancel = getProperty(propertyPrefix, key, "execution.isolation.thread.interruptOnFutureCancel", builder.getExecutionIsolationThreadInterruptOnFutureCancel(), default_executionIsolationThreadInterruptOnFutureCancel);
this.executionIsolationSemaphoreMaxConcurrentRequests = getProperty(propertyPrefix, key, "execution.isolation.semaphore.maxConcurrentRequests", builder.getExecutionIsolationSemaphoreMaxConcurrentRequests(), default_executionIsolationSemaphoreMaxConcurrentRequests);
this.fallbackIsolationSemaphoreMaxConcurrentRequests = getProperty(propertyPrefix, key, "fallback.isolation.semaphore.maxConcurrentRequests", builder.getFallbackIsolationSemaphoreMaxConcurrentRequests(), default_fallbackIsolationSemaphoreMaxConcurrentRequests);
this.fallbackEnabled = getProperty(propertyPrefix, key, "fallback.enabled", builder.getFallbackEnabled(), default_fallbackEnabled);
this.metricsRollingStatisticalWindowInMilliseconds = getProperty(propertyPrefix, key, "metrics.rollingStats.timeInMilliseconds", builder.getMetricsRollingStatisticalWindowInMilliseconds(), default_metricsRollingStatisticalWindow);
this.metricsRollingStatisticalWindowBuckets = getProperty(propertyPrefix, key, "metrics.rollingStats.numBuckets", builder.getMetricsRollingStatisticalWindowBuckets(), default_metricsRollingStatisticalWindowBuckets);
this.metricsRollingPercentileEnabled = getProperty(propertyPrefix, key, "metrics.rollingPercentile.enabled", builder.getMetricsRollingPercentileEnabled(), default_metricsRollingPercentileEnabled);
this.metricsRollingPercentileWindowInMilliseconds = getProperty(propertyPrefix, key, "metrics.rollingPercentile.timeInMilliseconds", builder.getMetricsRollingPercentileWindowInMilliseconds(), default_metricsRollingPercentileWindow);
this.metricsRollingPercentileWindowBuckets = getProperty(propertyPrefix, key, "metrics.rollingPercentile.numBuckets", builder.getMetricsRollingPercentileWindowBuckets(), default_metricsRollingPercentileWindowBuckets);
this.metricsRollingPercentileBucketSize = getProperty(propertyPrefix, key, "metrics.rollingPercentile.bucketSize", builder.getMetricsRollingPercentileBucketSize(), default_metricsRollingPercentileBucketSize);
this.metricsHealthSnapshotIntervalInMilliseconds = getProperty(propertyPrefix, key, "metrics.healthSnapshot.intervalInMilliseconds", builder.getMetricsHealthSnapshotIntervalInMilliseconds(), default_metricsHealthSnapshotIntervalInMilliseconds);
this.requestCacheEnabled = getProperty(propertyPrefix, key, "requestCache.enabled", builder.getRequestCacheEnabled(), default_requestCacheEnabled);
this.requestLogEnabled = getProperty(propertyPrefix, key, "requestLog.enabled", builder.getRequestLogEnabled(), default_requestLogEnabled);
this.executionIsolationThreadPoolKeyOverride = HystrixPropertiesChainedProperty.forString().add(propertyPrefix + ".command." + key.name() + ".threadPoolKeyOverride", (Object)null).build();
}
public HystrixProperty<Boolean> circuitBreakerEnabled() {
return this.circuitBreakerEnabled;
}
public HystrixProperty<Integer> circuitBreakerErrorThresholdPercentage() {
return this.circuitBreakerErrorThresholdPercentage;
}
public HystrixProperty<Boolean> circuitBreakerForceClosed() {
return this.circuitBreakerForceClosed;
}
public HystrixProperty<Boolean> circuitBreakerForceOpen() {
return this.circuitBreakerForceOpen;
}
public HystrixProperty<Integer> circuitBreakerRequestVolumeThreshold() {
return this.circuitBreakerRequestVolumeThreshold;
}
public HystrixProperty<Integer> circuitBreakerSleepWindowInMilliseconds() {
return this.circuitBreakerSleepWindowInMilliseconds;
}
public HystrixProperty<Integer> executionIsolationSemaphoreMaxConcurrentRequests() {
return this.executionIsolationSemaphoreMaxConcurrentRequests;
}
public HystrixProperty<HystrixCommandProperties.ExecutionIsolationStrategy> executionIsolationStrategy() {
return this.executionIsolationStrategy;
}
public HystrixProperty<Boolean> executionIsolationThreadInterruptOnTimeout() {
return this.executionIsolationThreadInterruptOnTimeout;
}
public HystrixProperty<Boolean> executionIsolationThreadInterruptOnFutureCancel() {
return this.executionIsolationThreadInterruptOnFutureCancel;
}
public HystrixProperty<String> executionIsolationThreadPoolKeyOverride() {
return this.executionIsolationThreadPoolKeyOverride;
}
/** @deprecated */
@Deprecated
public HystrixProperty<Integer> executionIsolationThreadTimeoutInMilliseconds() {
return this.executionTimeoutInMilliseconds;
}
public HystrixProperty<Integer> executionTimeoutInMilliseconds() {
return this.executionIsolationThreadTimeoutInMilliseconds();
}
public HystrixProperty<Boolean> executionTimeoutEnabled() {
return this.executionTimeoutEnabled;
}
public HystrixProperty<Integer> fallbackIsolationSemaphoreMaxConcurrentRequests() {
return this.fallbackIsolationSemaphoreMaxConcurrentRequests;
}
public HystrixProperty<Boolean> fallbackEnabled() {
return this.fallbackEnabled;
}
public HystrixProperty<Integer> metricsHealthSnapshotIntervalInMilliseconds() {
return this.metricsHealthSnapshotIntervalInMilliseconds;
}
public HystrixProperty<Integer> metricsRollingPercentileBucketSize() {
return this.metricsRollingPercentileBucketSize;
}
public HystrixProperty<Boolean> metricsRollingPercentileEnabled() {
return this.metricsRollingPercentileEnabled;
}
/** @deprecated */
public HystrixProperty<Integer> metricsRollingPercentileWindow() {
return this.metricsRollingPercentileWindowInMilliseconds;
}
public HystrixProperty<Integer> metricsRollingPercentileWindowInMilliseconds() {
return this.metricsRollingPercentileWindowInMilliseconds;
}
public HystrixProperty<Integer> metricsRollingPercentileWindowBuckets() {
return this.metricsRollingPercentileWindowBuckets;
}
public HystrixProperty<Integer> metricsRollingStatisticalWindowInMilliseconds() {
return this.metricsRollingStatisticalWindowInMilliseconds;
}
public HystrixProperty<Integer> metricsRollingStatisticalWindowBuckets() {
return this.metricsRollingStatisticalWindowBuckets;
}
public HystrixProperty<Boolean> requestCacheEnabled() {
return this.requestCacheEnabled;
}
public HystrixProperty<Boolean> requestLogEnabled() {
return this.requestLogEnabled;
}
private static HystrixProperty<Boolean> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, Boolean builderOverrideValue, Boolean defaultValue) {
return HystrixPropertiesChainedProperty.forBoolean().add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue).add(propertyPrefix + ".command.default." + instanceProperty, defaultValue).build();
}
private static HystrixProperty<Integer> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, Integer builderOverrideValue, Integer defaultValue) {
return HystrixPropertiesChainedProperty.forInteger().add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue).add(propertyPrefix + ".command.default." + instanceProperty, defaultValue).build();
}
private static HystrixProperty<String> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, String builderOverrideValue, String defaultValue) {
return HystrixPropertiesChainedProperty.forString().add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue).add(propertyPrefix + ".command.default." + instanceProperty, defaultValue).build();
}
private static HystrixProperty<HystrixCommandProperties.ExecutionIsolationStrategy> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, HystrixCommandProperties.ExecutionIsolationStrategy builderOverrideValue, HystrixCommandProperties.ExecutionIsolationStrategy defaultValue) {
return new HystrixCommandProperties.ExecutionIsolationStrategyHystrixProperty(builderOverrideValue, key, propertyPrefix, defaultValue, instanceProperty);
}
public static HystrixCommandProperties.Setter Setter() {
return new HystrixCommandProperties.Setter();
}
public static HystrixCommandProperties.Setter defaultSetter() {
return Setter();
}
static {
default_executionIsolationStrategy = HystrixCommandProperties.ExecutionIsolationStrategy.THREAD;
default_executionIsolationThreadInterruptOnTimeout = true;
default_executionIsolationThreadInterruptOnFutureCancel = false;
default_metricsRollingPercentileEnabled = true;
default_requestCacheEnabled = true;
default_fallbackIsolationSemaphoreMaxConcurrentRequests = 10;
default_fallbackEnabled = true;
default_executionIsolationSemaphoreMaxConcurrentRequests = 10;
default_requestLogEnabled = true;
default_circuitBreakerEnabled = true;
default_metricsRollingPercentileWindow = 60000;
default_metricsRollingPercentileWindowBuckets = 6;
default_metricsRollingPercentileBucketSize = 100;
default_metricsHealthSnapshotIntervalInMilliseconds = 500;
}
public static class Setter {
private Boolean circuitBreakerEnabled = null;
private Integer circuitBreakerErrorThresholdPercentage = null;
private Boolean circuitBreakerForceClosed = null;
private Boolean circuitBreakerForceOpen = null;
private Integer circuitBreakerRequestVolumeThreshold = null;
private Integer circuitBreakerSleepWindowInMilliseconds = null;
private Integer executionIsolationSemaphoreMaxConcurrentRequests = null;
private HystrixCommandProperties.ExecutionIsolationStrategy executionIsolationStrategy = null;
private Boolean executionIsolationThreadInterruptOnTimeout = null;
private Boolean executionIsolationThreadInterruptOnFutureCancel = null;
private Integer executionTimeoutInMilliseconds = null;
private Boolean executionTimeoutEnabled = null;
private Integer fallbackIsolationSemaphoreMaxConcurrentRequests = null;
private Boolean fallbackEnabled = null;
private Integer metricsHealthSnapshotIntervalInMilliseconds = null;
private Integer metricsRollingPercentileBucketSize = null;
private Boolean metricsRollingPercentileEnabled = null;
private Integer metricsRollingPercentileWindowInMilliseconds = null;
private Integer metricsRollingPercentileWindowBuckets = null;
private Integer metricsRollingStatisticalWindowInMilliseconds = null;
private Integer metricsRollingStatisticalWindowBuckets = null;
private Boolean requestCacheEnabled = null;
private Boolean requestLogEnabled = null;
Setter() {
}
public Boolean getCircuitBreakerEnabled() {
return this.circuitBreakerEnabled;
}
public Integer getCircuitBreakerErrorThresholdPercentage() {
return this.circuitBreakerErrorThresholdPercentage;
}
public Boolean getCircuitBreakerForceClosed() {
return this.circuitBreakerForceClosed;
}
public Boolean getCircuitBreakerForceOpen() {
return this.circuitBreakerForceOpen;
}
public Integer getCircuitBreakerRequestVolumeThreshold() {
return this.circuitBreakerRequestVolumeThreshold;
}
public Integer getCircuitBreakerSleepWindowInMilliseconds() {
return this.circuitBreakerSleepWindowInMilliseconds;
}
public Integer getExecutionIsolationSemaphoreMaxConcurrentRequests() {
return this.executionIsolationSemaphoreMaxConcurrentRequests;
}
public HystrixCommandProperties.ExecutionIsolationStrategy getExecutionIsolationStrategy() {
return this.executionIsolationStrategy;
}
public Boolean getExecutionIsolationThreadInterruptOnTimeout() {
return this.executionIsolationThreadInterruptOnTimeout;
}
public Boolean getExecutionIsolationThreadInterruptOnFutureCancel() {
return this.executionIsolationThreadInterruptOnFutureCancel;
}
/** @deprecated */
@Deprecated
public Integer getExecutionIsolationThreadTimeoutInMilliseconds() {
return this.executionTimeoutInMilliseconds;
}
public Integer getExecutionTimeoutInMilliseconds() {
return this.executionTimeoutInMilliseconds;
}
public Boolean getExecutionTimeoutEnabled() {
return this.executionTimeoutEnabled;
}
public Integer getFallbackIsolationSemaphoreMaxConcurrentRequests() {
return this.fallbackIsolationSemaphoreMaxConcurrentRequests;
}
public Boolean getFallbackEnabled() {
return this.fallbackEnabled;
}
public Integer getMetricsHealthSnapshotIntervalInMilliseconds() {
return this.metricsHealthSnapshotIntervalInMilliseconds;
}
public Integer getMetricsRollingPercentileBucketSize() {
return this.metricsRollingPercentileBucketSize;
}
public Boolean getMetricsRollingPercentileEnabled() {
return this.metricsRollingPercentileEnabled;
}
public Integer getMetricsRollingPercentileWindowInMilliseconds() {
return this.metricsRollingPercentileWindowInMilliseconds;
}
public Integer getMetricsRollingPercentileWindowBuckets() {
return this.metricsRollingPercentileWindowBuckets;
}
public Integer getMetricsRollingStatisticalWindowInMilliseconds() {
return this.metricsRollingStatisticalWindowInMilliseconds;
}
public Integer getMetricsRollingStatisticalWindowBuckets() {
return this.metricsRollingStatisticalWindowBuckets;
}
public Boolean getRequestCacheEnabled() {
return this.requestCacheEnabled;
}
public Boolean getRequestLogEnabled() {
return this.requestLogEnabled;
}
public HystrixCommandProperties.Setter withCircuitBreakerEnabled(boolean value) {
this.circuitBreakerEnabled = value;
return this;
}
public HystrixCommandProperties.Setter withCircuitBreakerErrorThresholdPercentage(int value) {
this.circuitBreakerErrorThresholdPercentage = value;
return this;
}
public HystrixCommandProperties.Setter withCircuitBreakerForceClosed(boolean value) {
this.circuitBreakerForceClosed = value;
return this;
}
public HystrixCommandProperties.Setter withCircuitBreakerForceOpen(boolean value) {
this.circuitBreakerForceOpen = value;
return this;
}
public HystrixCommandProperties.Setter withCircuitBreakerRequestVolumeThreshold(int value) {
this.circuitBreakerRequestVolumeThreshold = value;
return this;
}
public HystrixCommandProperties.Setter withCircuitBreakerSleepWindowInMilliseconds(int value) {
this.circuitBreakerSleepWindowInMilliseconds = value;
return this;
}
public HystrixCommandProperties.Setter withExecutionIsolationSemaphoreMaxConcurrentRequests(int value) {
this.executionIsolationSemaphoreMaxConcurrentRequests = value;
return this;
}
public HystrixCommandProperties.Setter withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy value) {
this.executionIsolationStrategy = value;
return this;
}
public HystrixCommandProperties.Setter withExecutionIsolationThreadInterruptOnTimeout(boolean value) {
this.executionIsolationThreadInterruptOnTimeout = value;
return this;
}
public HystrixCommandProperties.Setter withExecutionIsolationThreadInterruptOnFutureCancel(boolean value) {
this.executionIsolationThreadInterruptOnFutureCancel = value;
return this;
}
/** @deprecated */
@Deprecated
public HystrixCommandProperties.Setter withExecutionIsolationThreadTimeoutInMilliseconds(int value) {
this.executionTimeoutInMilliseconds = value;
return this;
}
public HystrixCommandProperties.Setter withExecutionTimeoutInMilliseconds(int value) {
this.executionTimeoutInMilliseconds = value;
return this;
}
public HystrixCommandProperties.Setter withExecutionTimeoutEnabled(boolean value) {
this.executionTimeoutEnabled = value;
return this;
}
public HystrixCommandProperties.Setter withFallbackIsolationSemaphoreMaxConcurrentRequests(int value) {
this.fallbackIsolationSemaphoreMaxConcurrentRequests = value;
return this;
}
public HystrixCommandProperties.Setter withFallbackEnabled(boolean value) {
this.fallbackEnabled = value;
return this;
}
public HystrixCommandProperties.Setter withMetricsHealthSnapshotIntervalInMilliseconds(int value) {
this.metricsHealthSnapshotIntervalInMilliseconds = value;
return this;
}
public HystrixCommandProperties.Setter withMetricsRollingPercentileBucketSize(int value) {
this.metricsRollingPercentileBucketSize = value;
return this;
}
public HystrixCommandProperties.Setter withMetricsRollingPercentileEnabled(boolean value) {
this.metricsRollingPercentileEnabled = value;
return this;
}
public HystrixCommandProperties.Setter withMetricsRollingPercentileWindowInMilliseconds(int value) {
this.metricsRollingPercentileWindowInMilliseconds = value;
return this;
}
public HystrixCommandProperties.Setter withMetricsRollingPercentileWindowBuckets(int value) {
this.metricsRollingPercentileWindowBuckets = value;
return this;
}
public HystrixCommandProperties.Setter withMetricsRollingStatisticalWindowInMilliseconds(int value) {
this.metricsRollingStatisticalWindowInMilliseconds = value;
return this;
}
public HystrixCommandProperties.Setter withMetricsRollingStatisticalWindowBuckets(int value) {
this.metricsRollingStatisticalWindowBuckets = value;
return this;
}
public HystrixCommandProperties.Setter withRequestCacheEnabled(boolean value) {
this.requestCacheEnabled = value;
return this;
}
public HystrixCommandProperties.Setter withRequestLogEnabled(boolean value) {
this.requestLogEnabled = value;
return this;
}
}
private static final class ExecutionIsolationStrategyHystrixProperty implements HystrixProperty<HystrixCommandProperties.ExecutionIsolationStrategy> {
private final HystrixDynamicProperty<String> property;
private volatile HystrixCommandProperties.ExecutionIsolationStrategy value;
private final HystrixCommandProperties.ExecutionIsolationStrategy defaultValue;
private ExecutionIsolationStrategyHystrixProperty(HystrixCommandProperties.ExecutionIsolationStrategy builderOverrideValue, HystrixCommandKey key, String propertyPrefix, HystrixCommandProperties.ExecutionIsolationStrategy defaultValue, String instanceProperty) {
this.defaultValue = defaultValue;
String overrideValue = null;
if (builderOverrideValue != null) {
overrideValue = builderOverrideValue.name();
}
this.property = HystrixPropertiesChainedProperty.forString().add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, overrideValue).add(propertyPrefix + ".command.default." + instanceProperty, defaultValue.name()).build();
this.parseProperty();
this.property.addCallback(new Runnable() {
public void run() {
ExecutionIsolationStrategyHystrixProperty.this.parseProperty();
}
});
}
public HystrixCommandProperties.ExecutionIsolationStrategy get() {
return this.value;
}
private void parseProperty() {
try {
this.value = HystrixCommandProperties.ExecutionIsolationStrategy.valueOf((String)this.property.get());
} catch (Exception var2) {
HystrixCommandProperties.logger.error("Unable to derive ExecutionIsolationStrategy from property value: " + (String)this.property.get(), var2);
this.value = this.defaultValue;
}
}
}
public static enum ExecutionIsolationStrategy {
THREAD,
SEMAPHORE;
private ExecutionIsolationStrategy() {
}
}
}
测试
1.一次正确一次错误访问
2.在窗口时间内多次错误访问,然后再正确访问
可以看到在时间窗口内达到一定的错误访问率的阈值后,即使访问正确也是执行的fallback 方法,当正确的服务访问调用进行一段时间后,可以发现正确的服务又可以正常。
综上,服务熔断的过程可以简单的描述为: 服务熔断 --> 服务降级 --> 恢复服务调用链路
总结
1.官网中对熔断机制的描述:
The precise way that the circuit opening and closing occurs is as follows:
Assuming the volume across a circuit meets a certain threshold (HystrixCommandProperties.circuitBreakerRequestVolumeThreshold())...
And assuming that the error percentage exceeds the threshold error percentage (HystrixCommandProperties.circuitBreakerErrorThresholdPercentage())...
Then the circuit-breaker transitions from CLOSED to OPEN.
While it is open, it short-circuits all requests made against that circuit-breaker.
After some amount of time (HystrixCommandProperties.circuitBreakerSleepWindowInMilliseconds()), the next single request is let through (this is the HALF-OPEN state). If the request fails, the circuit-breaker returns to the OPEN state for the duration of the sleep window. If the request succeeds, the circuit-breaker transitions to CLOSED and the logic in 1. takes over again.
熔断机制发生的具体方式可以描述如下:
1.假设整个服务的请求次数满足某个阈值 ( HystrixCommandProperties.circuitBreakerRequestVolumeThreshold())…
2.并假设错误百分比超过阈值错误百分比(HystrixCommandProperties.circuitBreakerErrorThresholdPercentage())…
3.然后断路器从 转变CLOSED为OPEN,触发熔断机制。
4.当它打开时,它会将针对该断路器的所有请求短路。
5.经过一段时间后,(HystrixCommandProperties.circuitBreakerSleepWindowInMilliseconds()),下一个请求被允许通过(这是HALF-OPEN状态)。如果请求失败,断路器将OPEN在睡眠窗口期间返回状态。如果请求成功,断路器转换到1. CLOSED中的逻辑再次接管
也就是说熔断机制的状态可以分为三种类型:
熔断打开(OPEN):请求不再进行调用当前服务,内部设置时钟一般为MTTR(平均故障处理时间),当打开时长达到所设时钟则进入半熔断状态
熔断关闭(CLOSE):熔断关闭不会对服务进行熔断。
熔断半开(HALF-OPE ):部分请求根据规则调用当前服务,如果请求成功且符合规则则认为当前服务恢复正常,关闭熔断。
下图显示了 HystrixCommand 和 HystrixObservableCommand与 HystrixCircuitBreaker 的交互方式,HystrixCircuitBreaker及其逻辑和决策流程,包括计数器在断路器中的行为方式
问题:
- 断路器在什么情况下才起作用
涉及到断路器的三个重要参数:快照时间窗、请求总数阀值、错误百分比阀值。
1:快照时间窗:断路器确定是否打开需要统计一些请求和错误数据,而统计的时间范围就是快照时间窗,默认为最近的10秒。
2:请求总数阀值:在快照时间窗内,必须满足请求总数阀值才有资格熔断。默认为20,意味着在10秒内,如果该hystrix命令的调用次数不足20次,即使所有的请求都超时或其他原因失败,断路器都不会打开。
3:错误百分比阀值:当请求总数在快照时间窗内超过了阀值,比如发生了30次调用,如果在这30次调用中,有15次发生了超时异常,也就是超过50%的错误百分比,在默认设定50%阀值情况下,这时候就会将断路器打开。
- 断路器开启和关闭的条件
1.当满足一定的阀值的时候(默认10秒内超过20个请求次数)
2.当失败率达到一定的时候(默认10秒内超过50%的请求失败)
3.到达以上阀值,断路器将会开启
4.当开启的时候,所有请求都不会进行转发
5.一段时间之后(默认是5秒),这个时候断路器是半开状态,会让其中一个请求进行转发。如果成功,断路器会关闭,若失败,继续开启。重复4和5
- 断路器打开之后
1:再有请求调用的时候,将不会调用主逻辑,而是直接调用降级fallback。通过断路器,实现了自动地发现错误并将降级逻辑切换为主逻辑,减少响应延迟的效果。
2:原来的主逻辑要如何恢复呢? 对于这一问题,hystrix也为我们实现了自动恢复功能。
当断路器打开,对主逻辑进行熔断之后,hystrix会启动一个休眠时间窗,在这个时间窗内,降级逻辑是临时的成为主逻辑,当休眠时间窗到期,断路器将进入半开状态,释放一次请求到原来的主逻辑上,如果此次请求正常返回,那么断路器将继续闭合,主逻辑恢复,如果这次请求依然有问题,断路器继续进入打开状态,休眠时间窗重新计时
HystrixCommand 所有配置
//========================All
@HystrixCommand(fallbackMethod = "str_fallbackMethod",
groupKey = "strGroupCommand",
commandKey = "strCommand",
threadPoolKey = "strThreadPool",
commandProperties = {
// 设置隔离策略,THREAD 表示线程池 SEMAPHORE:信号池隔离
@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
// 当隔离策略选择信号池隔离的时候,用来设置信号池的大小(最大并发数)
@HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "10"),
// 配置命令执行的超时时间
@HystrixProperty(name = "execution.isolation.thread.timeoutinMilliseconds", value = "10"),
// 是否启用超时时间
@HystrixProperty(name = "execution.timeout.enabled", value = "true"),
// 执行超时的时候是否中断
@HystrixProperty(name = "execution.isolation.thread.interruptOnTimeout", value = "true"),
// 执行被取消的时候是否中断
@HystrixProperty(name = "execution.isolation.thread.interruptOnCancel", value = "true"),
// 允许回调方法执行的最大并发数
@HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "10"),
// 服务降级是否启用,是否执行回调函数
@HystrixProperty(name = "fallback.enabled", value = "true"),
// 是否启用断路器
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
// 该属性用来设置在滚动时间窗中,断路器熔断的最小请求数。例如,默认该值为 20 的时候,
// 如果滚动时间窗(默认10秒)内仅收到了19个请求, 即使这19个请求都失败了,断路器也不会打开。
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"),
// 该属性用来设置在滚动时间窗中,表示在滚动时间窗中,在请求数量超过
// circuitBreaker.requestVolumeThreshold 的情况下,如果错误请求数的百分比超过50,
// 就把断路器设置为 "打开" 状态,否则就设置为 "关闭" 状态。
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
// 该属性用来设置当断路器打开之后的休眠时间窗。 休眠时间窗结束之后,
// 会将断路器置为 "半开" 状态,尝试熔断的请求命令,如果依然失败就将断路器继续设置为 "打开" 状态,
// 如果成功就设置为 "关闭" 状态。
@HystrixProperty(name = "circuitBreaker.sleepWindowinMilliseconds", value = "5000"),
// 断路器强制打开
@HystrixProperty(name = "circuitBreaker.forceOpen", value = "false"),
// 断路器强制关闭
@HystrixProperty(name = "circuitBreaker.forceClosed", value = "false"),
// 滚动时间窗设置,该时间用于断路器判断健康度时需要收集信息的持续时间
@HystrixProperty(name = "metrics.rollingStats.timeinMilliseconds", value = "10000"),
// 该属性用来设置滚动时间窗统计指标信息时划分"桶"的数量,断路器在收集指标信息的时候会根据
// 设置的时间窗长度拆分成多个 "桶" 来累计各度量值,每个"桶"记录了一段时间内的采集指标。
// 比如 10 秒内拆分成 10 个"桶"收集这样,所以 timeinMilliseconds 必须能被 numBuckets 整除。否则会抛异常
@HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "10"),
// 该属性用来设置对命令执行的延迟是否使用百分位数来跟踪和计算。如果设置为 false, 那么所有的概要统计都将返回 -1。
@HystrixProperty(name = "metrics.rollingPercentile.enabled", value = "false"),
// 该属性用来设置百分位统计的滚动窗口的持续时间,单位为毫秒。
@HystrixProperty(name = "metrics.rollingPercentile.timeInMilliseconds", value = "60000"),
// 该属性用来设置百分位统计滚动窗口中使用 “ 桶 ”的数量。
@HystrixProperty(name = "metrics.rollingPercentile.numBuckets", value = "60000"),
// 该属性用来设置在执行过程中每个 “桶” 中保留的最大执行次数。如果在滚动时间窗内发生超过该设定值的执行次数,
// 就从最初的位置开始重写。例如,将该值设置为100, 滚动窗口为10秒,若在10秒内一个 “桶 ”中发生了500次执行,
// 那么该 “桶” 中只保留 最后的100次执行的统计。另外,增加该值的大小将会增加内存量的消耗,并增加排序百分位数所需的计算时间。
@HystrixProperty(name = "metrics.rollingPercentile.bucketSize", value = "100"),
// 该属性用来设置采集影响断路器状态的健康快照(请求的成功、 错误百分比)的间隔等待时间。
@HystrixProperty(name = "metrics.healthSnapshot.intervalinMilliseconds", value = "500"),
// 是否开启请求缓存
@HystrixProperty(name = "requestCache.enabled", value = "true"),
// HystrixCommand的执行和事件是否打印日志到 HystrixRequestLog 中
@HystrixProperty(name = "requestLog.enabled", value = "true"),
},
threadPoolProperties = {
// 该参数用来设置执行命令线程池的核心线程数,该值也就是命令执行的最大并发量
@HystrixProperty(name = "coreSize", value = "10"),
// 该参数用来设置线程池的最大队列大小。当设置为 -1 时,线程池将使用 SynchronousQueue 实现的队列,
// 否则将使用 LinkedBlockingQueue 实现的队列。
@HystrixProperty(name = "maxQueueSize", value = "-1"),
// 该参数用来为队列设置拒绝阈值。 通过该参数, 即使队列没有达到最大值也能拒绝请求。
// 该参数主要是对 LinkedBlockingQueue 队列的补充,因为 LinkedBlockingQueue
// 队列不能动态修改它的对象大小,而通过该属性就可以调整拒绝请求的队列大小了。
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "5"),
}
)
public String strConsumer() {
return "hello 2020";
}
public String str_fallbackMethod()
{
return "*****fall back str_fallbackMethod";
}
Hystrix 工作流程:
步 骤 | 说明 |
---|---|
1 | 创建 HystrixCommand(用在依赖的服务返回单个操作结果的时候) 或 HystrixObserableCommand(用在依赖的服务返回多个操作结果的时候) 对象。 |
2 | 命令执行。其中 HystrixComand 实现了下面前两种执行方式;而 HystrixObservableCommand 实现了后两种执行方式:execute():同步执行,从依赖的服务返回一个单一的结果对象, 或是在发生错误的时候抛出异常。queue():异步执行, 直接返回 一个Future对象, 其中包含了服务执行结束时要返回的单一结果对象。observe():返回 Observable 对象,它代表了操作的多个结果,它是一个 Hot Obserable(不论 “事件源” 是否有 “订阅者”,都会在创建后对事件进行发布,所以对于 Hot Observable 的每一个 “订阅者” 都有可能是从 “事件源” 的中途开始的,并可能只是看到了整个操作的局部过程)。toObservable(): 同样会返回 Observable 对象,也代表了操作的多个结果,但它返回的是一个Cold Observable(没有 “订阅者” 的时候并不会发布事件,而是进行等待,直到有 “订阅者” 之后才发布事件,所以对于 Cold Observable 的订阅者,它可以保证从一开始看到整个操作的全部过程)。 |
3 | 若当前命令的请求缓存功能是被启用的, 并且该命令缓存命中, 那么缓存的结果会立即以 Observable 对象的形式 返回。 |
4 | 检查断路器是否为打开状态。如果断路器是打开的,那么Hystrix不会执行命令,而是转接到 fallback 处理逻辑(第 8 步);如果断路器是关闭的,检查是否有可用资源来执行命令(第 5 步) |
5 | 线程池/请求队列/信号量是否占满。如果命令依赖服务的专有线程池和请求队列,或者信号量(不使用线程池的时候)已经被占满, 那么 Hystrix 也不会执行命令, 而是转接到 fallback 处理逻辑(第8步) |
6 | Hystrix 会根据我们编写的方法来决定采取什么样的方式去请求依赖服务。HystrixCommand.run() :返回一个单一的结果,或者抛出异常。HystrixObservableCommand.construct(): 返回一个Observable 对象来发射多个结果,或通过 onError 发送错误通知。 |
7 | Hystrix会将 “成功”、“失败”、“拒绝”、“超时” 等信息报告给断路器, 而断路器会维护一组计数器来统计这些数据。断路器会使用这些统计数据来决定是否要将断路器打开,来对某个依赖服务的请求进行 “熔断/短路”。 |
8 | 当命令执行失败的时候, Hystrix 会进入 fallback 尝试回退处理, 我们通常也称该操作为 “服务降级”。而能够引起服务降级处理的情况有下面几种:第4步: 当前命令处于"熔断/短路"状态,断路器是打开的时候。第5步: 当前命令的线程池、 请求队列或 者信号量被占满的时候。第6步:HystrixObservableCommand.construct() 或 HystrixCommand.run() 抛出异常的时候。 |
9 | 当Hystrix命令执行成功之后, 它会将处理结果直接返回或是以Observable 的形式返回。 |
tips:如果我们没有为命令实现降级逻辑或者在降级处理逻辑中抛出了异常, Hystrix 依然会返回一个 Observable 对象,但是它不会发射任何结果数据, 而是通过 onError方法通知命令立即中断请求,并通过onError()方法将引起命令失败的异常发送给调用者。
五、服务监控
Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面
Hystrix-dashboard 使用
1.建module com-consumer-hystrix-dashboard9001
2.写pom.xml
<?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>cloudlearn</artifactId>
<groupId>com.avgrado.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>com-consumer-hystrix-dashboard9001</artifactId>
<dependencies>
<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>
3.新建application.yml
server:
port: 9001
4.主启动类(加上**@EnableHystrixDashboard** 注解)
package com.avgrado.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
/**
* @ClassName HystrixDashboardApplication9001
* @Description TODO
* @Author gongchen
* @Date 2022-02-23 17:34
*/
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication9001.class,args);
}
}
5.启动 com-consumer-hystrix-dashboard9001 监控服务,访问 http://localhost:9001/hystrix
6.修改cloud-provider-hystix-payment8001 ,在 PaymentHystrixApplication8001 中增加服务监控配置
/**
*此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
*ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
*只要在自己的项目里配置上下面的servlet就可以了
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
7.启动Eureka 集群和 cloud-provider-hystix-payment8001 服务
在 dashboard监控页面输入要监控的地址 http://localhost:8001/hystrix.stream
进入后的监控界面如下图
8.测试
先访问正确地址再访问错误地址,然后再访问正确地址,可以发现断路器从关闭到打开再到关闭的一个过程(服务熔断的过程)
9.如何看这个监控图
可以用 7色 1圈 1线 来概括
7色:每一种监控的状态对应着一种颜色
1圈: 实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,它的健康度从绿色<黄色<橙色<红色递减。
该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。1线:曲线:用来记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势。