2019.3 計劃翻譯 Spring Cloud Netflix 和 Spring Cloud OpenFeign
跳轉 Spring Cloud Greenwich.RELEASE 官方文檔 原文
@[toc]d
特性
III. Spring Cloud Netflix
13. 斷路器: Hystrix 用戶端
Hystrix 是 Netflix 下一個實作了 circuit breaker pattern 的庫,在微服務架構中,服務調用通常存在多層,如下圖所示:
圖 13.1. 微服務圖
較低層級的服務故障将引起上層服務的級聯故障。當在
metrics.rollingStats.timeInMilliseconds
(預設值為 10 秒)時間内對指定服務調用超過
circuitBreaker.requestVolumeThreshold
(預設值為 20 次請求)次,且失敗的比例大于
circuitBreaker.errorThresholdPercentage
(預設值為 50%),斷路器打開同時服務不可達。在出現錯誤且斷路器打開時,開發者可以提供一個回退邏輯。
圖 13.2. Hystrix 回退防止級聯故障
通過打開斷路器防止級聯故障,并給予不止所措甚至失敗的服務一段時間,用以恢複正常,回退邏輯可以是另一個 Hystrix 保護的調用、靜态資料或一個空值,一個請求的回退邏輯本身能被其他業務請求作為回退邏輯調用,這些調用最終都将得到靜态資料。
13.1. 怎樣引入 Hystrix
在項目中,通過 starter 使用 group ID:
org.springframework.cloud
artifact ID:
spring-cloud-starter-netflix-hystrix
引入 Hystrix。建構系統時,使用目前的
Spring Cloud Release Train
,詳情查閱 Spring Cloud Project page 。
一個使用 Eureka 服務端和 Hystrix 斷路器的程式示例:
@SpringBootApplication
@EnableCircuitBreaker
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
@Component
public class StoreIntegration {
@HystrixCommand(fallbackMethod = "defaultStores")
public Object getStores(Map<String, Object> parameters) {
//do stuff that might fail
}
public Object defaultStores(Map<String, Object> parameters) {
return /* something useful */;
}
}
@HystrixCommand
由名為 “javanica” 的 Netflix 庫提供,Spring Cloud 将使用 @HystrixCommand 注解的 Spring beans 自動包裝成一個連接配接到 Hystrix 斷路器的代理。斷路器自行計算何時打開和關閉,以及在故障時做什麼。
要配置
@HystrixCommand
,你可以使用一個
@HystrixProperty
注解清單填充
commandProperties
屬性,在 這裡 檢視更多詳情,在 Hystrix 維基 檢視更多可用屬性。
13.2. 傳遞安全上下文或使用 Spring 作用域
如果你希望某些本地線程上下文傳遞到
@HystrixCommand
,預設的聲明将失效,因為它線上程池中執行指令(逾時時)。你可以通過配置或直接在注解中開啟讓 Hystrix 在同一個線程中調用,這要求使用不同的 “隔離政策”,在注解中設定線程的示例如下:
@HystrixCommand(fallbackMethod = "stubMyService",
commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
}
)
...
你可以使用
@SessionScope
或
@RequestScope
來完成同樣的事情。如果遇到運作時異常,說明找不到作用域内的上下文,你需要使用同一個線程來執行。
你也可以設定
hystrix.shareSecurityContext
屬性為
true
。這樣會自動配置一個 Hystrix 并發政策插件鈎子,用來将
SecurityContext
從主線程轉換到 Hystrix 指令執行線程。Hystrix 不允許注冊多個 Hystrix 并發政策,是以可以将自定義一個
HystrixConcurrencyStrategy
聲明為 Spring bean 的形式作為擴充機制。Spring Cloud 将在 Spring 上下文中查找你的實作,并将其包裝到自己的插件中。
13.3. 健康訓示器
連接配接斷路器的狀态同樣通過調用應用程式的
/health
端點暴露,如下所示:
{
"hystrix": {
"openCircuitBreakers": [
"StoreIntegration::getStoresByLocationLink"
],
"status": "CIRCUIT_OPEN"
},
"status": "UP"
}
13.4. Hystrix 名額流
要使用 Hystrix 名額流,引入依賴
spring-boot-starter-actuator
并配置
management.endpoints.web.exposure.include: hystrix.stream
,暴露
/actuator/hystrix.stream
作為管理端點,如下所示:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
14. 斷路器: Hystrix 儀表盤
Hystrix 的主要優點之一是它收集每個 HystrixCommand 的一套名額,Hystrix 儀表盤以有效的方式顯示每個斷路器的健康狀況。
圖 14.1. Hystrix 儀表盤
15. Hystrix 逾時和 Ribbon 用戶端
當使用包含 Ribbon 用戶端的Hystrix 指令時,你需要確定 Hystrix 逾時配置長于 Ribbon 逾時配置,包括可能發生的任何潛在的重試,比如,你的 Ribbon 連接配接逾時配置為一秒,Hystrix 可能重試三次請求,你的 Hystrix 逾時配置應超過三秒鐘。
15.1. 怎樣引入 Hystrix 儀表盤
在項目中,通過 starter 使用 group ID: org.springframework.cloud artifact ID: spring-cloud-starter-netflix-hystrix-dashboard 引入 Hystrix 儀表盤。建構系統時,使用目前的
Spring Cloud Release Train
,詳情查閱 Spring Cloud Project page 。
要運作 Hystrix 儀表盤,使用
@EnableHystrixDashboard
注解修飾你的 Spring Boot 主類,然後通路
/hystrix
,并将儀表闆指向 Hystrix 用戶端應用程式中的單個執行個體
/hystrix.stream
端點。
連接配接到使用HTTPS的
/hystrix.stream
端點時,伺服器使用的證書必須由JVM信任。如果證書不可信,需要将證書導入到 JVM,以便 Hystrix 儀表盤成功連接配接到流端點
15.2. Turbine
15.2.1 Clusters Endpoint