天天看點

Circuit Breaker: Hystrix

整理之前的筆記,發現有一些内容沒有發出來,陸續發出來。。。

Circuit Breaker: Hystrix Clients

Netflix 建立了一個叫做Hystrix的庫,這個庫實作了熔斷器模式。在微服務架構中,經常會有一次請求調用多個微服務的情況。

Circuit Breaker: Hystrix

一個底層的服務調用失敗可能導緻所有上層服務的級聯調用失敗。當調用一個特定的服務達到一定的閥值時(Hystrix預設是5秒鐘20次調用失敗),熔斷器電路會自動打開。開發者隻需要提供一個fallback 方案就行了。

Circuit Breaker: Hystrix

熔斷器生效的情況下,可以停止服務級聯失效,給了失效的服務一定的時間修複。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

How to Include Hystrix Dashboard

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>