斷路器模式源於Martin Fowler的Circuit Breaker一文。「斷路器」自己是一種開關裝置,用於在電路上保護線路過載,當線路中有電器發生短路時,「斷路器」可以及時的切斷故障電路,防止發生過載、發熱、甚至起火等嚴重後果。html
在分佈式架構中,斷路器模式的做用也是相似的,當某個服務單元發生故障(相似用電器發生短路)以後,經過斷路器的故障監控(相似熔斷保險絲),向調用方返回一個錯誤響應,而不是長時間的等待。這樣就不會使得線程因調用故障服務被長時間佔用不釋放,避免了故障在分佈式系統中的蔓延。java
在微服務架構中爲例保證程序的可用性,防止程序出錯致使網絡阻塞,出現了斷路器模型。斷路器的情況反應了一個程序的可用性和健壯性,它是一個重要指標。Hystrix Dashboard是做爲斷路器狀態的一個組件,提供了數據監控和友好的圖形化界面web
複製項目 spring-cloud-ribbon-consumer-hystrix
,修更名稱 spring-cloud-ribbon-consumer-hystrix-dashboard
在它的基礎上進行改造。 Feign
的改造和這同樣。spring
在 pom
的工程文件引入相應的依賴:網絡
<dependency> <groupId> org.springframework.cloud </groupId> <artifactId> spring-cloud-starter-hystrix </artifactId> </dependency> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-actuator </artifactId> </dependency> <dependency> <groupId> org.springframework.cloud </groupId> <artifactId> spring-cloud-starter-hystrix-dashboard </artifactId> </dependency>
開啓 HD架構
修改 RibbonConsumerApplication.java
類app
在程序的入口 RibbonConsumerApplication
類,加上 @EnableHystrix
註解開啓斷路器,這個是必須的,而且須要在程序中聲明斷路點 @HystrixCommand;
加上 @EnableHystrixDashboard
註解,開啓 HystrixDashboard,
歡迎你們一塊兒學習研究相關技術願意瞭解源碼的朋友直接求求交流分享技術:2147775633分佈式
package io.ymq.example.ribbon.consumer.hystrix; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; . . import org.springframework.web.client.RestTemplate; @EnableHystrix @EnableDiscoveryClient @EnableHystrixDashboard @SpringBootApplication public class RibbonConsumerApplication { @LoadBalanced @Bean RestTemplate restTemplate() { return new RestTemplate(); public static void main(String[] args) { SpringApplication.run(RibbonConsumerApplication.class, args);
聲明斷路點 @HystrixCommand(fallbackMethod="defaultStores")
ide
package io.ymq.example.ribbon.consumer.hystrix; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; * 描述:調用提供者的 `home` 方法 * @author yanpenglei @RestController public class ConsumerController { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "defaultStores") @GetMapping(value = "/hello") public String hello() { return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody(); public String defaultStores() { return "feign + hystrix Dashboard ,提供者服務掛了";
@HystrixCommand
代表該方法爲 hystrix
包裹,能夠對依賴服務進行隔離、降級、快速失敗、快速重試等等 hystrix
相關功能 該註解屬性較多,下面講解其中幾個spring-boot
fallbackMethod
降級方法
commandProperties
普通配置屬性,能夠配置 HystrixCommand
對應屬性,例如採用線程池仍是信號量隔離、熔斷器熔斷規則等等
ignoreExceptions
忽略的異常,默認 HystrixBadRequestException
不計入失敗
groupKey()
組名稱,默認使用類名稱
commandKey
命令名稱,默認使用方法名
更多詳細源碼參考來源:http://minglisoft.cn/honghu/technology.html