天天看點

spring cloud: Hystrix斷路器(熔斷器)

1.Hystrix用戶端

Netflix已經建立了一個名為Hystrix的庫,實作了斷路器的模式。在microservice架構通常有多個層的服務調用。 

spring cloud: Hystrix斷路器(熔斷器)
低水準的服務的服務失敗會導緻級聯故障一直給到使用者。當調用一個特定的服務達到一定門檻值(預設5秒失敗20次),打開斷路器。在錯誤的情況下和一個開啟的斷路復原應可以由開發人員提供。 
spring cloud: Hystrix斷路器(熔斷器)

有一個斷路器阻止級聯失敗并且允許關閉服務一段時間進行愈合。復原會被其他hystrix保護調用,靜态資料或健全的空值。 

代碼如下:

@SpringBootApplication@EnableCircuitBreakerpublic class Application {

    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}123456789123456789      
@Componentpublic 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 */;
    }
}123456789101112123456789101112      

@HystrixCommand是由Netflix contrib 庫提供,叫做javanica。spring cloud自動包裝Spring bean與注釋的代理連接配接到Hystrix斷路器。斷路器計算何時打開和關閉斷路,并在失敗的情況下做什麼。 

配置@HystrixCommand可以使用commandProperties屬性的清單@HystrixProperty注釋。詳細請看https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration 

https://github.com/Netflix/Hystrix/wiki/Configuration

1.1 傳播安全上下文或者使用spring範圍

如果你想要一些線程本地上下文傳播到@HystrixCommand預設聲明将不會工作,因為它執行線程池中的指令(在逾時的情況下)。 

可以切換Hystrix使用一些配置用相同的線程調用者,或直接在注釋,讓它使用不同的“隔離政策”(Isolation Strategy)。 

例如:

@HystrixCommand(fallbackMethod = "stubMyService",
    commandProperties = {
      @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
    }
)...123456123456      

詳細内容請參考https://github.com/Netflix/Hystrix/wiki/Configuration

1.2 健康監控

連接配接的斷路器的狀态也暴露在調用應用程式的/health端點。

{
    "hystrix": {
        "openCircuitBreakers": [            "StoreIntegration::getStoresByLocationLink"
        ],
        "status": "CIRCUIT_OPEN"
    },
    "status": "UP"}123456789123456789      

1.3 Hystrix Metrics Stream(hystrix名額流)

spring-boot-starter-actuator中實作了Hystrix metrics stream。暴露/hystrix.stream作為一個管理端點。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>1234512345      

2.Hystrix dashboard

Hystrix的主要好處之一是它收集關于每個HystrixCommand組名額。Hystrix儀表闆顯示每個斷路器的健康高效的方式。 

spring cloud: Hystrix斷路器(熔斷器)

運作Hystrix儀表闆需要在spring boot主類上标注@EnableHystrixDashboard。然後通路/ hystrix檢視儀表盤,在hystrix用戶端應用使用/hystrix.stream監控。

2.1 turbine

看一個執行個體Hystrix資料對于整個系統的健康不是很有用。turbine是一個應用程式,該應用程式彙集了所有相關的/hystrix.stream端點到 /turbine.stream用于Hystrix儀表闆。運作turbine使用@EnableTurbine注釋你的主類,使用spring-cloud-starter-turbine這個jar。配置請參考https://github.com/Netflix/Turbine/wiki/Configuration-(1.x) 

唯一的差別是turbine.instanceUrlSuffix不需要端口号字首,因為這是自動處理,除非turbine.instanceInsertPort = false。

turbine.appConfig配置是一個eureka服務ID清單,turbine将使用這個配置查詢執行個體。turbine stream在hystrix dashboard中使用如下的url配置: 

http://my.turbine.server:8080/turbine.stream?cluster=,如果叢集的名稱是default,叢集參數可以忽略)。這個叢集參數必須和turbine.aggregator.clusterConfig比對。從eureka傳回的值都是大寫的,是以我們希望下面的例子可以工作,如果一個app使用eureka注冊,并且被叫做customers:

turbine:
  aggregator:
    clusterConfig: CUSTOMERS
  appConfig: customers12341234      

clusterName可以使用SPEL表達式定義,在turbine.clusterNameExpression。 

預設值是appName,意思是eureka服務ID最終将作為叢集的key,例如customers的InstanceInfo有一個CUSTOMERS的appName。另外一個例子是turbine.clusterNameExpression=aSGName,将從AWS ASG name擷取叢集名稱。

另一個例子:

turbine:
  aggregator:
    clusterConfig: SYSTEM,USER
  appConfig: customers,stores,ui,admin
  clusterNameExpression: metadata['cluster']1234512345      

在這種情況下,叢集名稱從4個服務從其中繼資料映射,期望包含“SYSTEM”和“USER”。

所有的app使用default,你需要一個文字表達式(使用單引号):

turbine:
  appConfig: customers,stores
  clusterNameExpression: 'default'123123      

2.2 turbine AMQP