Hystrix([url]https://github.com/Netflix/Hystrix[/url])是Netflix([url]https://www.netflix.com/global[/url])的一个开源项目,主要作用是通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。 其可以看做是Netflix团队对分布式系统运维的各种理念和实践的总结。值得一提的是在ThoughtWorks最新的Tech Radar 2014([url]http://www.thoughtworks.com/radar/#/tools[/url])中,Hystrix的评级已从评估(Assess)上升到试用(Trial),即其已经完善到可以在产品环境中试用了,相信会有越来越多的公司采用该类库来提升自己系统的容错能力,我所在的部门刚刚把Hystrix加入了工具箱,并已在某几个项目中实践了Hystrix。
[size=large][b]在项目中使用Hystrix[/b][/size]
Hystrix本质上是一个基于JVM的类库,Netflix团队已经把其发布到Maven中央库中,因此只要你的项目是基于JVM的,那么在项目中使用Hystrix就非常容易了。本文中将以Gradle为构建脚本的Spring MVC Web项目为例(代码已托管到github:h[url]ttps://github.com/xianlinbox/HystrixDemo[/url]),在该示例项目中,构建了一个Customer Service,该customer service会依赖另外的2个web service(Contact Service和Address Service, 示例中我使用moco([url]https://github.com/dreamhead/moco[/url])模拟了这2个服务)。如下图:
[img]http://dl2.iteye.com/upload/attachment/0104/7763/e1413732-e894-38b0-8493-e3811357119f.png[/img]
在Customer Service模块中使用Hystrix把对Contact Service和Address Service的依赖隔离开,可以防止一个服务的故障影响到另外一个服务。
首先,为项目添加Hystrix依赖:
'com.netflix.hystrix:hystrix-core:1.3.8',
然后,把所有需要访问远程系统,服务和第三方库的调用都封装到HystrixCommand中,
public class AddressHystrixCommand extends HystrixCommand<Address> { private Logger logger = LoggerFactory.getLogger(AddressHystrixCommand.class); private String customerId;
public AddressHystrixCommand(String customerId) { super(HystrixCommandGroupKey.Factory.asKey("Address")); this.customerId = customerId; }
@Override public Address run() throws Exception { logger.info("Get address for customer {}", customerId); String response = Request.Get("http://localhost:9090/customer/" + customerId + "/address") .connectTimeout(1000) .socketTimeout(1000) .execute() .returnContent() .asString();
return new ObjectMapper().readValue(response, Address.class); } }
public class ContactHystrixCommand extends HystrixCommand<Contact> { private Logger logger = LoggerFactory.getLogger(ContactHystrixCommand.class); private String customerId;
public ContactHystrixCommand(String customerId) { super(HystrixCommandGroupKey.Factory.asKey("Contact")); this.customerId = customerId; }
@Override public Contact run() throws Exception { logger.info("Get contact for customer {}", customerId); String response = Request.Get("http://localhost:9090/customer/" + customerId + "/contact") .connectTimeout(1000) .socketTimeout(1000) .execute() .returnContent() .asString();
return new ObjectMapper().readValue(response, Contact.class); } }
最后,在需要调用远程服务时候,使用HystrixCommand的方法即可
customer.setContact(new ContactHystrixCommand(customerId).execute()); 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中加入了非常细致的配置和灵活的使用方法,以帮助用户灵活的得到自己想要的控制效果。下面就来看一看具体有哪些配置和用法。
[size=large][b]配置HystrixCommand[/b][/size]
HystxixCommand支持如下的配置:
[b]GroupKey[/b]:该命令属于哪一个组,可以帮助我们更好的组织命令。
[b]CommandKey[/b]:该命令的名称
[b]ThreadPoolKey[/b]:该命令所属线程池的名称,同样配置的命令会共享同一线程池,若不配置,会默认使用GroupKey作为线程池名称。
[b]CommandProperties[/b]:该命令的一些设置,包括断路器的配置,隔离策略,降级设置,以及一些监控指标等。
[b]ThreadPoolProerties[/b]:关于线程池的配置,包括线程池大小,排队队列的大小等。
为了方便大家的配置,Hystrix非常贴心的提供了很多工厂方法。下面就是一个涉及到上面所有配置的例子:
public class EchoCommand extends HystrixCommand<String> { private String input;
protected EchoCommand(String input) { super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("EchoGroup")) .andCommandKey(HystrixCommandKey.Factory.asKey("Echo")) .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("EchoThreadPool")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)) .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() .withCoreSize(10)) ); this.input = input; }
@Override protected String run() throws Exception { return "Echo: " + input; } }
|