Spring Cloud Netflix—斷路器:Hystrix客戶端

Netflix的創造了一個調用的庫Hystrix實現了斷路器圖案。在微服務架構中,一般有多層服務調用。 HystrixGraph 圖1.微服務圖 較低級別的服務中的服務故障可能致使用戶級聯故障。當對特定服務的呼叫達到必定閾值時(Hystrix中的默認值爲5秒內的20次故障),電路打開,不進行通話。在錯誤和開路的狀況下,開發人員能夠提供後備。html

HystrixFallback 圖2. Hystrix回退防止級聯故障 開放式電路會中止級聯故障,並容許沒必要要的或失敗的服務時間來癒合。回退能夠是另外一個Hystrix保護的調用,靜態數據或一個正常的空值。回退可能被連接,因此第一個回退使得一些其餘業務電話又回到靜態數據。java

如何加入Hystrix 要在項目中包含Hystrix,請使用組org.springframework.cloud和artifact id spring-cloud-starter-hystrix的啓動器。有關 使用當前的Spring Cloud發佈列表設置構建系統的詳細信息,請參閱Spring Cloud項目頁面。web

示例啓動應用程序:spring

@SpringBootApplication @EnableCircuitBreaker public class Application {架構

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

}微服務

@Component public class StoreIntegration {ui

@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 contrib庫提供 。Spring Cloud在鏈接到Hystrix斷路器的代理中使用該註釋自動包裝Spring bean。斷路器計算什麼時候打開和關閉電路,以及在發生故障時應該作什麼。spa

要配置@HystrixCommand,您能夠使用commandProperties屬性列出@HystrixProperty註釋。請參閱 這裏 瞭解更多詳情。有關 可用屬性的詳細信息,請參閱Hystrix維基。代理

源碼來源:http://minglisoft.cn/honghu/technology.html
相關文章
相關標籤/搜索