1. @HysrixCommand()
使用此注解设置降级服务时:
- 降级服务返回值与原服务返回值保持一致
- 降级服务的参数与原服务参数保持一致
- fallbackMethod属性查找的是参数、返回值和原服务保持一致的降级方法(一般使用此属性设置降级方法)
- defaultFallback属性是查找返回值和原服务一致且参数为空的降级方法
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "payment_Global_FallbackMethod")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
String res = paymentHystrixService.paymentInfo_TimeOut(id);
return res;
}
public String payment_Global_FallbackMethod(@PathVariable("id") Integer id) {
return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
}
2. @DefaultProperties
- 该注解标注在controller类上面,定义默认的回退方法
- 原服务标注@HystrixCommand注解,但是不用再声明降级服务方法名
- 降级服务方法不应有参数
- 降级服务返回值与原服务返回值保持一致
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
String res = paymentHystrixService.paymentInfo_TimeOut(id);
return res;
}
public String payment_Global_FallbackMethod() {
return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
}