在分佈式架構中,當某個服務單元發生故障後,能經過斷路器的故障監控,向調用方返回一個錯誤響應,而不是長時間的等待。java
在Spring Cloud中使用了Hystrix 來實現斷路器的功能。Hystrix是Netflix開源的微服務框架套件之一,該框架目標在於經過控制那些訪問遠程系統、服務和第三方庫的節點,從而對延遲和故障提供更強大的容錯能力。Hystrix具有擁有回退機制和斷路器功能的線程和信號隔離,請求緩存和請求打包,以及監控和配置等功能。git
一 準備工做github
依次啓動eureka-server、compute-service、eureka-ribbon工程;web
訪問http://localhost:1111/能夠看到註冊中心的狀態;spring
訪問http://localhost:3333/add,調用eureka-ribbon的服務,該服務會去調用compute-service的服務,計算出10+20的值,頁面顯示30;緩存
關閉compute-service的服務,再次訪問http://localhost:3333/add,會獲得如下錯誤架構
2、ribbon引入Hystrixapp
一、ribbon工程的pom.xml中添加依賴框架
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
二、在啓動類中添加註解開啓斷路器功能分佈式
package com.daqsoft; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication //用來發現註冊服務 @EnableDiscoveryClient //開啓斷路器功能 @EnableCircuitBreaker public class CustomerDemoApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(CustomerDemoApplication.class, args); } }
三、改造原來的服務消費方式,新增ComputeService
類,在使用ribbon消費服務的函數上增長@HystrixCommand
註解來指定回調方法
package com.daqsoft; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; /** * @Description Created by liaoxx on 2017-6-13. */ @Service public class ComputeService { @Autowired RestTemplate restTemplate; /** * 調用服務發生故障時回調addServiceFallback方法 * @return */ @HystrixCommand(fallbackMethod = "addServiceFallback") public String addService(){ return restTemplate.getForEntity("http",String.class).getBody(); } public String addServiceFallback(){ return "error"; } }
四、提供rest接口的Controller改成調用ComputeService的addService
package com.daqsoft; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * @Description Created by liaoxx on 2017-6-12. */ @RestController public class CustomController { @Autowired private ComputeService computeService; @RequestMapping(value = "/add", method = RequestMethod.GET) public String add(){ return computeService.addService(); } }
五、啓動服務,訪問 http://localhost:3333/add 進行驗證(成功攔截並進行回調)