1. 降低配置
@HystrixCommand
8001先從自身找問題:
設定自身調用逾時時間的峰值,峰值内可以正常運作,超過了需要有兜底的方法處理,作服務降級fallback
1.1 8001fallback
@HystrixCommand報異常後如何處理
一旦調用服務方法失敗并抛出了錯誤資訊後,會自動調用@HystrixCommand标注好的fallbackMethod調用類中的指定方法

上圖故意制造異常
接收3秒鐘 ,運作5秒鐘,逾時異常
目前服務不可用了(逾時異常,運作異常),做服務降級,預設的兜底方案都是paymentInfo_TimeOutHandler
主啟動類激活
添加新注解@EnableCircuitBreaker
測試
http://localhost:8001/payment/hystrix/timeout/1
1.2 80fallback
80訂單微服務,也可以更好的保護自己,自己也依樣畫葫蘆進行用戶端降級保護。
- yml
feign:
hystrix:
enabled: true #如果處理自身的容錯就開啟。開啟方式與生産端不一樣。
- 主啟動
Spring Cloud(十):Hystrix服務降級 - 業務類
- OrderHystrixController
package com.lele.springcloud.controller;
import com.lele.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* @author: lele
* @date: 2021/3/20 17:45
* @description:
*/
@RestController
@Slf4j
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(fallbackMethod = "paymentInfoFallbackMethod", commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="1500")
})
public String paymentInfo_TimeOut(Integer id) {
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
public String paymentInfoFallbackMethod(Integer id) {
return "我是消費者80,對方支付系統繁忙請10秒鐘後再試或者自己運作出錯,請檢查自己,(╥╯^╰╥)";
}
}
目前問題
每個業務方法對應一個兜底的方法,代碼膨脹
統一和自定義的分開
解決問題
除了個别的核心業務有專屬,其他普通的都可以通過@DefaultProperties(defaultFallback = “”)統一跳轉到統一處理界面。
- controller
package com.lele.springcloud.controller;
import com.lele.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 lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* @author: lele
* @date: 2021/3/20 17:45
* @description:
*/
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
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(fallbackMethod = "paymentInfoFallbackMethod", commandProperties = {
// @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="1500")
// })
@HystrixCommand
public String paymentInfo_TimeOut(Integer id) {
int age = 10/0;
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
public String paymentInfoFallbackMethod(Integer id) {
return "我是消費者80,對方支付系統繁忙請10秒鐘後再試或者自己運作出錯,請檢查自己,(╥╯^╰╥)";
}
// 下面是全局 fallback 方法
public String payment_Global_FallbackMethod() {
return "Global異常處理資訊,請稍候再試~~";
}
}
服務降級,用戶端去調用服務端,碰上服務端當機或關閉.
本次案例服務降級處理是在用戶端80實作完成的,與服務端8001沒有關系,隻需要為Feign用戶端定義的接口添加一個服務降級處理的實作類即可實作解耦。
未來我們要面對的異常:
- 運作
- 逾時
- 當機
修改cloud-consumer-feign-hystrix-order80
根據cloud-consumer-feign-hystrix-order80已經有的PaymentHystrixService接口,重新建立一個類(PaymentFallbackService)實作該接口,統一為接口裡面的方法進行異常處理
- PaymentFallbackService 類實作PaymentHystrixService接口
package com.lele.springcloud.service;
import org.springframework.stereotype.Component;
/**
* @author: lele
* @date: 2021/3/21 8:44
* @description:
*/
@Component
public class PaymentFallbackService implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "------PaymentFallbackService fall back-paymentInfo_OK------";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
return "------PaymentFallbackService fall back-paymentInfo_TimeOut------";
}
}
- PaymentHystrixService
Spring Cloud(十):Hystrix服務降級
測試
單個eureka先啟動7001
PaymentHystrixMain8001啟動
正常通路測試
故意關閉微服務8001
用戶端自己調用提示
此時服務端provider已經down了,但是我們做了服務降級處理,讓用戶端在服務端不可用時也會獲得提示資訊而不會挂起耗死伺服器