Hystrix是用于防止系统服务调用异常、超时等情况,做出的备选响应方式,避免局部服务异常导致整个系统无法运行
@HystrixCommand降级一般都是用在客户端(服务消费端),也可以用在服务端(服务提供端),消费端用法如下:
第一步:配置yml文件,启动hystrix如下:
feign:
hystrix:
enabled: true
第二步:项目主启动类添加注解@EnableHystrix
第三部:在Controller类中使用,示例如下:
@GetMapping("/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutFallbackMethod",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
})
public CommonResult paymentInfo_TimeOut(@PathVariable("id") Integer id){
//int num = 10/0;
return paymentHystrixService.paymentInfo_TimeOut(id);
}
public String paymentInfo_TimeOutFallbackMethod(@PathVariable("id") Integer id){
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行错误,请检查自己!";
}
网友评论