FeignClient中使用熔断机制hystrix

一, Feign是自带断路器的

feign.hystrix.enabled=true  是否开启熔断器

hystrix.command.default.execution.timeout.enabled=true  是否开启超时熔断, 如果为false, 则熔断机制只在服务不可用时开启

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=300   设置超时熔断时间

[java] view plain copy
  1. @FeignClient(value = "YunCai-SearchServer", fallback=ServiceHystrix.class)  
  2. public interface SchedualServiceHi {  
  3.   
  4.     @RequestMapping(value = "/luke/itemsearch",method = RequestMethod.GET)  
  5.     ResponseMap sayHiFromClientOne(@RequestParam(value = "start") Integer start,  
  6.             @RequestParam(value = "rows") Integer rows,  
  7.             @RequestParam(value = "tenantId") Integer tenantId,  
  8.             @RequestParam(value = "status") Integer status,  
  9.             @RequestParam(value = "key") String key);  
  10. }  

[java] view plain copy
  1. @Component  
  2. public class ServiceHystrix implements SchedualServiceHi {  
  3.   
  4.     @Override  
  5.     public ResponseMap sayHiFromClientOne(Integer start, Integer rows, Integer tenantId, Integer status, String key) {  
  6.         SearchResult result = new SearchResult(0new ArrayList<>(Arrays.asList(1L,2L,3L)));  
  7.         return new ResponseMap(false"111"506, result);  
  8.     }  
  9. }  


二, Hystrix 仪表盘

首选在pom.xml引入spring-cloud-starter-hystrix-dashboard的起步依赖:

[java] view plain copy
  1. <dependency>  
  2.             <groupId>org.springframework.boot</groupId>  
  3.             <artifactId>spring-boot-starter-actuator</artifactId>  
  4.         </dependency>  
  5.   
  6.         <dependency>  
  7.             <groupId>org.springframework.cloud</groupId>  
  8.             <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>  
  9.         </dependency>  


在主程序启动类中加入@EnableHystrixDashboard注解,开启hystrixDashboard:

[java] view plain copy
  1. @SpringBootApplication  
  2. @EnableDiscoveryClient  
  3. @EnableHystrix  
  4. @EnableHystrixDashboard  
  5. public class ServiceRibbonApplication {  
  6.   
  7.     public static void main(String[] args) {  
  8.         SpringApplication.run(ServiceRibbonApplication.class, args);  
  9.     }  
  10.   
  11.     @Bean  
  12.     @LoadBalanced  
  13.     RestTemplate restTemplate() {  
  14.         return new RestTemplate();  
  15.     }  
  16.   
  17. }  


打开浏览器:访问http://localhost:8764/hystrix,界面如下:

FeignClient中使用熔断机制hystrix

点击monitor stream,进入下一个界面,访问:http://localhost:8764/hi?name=forezp

此时会出现监控界面:

FeignClient中使用熔断机制hystrix