服务熔断框架hystrix学习概要

一:框架

springboot、hystrix、maven

二:depend

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.                 <groupId>org.springframework.boot</groupId>  
  3.                 <artifactId>spring-boot-starter-web</artifactId>  
  4.                 <version>${springboot.version}</version>  
  5.             </dependency>  
  6.             <dependency>  
  7.                 <groupId>com.netflix.hystrix</groupId>  
  8.                 <artifactId>hystrix-core</artifactId>  
  9.                 <version>${hystrix.version}</version>  
  10.             </dependency>  
  11.             <dependency>  
  12.                 <groupId>com.netflix.hystrix</groupId>  
  13.                 <artifactId>hystrix-metrics-event-stream</artifactId>  
  14.                 <version>${hystrix.version}</version>  
  15.             </dependency>  
  16.             <dependency>  
  17.                 <groupId>com.netflix.hystrix</groupId>  
  18.                 <artifactId>hystrix-javanica</artifactId>  
  19.                 <version>${hystrix.version}</version>  
  20.             </dependency>  
三:下载hystrix-dashboard-1.5.5.war

地址:http://search.maven.org/remotecontent?filepath=com/netflix/hystrix/hystrix-dashboard/1.5.5/hystrix-dashboard-1.5.5.war

然后部署到tomcat容器中,此处省略步骤,不会的找块豆腐撞死TAT

部署完成后访问http://127.0.0.1:8080/hystrix-dashboard-1.5.5/,可以看到如下图



四:部署一个web应用

此处用springboot开发了一个简单的ok web应用,起主要作用的有两个类HystrixConfiguration.Java和MainServer.java

HystrixConfiguration:注册spring bean和servlet

MainServer:服务应用入口

代码如下:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.eden.main;  
  2.   
  3. import java.util.Random;  
  4. import java.util.concurrent.TimeUnit;  
  5.   
  6. import org.slf4j.Logger;  
  7. import org.slf4j.LoggerFactory;  
  8. import org.springframework.boot.SpringApplication;  
  9. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.bind.annotation.RequestMethod;  
  12. import org.springframework.web.bind.annotation.RestController;  
  13.   
  14. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;  
  15. import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;  
  16.   
  17. /** 
  18.  * @author eden.ding1991@gmail.com 2016年8月31日 上午10:02:06 
  19.  */  
  20. @SpringBootApplication  
  21. @RestController  
  22. public class MainServer {  
  23.   
  24.     private static final Logger LOG = LoggerFactory.getLogger(MainServer.class);  
  25.   
  26.     public static void main(String[] args) {  
  27.         SpringApplication.run(MainServer.class, args);  
  28.     }  
  29.   
  30.     @RequestMapping(value = "/ok", method = RequestMethod.GET)  
  31.     @HystrixCommand(fallbackMethod = "okFallback", threadPoolProperties = {  
  32.             @HystrixProperty(name = "coreSize", value = "30"), @HystrixProperty(name = "maxQueueSize", value = "100"),  
  33.             @HystrixProperty(name = "queueSizeRejectionThreshold", value = "20") }, commandProperties = {  
  34.                     @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100"),  
  35.                     @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "50")  
  36.   
  37.     })  
  38.     public String ok() throws Exception {  
  39.         int l = new Random().nextInt(200);  
  40.         LOG.info(String.format("l=%s", l));  
  41.         TimeUnit.MILLISECONDS.sleep(l);  
  42.         return "ok";  
  43.     }  
  44.   
  45.     public String okFallback(Throwable e) {  
  46.         System.out.println("execute okFallback!" + e.getMessage());  
  47.         LOG.error("error", e);  
  48.         return "fallback";  
  49.     }  
  50.   
  51. }  

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.eden.main.hystrix;  
  2.   
  3. import org.springframework.boot.web.servlet.ServletRegistrationBean;  
  4. import org.springframework.context.annotation.Bean;  
  5. import org.springframework.context.annotation.Configuration;  
  6.   
  7. import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;  
  8. import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;  
  9.   
  10. /** 
  11.  * 
  12.  * @author eden.ding1991@gmail.com 2016年8月31日 上午11:14:16 
  13.  */  
  14. @Configuration  
  15. public class HystrixConfiguration {  
  16.   
  17.     //注册Hystrix框架的拦截器,使得框架的注解能够生效  
  18.     @Bean  
  19.     public HystrixCommandAspect hystrixAspect() {  
  20.         return new HystrixCommandAspect();  
  21.     }  
  22.   
  23.     //注入servlet,拦截url="/hystrix.stream"  
  24.     //测试git dev commit  
  25.     @Bean  
  26.     public ServletRegistrationBean hystrixMetricsStreamServlet() {  
  27.         ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());  
  28.         registration.addUrlMappings("/hystrix.stream");  
  29.         return registration;  
  30.     }  
  31. }  


@HystrixProperty这个部分注解的含义请参考hystrix的github https://github.com/Netflix/Hystrix/wiki/Configuration


五:启动这个简单的web应用,访问http://127.0.0.1:8081/ok

端口号是8081,这个地方注意,为了防止与dashboard冲突,所以在web应用的application.yml将端口号改成了8081

上面注册的spring bean中有一个servlet是HystrixMetricsStreamServlet,拦截的url是/hystrix.stream,这个地方主要是生成监控需要的数据,可以直接访问这个url:http://127.0.0.1:8081/hystrix.stream  你会看到不停生成一个json格式的数据,里面全是监控的服务的各项metrics


六:开启监控上述web应用



七:监控界面


上述界面是用的测试案例中模拟的,并发60个线程访问,超时时间随机数0-200,系统设计的阈值是100ms,所以看到上图中有28个请求超出阈值导致熔断器打开,然后从界面中也能看到多少请求成功,多少失败,错误率,活跃线程数,排队线程数等

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值