一、Hystrix簡介
Hystrix是Netflix開源的一個延遲和容錯庫,主要用於處理分散式系統中的應用服務間的延遲、故障和容錯機制等問題。它可以作為客戶端庫使用,責任在於提供默認的線程池和並發請求數量限制之類的基礎實現,同時還可以作為服務端組件使用,收集和監控服務調用情況,向調用者返回降級機制的響應等。
Hystrix主要有以下幾個特點:
1、降級機制:當一個服務調用失敗或處於超時狀態時,自動切換到後備方法執行。
2、重試機制:自動發起多次嘗試重複請求一個失敗的服務。
3、隔離機制:將客戶端線程池與服務所在的線程池隔離開,防止因為服務異常導致客戶端整個應用程序崩潰。
4、熔斷器:當服務調用失敗或超時超過一定閾值時,自動打開熔斷器進行熔斷,避免服務雪崩。
5、實時監控:對熔斷器/線程池/請求數量等信息進行實時監控和度量。
二、Hystrix在Spring Boot中的應用
在Spring Boot中,使用Hystrix非常容易。Hystrix提供了Spring Cloud Hystrix Starter,封裝了相關的依賴和配置,可以幫助我們快速集成Hystrix。同時,通過在服務類的方法上加上@HystrixCommand註解,可以為每個服務方法自動啟用Hystrix。
具體來說,我們需要做以下幾個步驟:
1、加入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2、啟用Hystrix
在應用程序的主類上添加@EnableHystrix註解:
@SpringBootApplication
@EnableHystrix
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
3、使用@HystrixCommand註解
在服務類的方法上加上@HystrixCommand註解,並指定fallback方法。當服務執行失敗或超時時,將自動執行fallback方法。
@Service
public class OrderService {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "fallback")
public String getOrder(String orderId) {
String url = "http://order-service/order/{orderId}";
return restTemplate.getForObject(url, String.class, orderId);
}
public String fallback(String orderId) {
return "Service unavailable!";
}
}
三、降級機制
當服務處於故障或網路問題時,Hystrix自動切換到fallback方法執行,以保障客戶端的服務可用性。通過指定fallbackMethod屬性,我們可以為每個服務方法定義對應的降級方法。降級方法不能接受與原服務方法不同的參數,同時返回類型可以與原服務方法不同。
四、重試機制
當服務調用失敗時,Hystrix會自動發起多次嘗試重複請求,以提高服務的可用性。可以通過參數控制重試次數、重試間隔和是否支持重試等。
示例:設置重試次數為3,每次重試的間隔為2s
@HystrixCommand(fallbackMethod = "fallback",
commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE"),
@HystrixProperty(name="execution.isolation.semaphore.maxConcurrentRequests", value="10"),
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="3000"),
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold", value="2"),
@HystrixProperty(name="metrics.rollingStats.timeInMilliseconds", value="10000"),
@HystrixProperty(name="retry.enabled", value="true"),
@HystrixProperty(name="retry.attempts", value="3"),
@HystrixProperty(name="retry.interval", value="2000")
})
public String getOrder(String orderId) {
String url = "http://order-service/order/" + orderId;
return restTemplate.getForObject(url, String.class);
}
五、隔離機制
Hystrix通過隔離機制,將客戶端線程池與服務所在的線程池隔離開,防止因為服務異常導致客戶端整個應用程序崩潰。隔離機制主要有兩種策略可選:Thread和Semaphore,默認為Thread模式。可以通過@HystrixCommand註解中的execution.isolation.strategy屬性,進行設置。
示例:設置隔離策略為Semaphore,同時最大並發請求數量為10
@HystrixCommand(fallbackMethod = "fallback",
commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE"),
@HystrixProperty(name="execution.isolation.semaphore.maxConcurrentRequests", value="10"),
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="3000"),
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold", value="2"),
@HystrixProperty(name="metrics.rollingStats.timeInMilliseconds", value="10000")
})
public String getOrder(String orderId) {
String url = "http://order-service/order/" + orderId;
return restTemplate.getForObject(url, String.class);
}
六、熔斷器
當服務調用失敗或超時超過一定閾值時,Hystrix會自動打開熔斷器進行熔斷,防止因為服務異常導致整個系統崩潰。在熔斷的情況下,所有的服務調用都將迅速失敗並返回預定的fallback響應。當熔斷器處於半開狀態時,Hystrix會自動進行探測,以確定服務是否恢復。
可以通過@HystrixCommand註解中的circuitBreaker屬性,進行配置熔斷器的參數。主要屬性包括:requestVolumeThreshold(觸發熔斷器的最小請求數),sleepWindowInMilliseconds(熔斷器打開後的休眠時間)等。
示例:設置觸發熔斷的最小請求數為2,熔斷器打開後的休眠時間為5s
@HystrixCommand(fallbackMethod = "fallback",
commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE"),
@HystrixProperty(name="execution.isolation.semaphore.maxConcurrentRequests", value="10"),
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="3000"),
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold", value="2"),
@HystrixProperty(name="metrics.rollingStats.timeInMilliseconds", value="10000"),
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds", value="5000")
})
public String getOrder(String orderId) {
String url = "http://order-service/order/" + orderId;
return restTemplate.getForObject(url, String.class);
}
七、實時監控
Hystrix提供了實時監控功能,可以監測Hystrix熔斷器的運行狀況,記錄請求次數、成功次數、失敗次數等相關指標,以幫助開發者進行性能監測、問題排查和提高服務可用性。
在Spring Boot應用程序中,可以加入spring-boot-starter-actuator依賴,以啟用相關的監控端點。具體來說,我們需要做以下幾個步驟:
1、加入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>
<scope>runtime</scope>
</dependency>
2、啟用Hystrix監控
在應用程序的主類上添加@EnableHystrixMetricsStream註解,以啟用Hystrix實時監控功能。
@SpringBootApplication
@EnableHystrix
@EnableHystrixMetricsStream
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
3、訪問監控端點
訪問http://:/actuator/hystrix.stream可以獲取Hystrix的實時監控信息。
總結
在分散式系統中,服務間的故障和網路問題是經常會遇到的問題。Hystrix作為延遲和容錯庫,採用了許多強大的機制,以保障服務的可用性和性能。在Spring Boot應用中,我們可以通過簡單的配置和註解使用Hystrix,並通過實時監控功能,幫助我們提高服務質量和可用性。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/186595.html