本文节选自《疯狂Spring Cloud微服务架构实战》html
京东购买地址:https://item.jd.com/12256011.htmljava
当当网购买地址:http://product.dangdang.com/25201393.htmlgit
Spring Cloud教学视频:https://my.oschina.net/JavaLaw/blog/1552993github
使用Hystrix时,能够为命令设置属性,如下的代码片段,为一个命令设置了执行的超时时间:架构
public MyCommand(boolean isTimeout) { super( Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() .withExecutionTimeoutInMilliseconds(500)) ); }
以上的配置仅对该命令生效,设置了命令的超时时间为500毫秒,该配置项的默认值为1秒,若是想对全局生效,可使用如下的代码片段:微服务
ConfigurationManager .getConfigInstance() .setProperty( "hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds", 500);
以上的代码片段,一样设置了命令超时时间为500毫秒,但对全局有效。除了超时的配置外,还须要了解一下命令的相关名称,能够为命令设置如下名称:测试
如下的代码片段,分别设置以上的3个Key:ui
public RunCommand(String msg) { super( Setter.withGroupKey( HystrixCommandGroupKey.Factory.asKey("group-key")) .andCommandKey(HystrixCommandKey.Factory.asKey("command-key")) .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("pool-key")) ); }
Hystrix的配置众多,后面章节的案例会涉及部分的配置,读者若是想了解更多的配置,可到如下的地址查看:https://github.com/Netflix/Hystrix/wiki/Configurationspa
根据前面章节的流程图可知,至少会有3种状况触发回退(fallback):.net
在命令中,实现父类(HystrixCommand)的getFallback()方法,便可实现回退,当以上的状况发生时,将会执行回退方法。前面的例子中,已经展现了“执行命令失败”的回退,下面测试一下断路器被打开时的回退,详细请见代码清单6-7。
代码清单6-7:
06\6.2\first-hnystrix-client\src\main\java\org\crazyit\cloud\fallback\FallbackTest.java
public class FallbackTest { public static void main(String[] args) { // 断路器被强制打开 ConfigurationManager.getConfigInstance().setProperty( "hystrix.command.default.circuitBreaker.forceOpen", "true"); FallbackCommand c = new FallbackCommand(); c.execute(); // 建立第二个命令,断路器关闭 ConfigurationManager.getConfigInstance().setProperty( "hystrix.command.default.circuitBreaker.forceOpen", "false"); FallbackCommand c2 = new FallbackCommand(); c2.execute(); } static class FallbackCommand extends HystrixCommand<String> { public FallbackCommand() { super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")); } /** * 断路器被强制打开,该方法不会执行 */ protected String run() throws Exception { System.out.println("命令执行"); return ""; } /** * 回退方法,断路器打开后会执行回退 */ protected String getFallback() { System.out.println("执行回退方法"); return "fallback"; } } }
若是让断路器打开,须要符合必定的条件,本例为了简单起见,在代码清单中,使用了配置管理类(ConfigurationManager)将断路器强制打开与关闭,在打开断路器后,FallbackCommand总会执行回退(getFallback)方法,将断路器关闭,命令执行正常。若是断路器被打开,而命令中没有提供回退方法,将抛出如下异常:
com.netflix.hystrix.exception.HystrixRuntimeException: FallbackCommand short-circuited and no fallback available.
另外,须要注意的是,命令执行后,无论是否会触发回退,都会去计算整个链路的健康情况,根据健康情况来判断是否要打开断路器,若是命令仅仅失败了一次,是不足以打开断路器的,关于断路器的逻辑将在后面章节讲述。
Hystrix的回退机制比较灵活,你能够在A命令的回退方法中执行B命令,若是B命令也执行失败,一样也会触发B命令的回退,这样就造成一种链式的命令执行,例如如下代码片段:
static class CommandA extends HystrixCommand<String> { …省略其余代码 protected String run() throws Exception { throw new RuntimeException(); } protected String getFallback() { return new CommandB().execute(); } }
还有其余较为复杂的例子,例如银行转帐,假设一个转帐命令包含调用A银行扣款、B银行加款两个命令,其中一个命令失败后,再执行转帐命令的回退,如图6-4所示。
图6-4 多命令回退
要作到图6-4的多命令只执行一次回退的效果,CommandA与CommandB,不能有回退方法,若是CommandA命令执行失败,而且该命令有回退方法,此时将不会执行“MainCommand”的回退方法。除了上面所提到的链式的回退以及多命令回退,读者还能够根据实际状况来设计回退。
本文节选自《疯狂Spring Cloud微服务架构实战》
Spring Cloud教学视频:https://my.oschina.net/JavaLaw/blog/1552993
本书代码共享地址:https://gitee.com/yangenxiong/SpringCloud