2019独角兽企业重金招聘Python工程师标准>>>

使用Hystrix守护应用(1) 博客分类: java 微服务

Hystrix(https://github.com/Netflix/Hystrix)是Netflix(https://www.netflix.com/global)的一个开源项目,主要作用是通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。 其可以看做是Netflix团队对分布式系统运维的各种理念和实践的总结。值得一提的是在ThoughtWorks最新的Tech Radar 2014(http://www.thoughtworks.com/radar/#/tools)中,Hystrix的评级已从评估(Assess)上升到试用(Trial),即其已经完善到可以在产品环境中试用了,相信会有越来越多的公司采用该类库来提升自己系统的容错能力,我所在的部门刚刚把Hystrix加入了工具箱,并已在某几个项目中实践了Hystrix。

在项目中使用Hystrix 
Hystrix本质上是一个基于JVM的类库,Netflix团队已经把其发布到Maven中央库中,因此只要你的项目是基于JVM的,那么在项目中使用Hystrix就非常容易了。本文中将以Gradle为构建脚本的Spring MVC Web项目为例(代码已托管到github:https://github.com/xianlinbox/HystrixDemo),在该示例项目中,构建了一个Customer Service,该customer service会依赖另外的2个web service(Contact Service和Address Service, 示例中我使用moco(https://github.com/dreamhead/moco)模拟了这2个服务)。如下图: 

在Customer Service模块中使用Hystrix把对Contact Service和Address Service的依赖隔离开,可以防止一个服务的故障影响到另外一个服务。

首先,为项目添加Hystrix依赖:

Gradle代码  
  1. 'com.netflix.hystrix:hystrix-core:1.3.8',

然后,把所有需要访问远程系统,服务和第三方库的调用都封装到HystrixCommand中,

Java代码  
  1. public class AddressHystrixCommand extends HystrixCommand<Address> {
  2. private Logger logger = LoggerFactory.getLogger(AddressHystrixCommand.class);
  3. private String customerId;
  4. public AddressHystrixCommand(String customerId) {
  5. super(HystrixCommandGroupKey.Factory.asKey("Address"));
  6. this.customerId = customerId;
  7. }
  8. @Override
  9. public Address run() throws Exception {
  10. logger.info("Get address for customer {}", customerId);
  11. String response = Request.Get("http://localhost:9090/customer/" + customerId + "/address")
  12. .connectTimeout(1000)
  13. .socketTimeout(1000)
  14. .execute()
  15. .returnContent()
  16. .asString();
  17. return new ObjectMapper().readValue(response, Address.class);
  18. }
  19. }
  20. public class ContactHystrixCommand extends HystrixCommand<Contact> {
  21. private Logger logger = LoggerFactory.getLogger(ContactHystrixCommand.class);
  22. private String customerId;
  23. public ContactHystrixCommand(String customerId) {
  24. super(HystrixCommandGroupKey.Factory.asKey("Contact"));
  25. this.customerId = customerId;
  26. }
  27. @Override
  28. public Contact run() throws Exception {
  29. logger.info("Get contact for customer {}", customerId);
  30. String response = Request.Get("http://localhost:9090/customer/" + customerId + "/contact")
  31. .connectTimeout(1000)
  32. .socketTimeout(1000)
  33. .execute()
  34. .returnContent()
  35. .asString();
  36. return new ObjectMapper().readValue(response, Contact.class);
  37. }
  38. }

最后,在需要调用远程服务时候,使用HystrixCommand的方法即可

Java代码  
  1. customer.setContact(new ContactHystrixCommand(customerId).execute());
  2. customer.setAddress(new AddressHystrixCommand(customerId).execute());

运行效果如下: 
21:01:11.117 [373380609@qtp-1421210709-0] INFO  c.x.h.s.CustomerService - Get Customer 1234 
21:01:11.163 [373380609@qtp-1421210709-0] WARN  c.n.c.s.URLConfigurationSource - No URLs will be polled as dynamic configuration sources. 
21:01:11.207 [hystrix-Contact-1] INFO  c.x.h.d.ContactHystrixCommand - Get contact for customer 1234 
21:01:11.378 [hystrix-Address-1] INFO  c.x.h.d.AddressHystrixCommand - Get address for customer 1234

从日志中,我们可以看到HystrixCommand封装的服务分别运行在单独的线程中。上面只是最简单的Hystrix用法,Netflix在Hystrix中加入了非常细致的配置和灵活的使用方法,以帮助用户灵活的得到自己想要的控制效果。下面就来看一看具体有哪些配置和用法。

配置HystrixCommand 
HystxixCommand支持如下的配置: 
GroupKey:该命令属于哪一个组,可以帮助我们更好的组织命令。 
CommandKey:该命令的名称 
ThreadPoolKey:该命令所属线程池的名称,同样配置的命令会共享同一线程池,若不配置,会默认使用GroupKey作为线程池名称。 
CommandProperties:该命令的一些设置,包括断路器的配置,隔离策略,降级设置,以及一些监控指标等。 
ThreadPoolProerties:关于线程池的配置,包括线程池大小,排队队列的大小等。

为了方便大家的配置,Hystrix非常贴心的提供了很多工厂方法。下面就是一个涉及到上面所有配置的例子:

Java代码  
  1. public class EchoCommand extends HystrixCommand<String> {
  2. private String input;
  3. protected EchoCommand(String input) {
  4. super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("EchoGroup"))
  5. .andCommandKey(HystrixCommandKey.Factory.asKey("Echo"))
  6. .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("EchoThreadPool"))
  7. .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
  8. .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD))
  9. .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
  10. .withCoreSize(10))
  11. );
  12. this.input = input;
  13. }
  14. @Override
  15. protected String run() throws Exception {
  16. return "Echo: " + input;
  17. }
  18. }

http://ningandjiao.iteye.com/blog/2171185

转载于:https://my.oschina.net/xiaominmin/blog/1598974

使用Hystrix守护应用(1)相关推荐

  1. 使用Hystrix守护应用(3)

    2019独角兽企业重金招聘Python工程师标准>>> 使用Hystrix守护应用(3) 博客分类: 架构 spring 微服务 监控HystrixCommand  除了隔离依赖服务 ...

  2. Hystrix的一个坑,queue中的run方法没有被执行?

    今天学的时候随手测了一下Hystrix的queue的异步执行,发现执行queue之后,还没有打印run方法中的内容,程序就结束了: import com.netflix.hystrix.Hystrix ...

  3. hystrix 源码 线程池隔离_Spring Cloud Hystrix 源码学习合集

    # Spring Cloud Hystrix 源码学习合集 **Hystrix: Latency and Fault Tolerance for Distributed Systems** ![](h ...

  4. Python 多进程笔记 — 启动进程的方式、守护进程、进程间通信、进程池、进程池之间通信、多进程生产消费模型

    1 面向过程启动多进程 Python 操作进程的类都定义在 multiprocessing 模块,该模块提供了一个 Process 类来代表一个进程对象,这个对象可以理解为是一个独立的进程,可以执行另 ...

  5. 【微服务架构】SpringCloud之断路器(hystrix)

    说在前面 在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用 ...

  6. 用 Hystrix 构建高可用服务架构

    1 hystrix是什么 在分布式系统中,每个服务都可能会调用很多其他服务,被调用的那些服务就是依赖服务,有的时候某些依赖服务出现故障也是很正常的. Hystrix 可以让我们在分布式系统中对服务间的 ...

  7. 5.1.14 守护线程

    守护进程与守护线程的区别: 守护进程:主进程代码运行完后,守护进程就终止. 守护线程:主进程运行完后,守护线程就终止.不过,如果主线程有多个线程的话, 其他线程未执行完,主线程就还在.守护线程会等主进 ...

  8. php 一秒操作一次_php守护进程 加linux命令nohup实现任务每秒执行一次

    Unix中 nohup 命令功能就是不挂断地运行命令,同时 nohup 把程序的所有输出到放到当前目录 nohup.out 文件中,如果文件不可写,则放到 /nohup.out 文件中.那么有了这个命 ...

  9. 【部署类】专题:消息队列MQ、进程守护Supervisor

    目录 1 背景需求 2 技术方案 2.1 消息队列 2.2 进程守护 3 源码介绍 3.1 supervisor部分 3.1.1 supervisord.conf 内容 3.1.2 MM3D.conf ...

最新文章

  1. 分布式配置中心disconf第三部(基于xml配置的实现原理)
  2. Ubuntu 16.04更新软件提示需要安装不能信任的软件包 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease
  3. RxJava 在Android中的应用(二)
  4. 中国牙科用人工骨替代材料市场供需态势与未来投资方向分析报告2022年
  5. window下的host路径
  6. python切面异常处理_Spring项目中优雅的异常处理
  7. 终于知道以后该咋办了!
  8. 星巴克、喜茶们左右围守 瑞幸的大师故事还能讲多久
  9. Git CMD - diff: Show changes between commits, commit and working tree, etc
  10. 图片资源加载路径分析以及netbeans打包java程序,并包含图片资源
  11. python中实参必须是常量吗_7 python函数参数(必须参数、可变参数、关键字参数)...
  12. 黑马程序员-java学习笔记_整理黑马官网Java自学27天基础视频及笔记
  13. 《Netty权威指南》笔记 —— 第十二、十三、十四章
  14. 练习华为大型公司网络构建拓扑图
  15. 十个程序员必备的网站推荐和较出名的国外程序员论坛
  16. python实现 stft_Python中可转换的STFT和ISTFT
  17. 阿里云解析PrivateZone和云解析DNS的区别
  18. glassfish linux,使用glassfish在linux上的部署网站
  19. 大学英语综合教程二 Unit 1 到Unit 8 课文内容英译中 中英翻译
  20. 编写MapReduce程序计算平均分

热门文章

  1. [原]pomelo开发环境搭建
  2. 创建log文件的代码
  3. 2.权限管理准备工作:你应该知道的ASP.NET网站最基本的安全措施!
  4. 马斯克、吴恩达等27人出镜:AI可能成为不朽独裁者,人类就像蚂蚁束手就擒
  5. Nginx 学习笔记(十)介绍HTTP / 2服务器推送(译)
  6. Windows 使用windump进行循环抓包
  7. Redis安装及HA(High Availability)配置
  8. Dom对象与jQuery对象的转换
  9. “加密解密专区”的“滚动”广告太老了
  10. Q107:Mac系统下GDB对PBRT-V3进行debug