Hystrix 服務降級
阿新 • • 發佈:2022-05-17
服務提供方-服務降級
- 首先匯入 Hystrix Maven 依賴
<!--新增hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 在可能會超時或出現異常的方法上加
@HystrixCommand
註解,並添加回調方法
//超時降級演示 @HystrixCommand(fallbackMethod = "payment_TimeoutHandler", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") //5秒鐘以內就是正常的業務邏輯 }) @GetMapping("/hystrix/timeout/{id}") public String paymentInfo_Timeout(@PathVariable("id") Integer id) { String result = paymentService.payment_Timeout(id); log.info("*******result:" + result); return result; } // 方法名與 @HystrixCommand 宣告的方法名一致 public String payment_TimeoutHandler(Integer id) { return "執行緒池:" + Thread.currentThread().getName() + " payment_TimeoutHandler,系統繁忙,請稍後再試\t o(╥﹏╥)o "; }
- 在主啟動類上新增
@EnableCircuitBreaker
方法
服務消費方-服務降級
- 新增 Hystrix Maven 依賴
<!--新增hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 新增 yml 配置
feign: hystrix: enabled: true #如果處理自身的容錯就開啟。開啟方式與生產端不一樣。
- 新增
@HystrixCommand
註解配置,並新增
@HystrixCommand(fallbackMethod = "payment_TimeoutHandler", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500") //5秒鐘以內就是正常的業務邏輯 }) @GetMapping("/payment/hystrix/timeout/{id}") public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ String result = paymentHystrixService.paymentInfo_Timeout(id); log.info("*******result:"+result); return result; } public String payment_TimeoutHandler(Integer id) { return "我是消費者80,對方支付系統繁忙請10秒後再試。或自己執行出錯,請檢查自己。"; }
- 在主啟動類
@EnableHystrix
註解
服務降級-統一通用處理
每個業務方法對應一個兜底的方法,就會出現程式碼膨脹,程式碼耦合。
我們可以將統一通用處理和自定義獨立處理的分開。
在不需要特殊處理的業務類上新增統一的註解
// 在 controller 類上新增預設的屬性配置
@DefaultProperties(defaultFallback = "payment_global_fallback_method", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500") // 1.5秒鐘以內就是正常的業務邏輯
})
public class OrderHystrixController {
...
// 如果沒有新增相應的屬性就是用預設的配置
@HystrixCommand
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
int i = 10 / 0;
String result = paymentHystrixService.paymentInfo_OK(id);
log.info("*******result:"+result);
return result;
}
// 如果是添加了相應的配置,那麼就是用獨立的處理方法
@HystrixCommand(fallbackMethod = "payment_TimeoutHandler", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500") //5秒鐘以內就是正常的業務邏輯
})
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
String result = paymentHystrixService.paymentInfo_Timeout(id);
log.info("*******result:"+result);
return result;
}
public String payment_TimeoutHandler(Integer id) {
return "我是消費者80,對方支付系統繁忙請10秒後再試。或自己執行出錯,請檢查自己。";
}
public String payment_global_fallback_method(){
return "我是消費者80,對方支付系統繁忙請10秒後再試。這是通用的回撥函式。";
}