一、熔斷器Hystrix使用
Hystrix是一個用於處理分布式系統的熔斷器,主要用於處理由遠程服務調用引起的延遲和故障。下面我們來看看如何使用Hystrix。
在使用Hystrix之前,我們需要添加Hystrix依賴:
<dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>1.5.18</version> </dependency>
然後,我們需要在實際的服務調用方法中添加註釋,說明這個方法需要使用熔斷器:
// 使用Hystrix熔斷器 @HystrixCommand(fallbackMethod = "fallbackMethod") public Object serviceMethod() { // ... } // 熔斷器降級方法 public Object fallbackMethod() { // ... }
在上述示例中,我們在serviceMethod方法中添加了@HystrixCommand注釋,並指定了降級方法fallbackMethod。當服務調用出現問題時,Hystrix會使用fallbackMethod方法來處理這個問題。
二、Hystrix拒絕策略
當Hystrix的熔斷器處於關閉狀態時,如果服務調用超時或者返回錯誤,Hystrix會根據拒絕策略來進行處理。Hystrix提供了以下幾種拒絕策略:
- THREAD:通過新的線程來執行服務調用
- SEMAPHORE:通過信號量來控制服務調用並發數量
在使用Hystrix時,可以通過@HystrixCommand注釋來指定拒絕策略:
@HystrixCommand(fallbackMethod = "fallbackMethod", commandProperties = { // 指定拒絕策略為SEMAPHORE @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE") }) public Object serviceMethod() { // ... }
三、Hystrix全局配置
Hystrix提供了許多全局配置,用於控制Hystrix的行為。以下是一些常用的全局配置:
- hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:Hystrix的超時時間,默認為1000ms
- hystrix.threadpool.default.coreSize:線程池的核心線程數,默認為10
- hystrix.threadpool.default.maxQueueSize:線程池的最大隊列大小,默認為-1
在使用這些全局配置時,可以在application.yml文件中配置相應的屬性:
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 2000 threadpool: default: coreSize: 20 maxQueueSize: 50
四、Hystrix服務恢復
當服務出現故障時,Hystrix會使用熔斷器開啟,避免服務請求繼續發送到該服務上,造成更多的故障,但是如果故障是暫時的,服務恢復之後,需要Hystrix及時將服務調用請求重新發送到該服務上。
在Hystrix中,默認情況下,服務恢復是禁用的,需要手動開啟。可以通過以下方式,將Hystrix的服務恢復開啟:
// 開啟Hystrix的服務恢復 @EnableHystrix public class Application { }
五、普羅米修斯Hystrix
Hystrix提供了普羅米修斯監控插件,可以幫助我們實時監控Hystrix的運行情況,以及定位Hystrix出現問題的原因。
使用該插件需要添加以下依賴:
<dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient</artifactId> <version>0.9.0</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_hotspot</artifactId> <version>0.9.0</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_dropwizard</artifactId> <version>0.9.0</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_servlet</artifactId> <version>0.9.0</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>jmx_prometheus_javaagent</artifactId> <version>0.9.0</version> </dependency>
添加完依賴之後,我們可以通過以下方式將Hystrix的數據暴露給Prometheus:
@Bean public ServletRegistrationBean getServlet() { HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
然後我們啟動服務,訪問http://localhost:xxxx/hystrix.stream,就可以看到Hystrix運行情況的實時監控數據。
六、Hystrix熔斷器
Hystrix熔斷器可以幫助我們在服務調用出現問題時,避免服務出現級聯故障。
以下是熔斷器常用屬性:
- circuitBreaker.requestVolumeThreshold:熔斷器請求閾值,如果在設置的時間窗口內,請求次數小於requestVolumeThreshold,則熔斷器不會開啟
- circuitBreaker.sleepWindowInMilliseconds:休眠時間窗口,開啟熔斷器後,在該時間窗口內,即使請求通過,也不會嘗試關閉熔斷器
- circuitBreaker.errorThresholdPercentage:錯誤率閾值,當請求錯誤率達到閾值時,熔斷器會開啟
可以通過以下方式,設置熔斷器的屬性:
@HystrixCommand(fallbackMethod = "fallbackMethod", commandProperties = { @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"), @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"), @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50") }) public Object serviceMethod() { // ... }
七、Hystrix熔斷策略
Hystrix提供了多種熔斷策略,可以根據不同業務場景來選擇合適的策略。以下是幾種常用的熔斷策略:
- 採用百分比失敗率(FPR),該方法適用於流量較小的情況。當錯誤率達到一定程度時,自動熔斷服務
- 採用固定間隔切換(Fixed Interval),在一段時間內不斷進行服務測試,如果測試通過,則切換服務
- 採用連續失敗計數(Consecutive Failures),服務調用出現連續失敗時,自動熔斷服務
可以通過以下方式,指定熔斷策略:
@HystrixCommand(fallbackMethod = "fallbackMethod", commandProperties = { // 指定熔斷策略為採用百分比失敗率(FPR) @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50") }) public Object serviceMethod() { // ... }
八、Hystrix熔斷應用場景選取
熔斷器和服務降級對於分布式系統來說非常重要,因為避免級聯故障和積壓請求,是確保分布式系統可靠性的重要措施。
下面列舉了幾個適用於Hystrix熔斷應用場景:
- 當服務調用需耗費大量時間時,可使用超時機制,限制服務調用時間,減少請求積壓。
- 當服務出現故障時,可使用熔斷器,開啟熔斷機制,避免服務出現級聯故障。
- 當服務調用頻率過高時,可使用限流機制,使得服務調用不會對系統產生過大的壓力。
綜合使用這些機制,可以有效地保障分布式系統在高並發、大流量的情況下的穩定性和高可用性。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/278304.html