一、服務降級(服務提供者)
1.pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.啟動類添加@EnableCircuitBreaker注解
3.需要服務降級的方法上添加@HystrixCommand
@HystrixCommand(fallbackMethod = "paymentInfo_timeout_handler", commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "2000")
})
4.提供觸發服務降級條件或發生例外時的執行方法
public String paymentInfo_timeout_handler(Long id){
return "執行緒:" + Thread.currentThread().getName() + " 系統繁忙請稍后再試,id:" + id + ",hystrix兜底處理";
}
二、服務降級(服務消費者)
1.pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.yaml檔案
feign:
hystrix:
enabled: true
client:
config:
default:
ReadTimeout: 15000
ConnectTimeout: 15000 #設定ribbon超時時間
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 15000 # 設定hystrix的超時時間
3.啟動類添加@EnableHystrix注解
4.在需要進行服務降級的呼叫介面上添加@HystrixCommand
@HystrixCommand(fallbackMethod = "paymentInfo_timeout_fallback", commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "9000")
})
5.提供觸發服務降級條件或呼叫發生例外時的執行方法
public String paymentInfo_timeout_fallback(@PathVariable("id") Long id){
return "我是訂單模塊80,支付模塊業務繁忙請稍后再試" + id;
}
6.注意:hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds和ribbon超時時間默認都是1s,網上有人說只需要配置其中一個,但是本人測驗,這兩塊配置少了任何一個,呼叫時間超過1s后都會觸發服務降級
三、全域服務降級DefaultProperties
1.在介面類上添加注解@DefaultProperties
@DefaultProperties(defaultFallback = "global_fallback")
2.提供觸發服務降級時執行的方法
public String global_fallback(){
return "全域例外服務降級處理";
}
3.在需要服務降級的介面上添加@HystrixCommand注解,默認觸發服務降級時,即會執行全域服務降級處理方法,
四、通配服務降級
1.提供FeignClient介面的實作類(服務降級處理類)
@Component
public class OrderServiceImpl implements OrderService {
@Override
public String paymentInfo(Long id) {
return "paymentIfo fall back 在服務上配置服務降級";
}
@Override
public String paymentInfo_timeout(Long id) {
return "paymentIfo time out fall back 在服務上配置服務降級";
}
}
2.定義@FeignClient的fallback屬性指向服務降級處理類
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = OrderServiceImpl.class)
五、服務熔斷
熔斷打開:斷路器配置以后默認處于closed狀態,達到熔斷條件后,進入open狀態,請求不再呼叫當前服務,內部設定一般為MTTR(平均故障處理時間,默認5s),當打開長達到所設時鐘則進入half open
熔斷關閉:熔斷關閉不會對服務進行熔斷
熔斷半開:部分請求根據規則呼叫當前服務,如果請求成功且符合規則則認為當前服務恢復正常,關閉熔斷
模擬代碼如下:
@HystrixCommand(fallbackMethod = "paymentCircutHandler", 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"), //失敗率達到多少后跳閘
})
@GetMapping("/payment/hystrix/circut/{id}")
public String paymentCircutBreaker(@PathVariable Long id){
if (id < 0){
throw new RuntimeException("id不合法");
}
String result = "執行緒:" + Thread.currentThread().getName() + " 呼叫成功,流水號:" + IdUtil.simpleUUID();
log.info("******result:{}", result);
return result;
}
public String paymentCircutHandler(Long id){
return "id不合法,請稍后再試";
}
Hystrix斷路器三個重要指標引數
1、circuitBreaker.sleepWindowInMilliseconds
斷路器的快照時間窗,也叫做視窗期,可以理解為一個觸發斷路器的周期時間值,默認為10秒(10000),
2、circuitBreaker.requestVolumeThreshold
斷路器的視窗期內觸發斷路的請求閾值,默認為20,換句話說,假如某個視窗期內的請求總數都不到該配置值,那么斷路器連發生的資格都沒有,斷路器在該視窗期內將不會被打開,
3、circuitBreaker.errorThresholdPercentage
斷路器的視窗期內能夠容忍的錯誤百分比閾值,默認為50(也就是說默認容忍50%的錯誤率),打個比方,假如一個視窗期內,發生了100次服務請求,其中50次出現了錯誤,在這樣的情況下,斷路器將會被打開,在該視窗期結束之前,即使第51次請求沒有發生例外,也將被執行fallback邏輯,
六、Hystrix圖形化dashboard
hystrix dashboard專案模塊搭建
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
啟動類
添加@EnableHystrixDashboard注解
dashboard專案啟動后瀏覽器打開監控頁面
http://localhost:9001/hystrix
添加被監控專案
http://localhost:8008/hystrix.stream
注意
根據尚硅谷課程講解,由于springcloud的坑,在被監控專案啟動類中需要添加如下代碼
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/286263.html
標籤:其他
