整理之前的筆記,發現有一些內容沒有發出來,陸續發出來。。。
Circuit Breaker: Hystrix Clients
Netflix 創建了一個叫做Hystrix的庫,這個庫實現了熔斷器模式。在微服務架構中,經常會有一次請求調用多個微服務的情況。
一個底層的服務調用失敗可能導致所有上層服務的級聯調用失敗。當調用一個特定的服務達到一定的閥值時(Hystrix默認是5秒鐘20次調用失敗),熔斷器電路會自動打開。開發者只需要提供一個fallback 方案就行了。
熔斷器生效的情況下,可以停止服務級聯失效,給了失效的服務一定的時間修復。fallback 可以是另一個受Hystrix 保護的服務,靜態的數據或者是一個有意義的空值。
How to Include Hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
[Example boot app:]
@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是Netflix contrib library "javanica"提供的。Spring Cloud在一個代理中自動的包裝@HystrixCommand註解過的方法,使其鏈接到一個Hystrix 的熔斷器。熔斷器自己計算什麼時候打開關閉熔斷器閘門以及在調用失敗時可以幹什麼。
可以使用@HystrixCommand 的commandProperties 屬性給HystrixCommand 配置一個@HystrixProperty列表。
更多信息
Hystrix wiki-commandProperties所有可用的配置
Propagating the Security Context or using Spring Scopes
如果你希望一些thread local的上下文傳播到@HystrixCommand的實現中,默認將不會起作用,因爲HystrixCommand的執行時在一個線程池中。你可以通過設置Isolation Strategy,將Hystrix 配置成HystrixCommand的執行跟請求調用在同一個線程裏面。
@HystrixCommand(fallbackMethod = "stubMyService",
commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
}
)
...
如果你使用@SessionScope或者@RequestScope,將會有同樣的效果。
你還有一個選項就是設置hystrix.shareSecurityContext屬性爲true。這將會自動配置一個Hystrix併發策略的插件鉤子,這個鉤子會將SecurityContext 從主線程傳遞到Hystrix command執行的線程。Hystrix不允許設置多個併發策略,通過聲明自己的HystrixConcurrencyStrategy實現作爲一個spring bean,可以擴展成可以設置多個併發策略。Spring Cloud將會在Spring的上下文中尋找HystrixConcurrencyStrategy的實現,然後包裝成自己的插件。
Health Indicator
熔斷器的狀態暴露在/health endpoint中。
{
"hystrix": {
"openCircuitBreakers": [
"StoreIntegration::getStoresByLocationLink"
],
"status": "CIRCUIT_OPEN"
},
"status": "UP"
}
Hystrix Metrics Stream
引入spring-boot-starter-actuator可以啓動Hystrix的metrics stream功能,信息暴露在/hystrix.stream endpoint上。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Circuit Breaker: Hystrix Dashboard
Hystrix會從每一個HystrixCommand中收集度量信息。Hystrix Dashboard 以一個高效的方式展示每一個熔斷器的將康狀態。
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-D5L9TSQf-1569737545043)(httphttp://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.3.1.RELEASE/images/Hystrix.png)]
Hystrix Timeouts And Ribbon Clients
當使用Hystrix command來包裝Ribbon Client時,你肯定希望Hystrix 的超時時間要長於Ribbon的超時時間,因爲有可能一次請求會包含多次嘗試。例如,如果你的Ribbon 鏈接超時是1秒,Ribbon Client的一次請求可能需要嘗試3次,也就是說,Hystrix 的超時時間應該稍稍多餘3秒。
How to Include Hystrix Dashboard
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
使用@EnableHystrixDashboard註解你的Spring Boot主類可以啓動Hystrix Dashboard。可以使用Hystrix Dashboard訪問Hystrix應用的/hystrix和/hystrix.stream端口來獲取一個友好的信息展示方式。