什麼是斷路器html
斷路器模式源於Martin Fowler的Circuit Breaker一文。「斷路器」自己是一種開關裝置,用於在電路上保護線路過載,當線路中有電器發生短路時,「斷路器」可以及時的切斷故障電路,防止發生過載、發熱、甚至起火等嚴重後果。git
在分佈式架構中,斷路器模式的做用也是相似的,當某個服務單元發生故障(相似用電器發生短路)以後,經過斷路器的故障監控(相似熔斷保險絲),向調用方返回一個錯誤響應,而不是長時間的等待。這樣就不會使得線程因調用故障服務被長時間佔用不釋放,避免了故障在分佈式系統中的蔓延。github
在Spring Cloud中使用了Hystrix 來實現斷路器的功能。Hystrix是Netflix開源的微服務框架套件之一,該框架目標在於經過控制那些訪問遠程系統、服務和第三方庫的節點,從而對延遲和故障提供更強大的容錯能力。Hystrix具有擁有回退機制和斷路器功能的線程和信號隔離,請求緩存和請求打包,以及監控和配置等功能。spring
接下來咱們就以一個簡單的例子,介紹一下Spring cloud Hystrix的使用緩存
首先在工程中添加spring cloud hystrix的依賴:架構
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
在工程的啓動類中加入@EnableCircuitBreaker開啓熔斷器功能app
@SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class Application { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args){ SpringApplication.run(Application.class,args); } }
這裏也可使用SpringCloud對應的@SpringCloudApplication註解來修飾啓動類,從代碼能夠看到,該註解包含了其餘的三個註解,也就覺得着一個標準的SpringCloud程序包含着服務發現和熔斷機制。歡迎你們一塊兒學習研究相關技術願意瞭解源碼的朋友直接求求交流分享技術:2147775633框架
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public @interface SpringCloudApplication { }
新增ComputeService
類,在使用ribbon消費服務的函數上增長@HystrixCommand
註解來指定回調方法分佈式
@Service public class ComputeService { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "addServiceFallback") public String addService(){ return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody(); } public String addServiceFallback() { return "error"; } }
提供rest接口的Controller改成調用ComputeService的addServiceide
@RestController public class ConsumerController { @Autowired private ComputeService computeService; @RequestMapping(value = "/add", method = RequestMethod.GET) public String add() { return computeService.addService(); } }
驗證斷路器的回調
Feign使用Hystrix
注意這裏說的是「使用」,沒有錯,咱們不須要在Feigh工程中引入Hystix,Feign中已經依賴了Hystrix,咱們能夠在未作任何改造前,嘗試下面你的操做:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Jun 25 22:10:05 CST 2016 There was an unexpected error (type=Internal Server Error, status=500). add timed-out and no fallback available.
若是您夠仔細,會發現與在ribbon中的報錯是不一樣的,看到add timed-out and no fallback available
這句,或許您已經猜到什麼,看看咱們的控制檯,能夠看到報錯信息來自hystrix-core-1.5.2.jar
,因此在這個工程中,咱們要學習的就是如何使用Feign中集成的Hystrix。
使用@FeignClient
註解中的fallback屬性指定回調類
@FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class) public interface ComputeClient { @RequestMapping(method = RequestMethod.GET, value = "/add") Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b); }
建立回調類ComputeClientHystrix
,實現@FeignClient
的接口,此時實現的方法就是對應@FeignClient
接口中映射的fallback函數。
@Component public class ComputeClientHystrix implements ComputeClient { @Override public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) { return -9999; } }
再用以前的方法驗證一下,是否在compute-service服務不可用的狀況下,頁面返回了-9999。