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,界面如下:

Paste_Image.png

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

此时会出现监控界面:

Paste_Image.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值