本文共 4616 字,大约阅读时间需要 15 分钟。
参考:https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica
Java语言比其他语言(如反射和注解)具有很大的优势。 所有现代框架,如Spring,Hibernate,myBatis等都力求最大限度地利用这一优势。 引入Hystrix注解的想法是改进的明显解决方案。 目前使用Hystrix涉及撰写大量的代码,这是快速开发的障碍。 您可能花费大量时间编写Hystrix命令。 通过引入支持注解,可以更容易的使用Hystrix。
通过在项目中对Hystrix的使用,总结出常用的如下几点:
public class UserService {... @HystrixCommand public User getUserById(String id) { return userResource.getUserById(id); }}...
@HystrixCommand public Future3、响应执行--------这块我也不太了解,平常也用不到,貌似使用的rx包下Obervable类,在Android开发中用的比较平凡getUserByIdAsync(final String id) { return new AsyncResult () { @Override public User invoke() { return userResource.getUserById(id); } }; }
@HystrixCommand public ObservablegetUserById(final String id) { return Observable.create(new Observable.OnSubscribe () { @Override public void call(Subscriber observer) { try { if (!observer.isUnsubscribed()) { observer.onNext(new User(id, name + id)); observer.onCompleted(); } } catch (Exception e) { observer.onError(e); } } }); }
@HystrixCommand(groupKey = "hello",commandKey = "hello-service",threadPoolKey = "hello-pool", threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "30"), @HystrixProperty(name = "maxQueueSize", value = "101"), @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"), @HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"), @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"), @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440") }, commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000"), @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD")}, fallbackMethod = "helloFallBack" )大部分场景下使用默认属性即可,不需要配置那么多属性,更多的属性可以参考:https://github.com/Netflix/Hystrix/wiki/Configuration
@DefaultProperties(groupKey = "DefaultGroupKey")class Service { @HystrixCommand // hystrix command group key is 'DefaultGroupKey' public Object commandInheritsDefaultProperties() { return null; } @HystrixCommand(groupKey = "SpecificGroupKey") // command overrides default group key public Object commandOverridesGroupKey() { return null; }}
commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")}如果不配置的话默认是1s,如果依赖服务的响应时间超过1s,则执行fallback;所以需要根据实际情况而定
@HystrixCommand(fallbackMethod = "fallback1") User getUserById(String id) { throw new RuntimeException("getUserById command failed"); } @HystrixCommand(fallbackMethod = "fallback2") User fallback1(String id, Throwable e) { assert "getUserById command failed".equals(e.getMessage()); throw new RuntimeException("fallback1 failed"); } @HystrixCommand(fallbackMethod = "fallback3") User fallback2(String id) { throw new RuntimeException("fallback2 failed"); } @HystrixCommand(fallbackMethod = "staticFallback") User fallback3(String id, Throwable e) { assert "fallback2 failed".equals(e.getMessage()); throw new RuntimeException("fallback3 failed"); } User staticFallback(String id, Throwable e) { assert "fallback3 failed".equals(e.getMessage()); return new User("def", "def"); } // test @Test public void test() { assertEquals("def", getUserById("1").getName()); }
转载地址:http://pkjdi.baihongyu.com/