一、Hystrix是什么?
Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
Hystrix官网已经停更了。
二、Hystrix能做什么
Hystrix可以实现服务降级、服务熔断、服务限流,接近实时监控等
服务降级:当下游服务因某种原因响应过慢,下游服务主动停掉一些不太重要的业务,调用降级逻辑,释放出服务器资源,增加响应速度!
服务熔断:当下游服务因某种原因突然变得不可用或响应过慢,上游服务为保证自己整体服务的可用性,不再继续调用目标服务,直接返回,快速释放资源,如果目标服务情况好转则恢复调用。
涉及到断路器的三个重要参数:快照时间窗、请求总数阀值、错误百分比阀值。
1:快照时间窗:断路器确定是否打开需要统计一些请求和错误数据,而统计的时间范围就是快照时间窗,默认为最近的10秒。
2:请求总数阀值:在快照时间窗内,必须满足请求总数阀值才有资格熔断。默认为20,意味着在10秒内,如果该hystrix命令的调用次数不足20次,即使所有的请求都超时或其他原因失败,断路器都不会打开。
3:错误百分比阀值:当请求总数在快照时间窗内超过了阀值,比如发生了30次调用,如果在这30次调用中,有15次发生了超时异常,也就是超过50%的错涅百今比在默认设定50%阀值情况下这时候就会将断器打开。
熔断器开启后
1:再有请求调用的时候,将不会调用主逻辑,是直接调用降级fallback。通过断路器,实现了自动地发现错误并将降级逻辑切换为主逻辑,减少响应延迟的效果。
2:原来的主逻辑要如何恢复呢?
对于这个问题,hystrix也为我们实现了 自动恢复功能。
当断路器打开,对主逻辑进行熔断之后,hystrix会启动一个休眠时间窗,在这个时间窗内,降级逻辑是临时的成为主逻辑,当休眠时间窗到期,断路器将进入半开状态,释放一次请求到原来的主逻辑上,如果此次请求正常返回,那么断路器将继续闭合,主逻辑恢复,如果这次请求依然有问题,断路器继续进入打开状态,休眠时间窗重新计时。
服务限流:让下游服务器不会因为的承载过大而宕机,秒杀高并发等操作,严禁一窝蜂的过来拥挤,大家排队,一秒钟N个,有序进行。
三、Hystrix使用
3.1 Hystrix服务降级
环境:注册中心(Eureka),服务提供者,服务消费者(结合OpenFeign)
注意:在方法里面,不管是发生异常还是超时,都会触发降级
注册中心
<!-- 服务注册中心的服务端 eureka-server -->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 通信监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.在application.yml添加配置
注意 :hostname: eureka7001.com 是配置了本地映射,如果需要配置,看https://blog.csdn.net/weixin_46204877/article/details/126787304 这篇文章,如果不配置,那就将hostname的值改为localhost,要注意,这里改之后,后面的服务地址也要跟着变化。
server:
port: 7001
#单机版
eureka:
instance:
# hostname: localhost #eureka服务端的实例名字 这个是没有配置本地映射
hostname: eureka7001.com #eureka服务端的实例名字 配置了本地映射
client:
register-with-eureka: false #表示不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/
3.在主启动类添加注解 @EnableEurekaServer
4.启动,访问 http://localhost:7001/ 出现以下界面则成功。
服务提供者
<!--新增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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.在application.yml添加配置
server:
port: 8001
spring:
application:
name: cloud-provider-hystrix-payment
eureka:
client:
#是否注册
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka #单机 这个服务地址就是在注册中心配置的服务名称
3.在主启动类添加 @EnableEurekaServer注解
4.启动服务会在http://localhost:7001/看见对应的服务信息。
5.在访问提供者模块编写方法测试
6.PaymentService类,仔细看注解。
/**
* 测试Hystrix
*/
@Service
public class PaymentService {
//服务降级
/**
* 正常访问,肯定OK
*
* @param id
* @return
*/
public String paymentInfoOK(Integer id) {
return "线程池: " + Thread.currentThread().getName() + " paymentInfoOK,id: " + id + "\t"
+ "O(∩_∩)O哈哈~";
}
/**
* 超时访问,设置自身调用超时的峰值,峰值内正常运行,超过了峰值需要服务降级 自动调用fallbackMethod 指定的方法
* 超时异常或者运行异常 都会进行服务降级
*
* (name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")表示
请求该接口超过3秒没有返回就降级
*
* @param id
* @return
*/
@HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})
public String paymentInfoTimeOut(Integer id) {
// int age = 10/0;
int second = 5;
long start = System.currentTimeMillis();
try {
TimeUnit.SECONDS.sleep(second);
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end - start);
return "线程池: " + Thread.currentThread().getName() + " paymentInfoTimeOut,id: " + id + "\t"
+ "O(∩_∩)O哈哈~" + " 耗时(秒): " + second;
}
/**
* paymentInfoTimeOut 方法失败后 自动调用此方法 实现服务降级 告知调用者 paymentInfoTimeOut 目前无法正常调用
*
* @param id
* @return
*/
public String paymentInfoTimeOutHandler(Integer id) {
return "线程池: " + Thread.currentThread().getName() + " paymentInfoTimeOutHandler8001系统繁忙或者运行报错,请稍后再试,id: " + id + "\t"
+ "o(╥﹏╥)o";
}
}
7.PaymentController类
@RestController
@RequestMapping("payment")
@Slf4j
public class PaymentController {
/**
* 服务对象
*/
@Resource
private PaymentService paymentService;
/**
* 正常访问
*
* @param id
* @return
*/
@GetMapping("/hystrix/ok/{id}")
public String paymentInfoOK(@PathVariable("id") Integer id) {
String result = paymentService.paymentInfoOK(id);
log.info("result: " + result);
return result;
}
/**
* 超时或者异常
*
* @param id
* @return
*/
@GetMapping("/hystrix/timeout/{id}")
public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
String result = paymentService.paymentInfoTimeOut(id);
log.info("result: " + result);
return result;
}
}
8.访问http://localhost:8001/payment/hystrix/timeout/31 可以看到,我们设置了当服务超过3秒没有响应就降级,跳转对应降级的处理方法 paymentInfoTimeOutHandler() 在执行。
访问 http://localhost/consumer/payment/hystrix/ok/31就是正常的。
到这,服务提供者降级就完成了。下面就是服务提供者的降级配置例子。
服务提供者
服务提供者是采用了全局降级配置。以及使用了OpenFeign,在方法上面有对应的注释,仔细观看。
<!--新增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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.application.xml文件添加配置
server:
port: 80
eureka:
client:
#不注册
register-with-eureka: false
service-url:
#单机
defaultZone: http://eureka7001.com:7001/eureka/
feign:
hystrix:
enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样。
3.主启动类添加 @EnableFeignClients,@EnableHystrix 注解
@SpringBootApplication
@EnableFeignClients // 启动 feign
@EnableHystrix // 启动 hystrix
public class FeignHystrixOrder80Application {
public static void main(String[] args) {
SpringApplication.run(FeignHystrixOrder80Application.class, args);
}
}
4.OrderHystrixController类
@RestController
@RequestMapping("consumer")
@Slf4j
// hystrix 全局fallback(降级)方法,如果方法单独配置降级后,使用的就是所配置的方法
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
// 正常的方法调用,为了和降级做比较。
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfoOK(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfoOK(id);
return result;
}
//服务降级测试方法,和提供者的测试方法大同小异,不过这使用的是全局降级方法配置
@GetMapping("/payment/hystrix/timeout/{id}")
// @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = {
// @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
// })
@HystrixCommand //没有配置降级方法就用全局配置的降级方法
public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
int age = 10 / 0;
String result = paymentHystrixService.paymentInfoTimeOut(id);
return result;
}
/**
* 超时访问,设置自身调用超时的峰值,峰值内正常运行,超过了峰值需要服务降级 自动调用fallbackMethod 指定的方法
* 超时异常或者运行异常 都会进行服务降级
*
* @param id
* @return
*/
public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}
/**
* hystrix 全局fallback方法
* @return
*/
public String payment_Global_FallbackMethod() {
return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
}
}
5.PaymentHystrixService类
@Component //注册成Bean
// FeignClient 中Fallback熟悉 客户端的服务降级 针对 CLOUD-PROVIDER-HYSTRIX-PAYMENT 该服务 提供了一个 对应的服务降级类
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallbackServiceImpl.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
String paymentInfoOK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
String paymentInfoTimeOut(@PathVariable("id") Integer id);
}
6.PaymentFallbackServiceImpl类
@Component //注入bean
public class PaymentFallbackServiceImpl implements PaymentHystrixService {
@Override
public String paymentInfoOK(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_OK ,o(╥﹏╥)o";
}
@Override
public String paymentInfoTimeOut(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_TimeOut ,o(╥﹏╥)o";
}
}
7.启动消费者,服务http://localhost/consumer/payment/hystrix/timeout/31 页面打印信息则成功。
到这,服务提供者和服务消费者降级就完成了。
3.2 Hystrix服务熔断
仔细看注释。
1.回到服务提供者模块,在PaymentService类中添加方法。
2.PaymentService类
添加 一个熔断的方法和降级的方法,注意的是在方法体里面发生异常和超时,都会触发熔断。
/**
* 服务熔断 超时、异常、都会触发熔断
* 1、默认是最近10秒内收到不小于10个请求,<br/>
* 2、并且有60%是失败的<br/>
* 3、就开启断路器<br/>
* 4、开启后所有请求不再转发,降级逻辑自动切换为主逻辑,减小调用方的响应时间<br/>
* 5、经过一段时间(时间窗口期,默认是5秒),断路器变为半开状态,会让其中一个请求进行转发。<br/>
* 5.1、如果成功,断路器会关闭,<br/>
* 5.2、若失败,继续开启。重复4和5<br/>
*
* @param id
* @return
*/
@HystrixCommand(fallbackMethod = "paymentCircuitBreakerFallback",
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"),// 失败率达到多少后跳闸60%
// @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")// 超时处理
})
public String paymentCircuitBreaker(Integer id) {
if (id < 0) {
throw new RuntimeException("******id 不能负数");
}
//测试异常
// int age = 10 / 0;
// int second = 3;
// try {
// TimeUnit.SECONDS.sleep(second);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
String serialNumber = IdUtil.simpleUUID(); //等价于UUID.randomUUID().toString();
return Thread.currentThread().getName() + "\t" + "调用成功,流水号: " + serialNumber;
}
/**
* paymentCircuitBreaker 方法的 fallback(降级),<br/>
* 当断路器开启时,主逻辑熔断降级,该 fallback 方法就会替换原 paymentCircuitBreaker 方法,处理请求
* @param id
* @return
*/
public String paymentCircuitBreakerFallback(Integer id) {
return Thread.currentThread().getName() + "\t" + "id 不能负数或超时或自身错误,请稍后再试,/(ㄒoㄒ)/~~ id: " + id;
}
3.在主启动类添加配置解决Hystrix自身小Bug
//测试监控必须要有ServletRegistrationBean方法和actuator依赖
/**
* 注意:新版本Hystrix需要在主启动类中指定监控路径
* 此配置是为了服务监控而配置,与服务容错本身无关,spring cloud升级后的坑
* ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
* 只要在自己的项目里配置上下面的servlet就可以了
*
* @return ServletRegistrationBean
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
// 一启动就加载
registrationBean.setLoadOnStartup(1);
// 添加url
registrationBean.addUrlMappings("/hystrix.stream");
// 设置名称
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
4.PaymentController类
添加测试熔断的方法。
/**
* 服务熔断
*
* @param id
* @return
*/
@GetMapping("/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
String result = paymentService.paymentCircuitBreaker(id);
log.info("****result: " + result);
return result;
}
5.重启8001主启动类
演示熔断
访问 http://localhost:8001/payment/circuit/-31 因为方法里面穿的数要大于0,我们传的负数,会触发降级方法,因为设置熔断的触发条件是10次请求中失败要达到60%,所以我们需要反复的刷新访问 http://localhost:8001/payment/circuit/-31 达到触发熔断的条件,当你刷到一定次数时,你访问 http://localhost:8001/payment/circuit/31 会出现不能访问的现象,但是你等一会在刷新,又可以正常访问,说明熔断配置正确。
正常访问 http://localhost:8001/payment/circuit/31 ,会提示成功。
3.3 Hystrix熔断部分参数说明
@HystrixCommand(fallbackMethod = "str._fallbackMethod" ,
groupKey = "strGroupCommand" ,
commandKey = "strCommarld",
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.time inMilliseconds", value = "10000") ,
//该属性用来没置燎动时间窗统计指标信息时划分”桶"的数量,断路器在收集指标信息的时候会根据
//设置的时间窗长度拆分成多个"桶"来累计各度量值,每个”桶"记录了-段时间内的来集 指标。
//比如10秒内拆分成10个”桶”收集这样,所以timeinMilliseconds 必须能被numBuckets 整除。否则会抛异常
@HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "10"),
//滚动时间窗设置,该时间用于断路 器判断健康度时需要收集信息的持续时间
@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.rollingPercentihle.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"),
@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")
}
)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/77189.html