一起学习Hystrix--Hystrix常用场景--失败


声明:本文转载自https://my.oschina.net/u/2342969/blog/1817652,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

目录

  • Hystrix本系列博文
  • 快速失败
  • 静默失败
  • 声明

Hystrix本系列博文

    以下为博主写Hystrix系列的文章列表

     点击查看 Hystrix入门

    点击查看 Hystrix命令执行

    点击查看 Hystrix处理异常机制(降级方法)

    点击查看 Hystrix命令名称、分组、线程池

    点击查看 Hystrix命令名称、Hystrix请求处理

快速失败

    最基本的操作是仅仅执行一个操作,没有回退或者降级方案。如果出现异常,则直接抛出一个异常。

就像下面示例一样:

// 转帖请注明原贴地址:https://my.oschina.net/u/2342969/blog/1817652 public class HystrixFailsFast extends HystrixCommand<String> {      private final boolean throwException;      public HystrixFailsFast(boolean throwException) {         super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));         this.throwException = throwException;     }      @Override     protected String run() {         if (throwException) {             throw new RuntimeException("failure from HystrixFailsFast");         } else {             return "success";         }     } }

点击查看完整代码

以上代码的单元测试如下:

// 转帖请注明原贴地址:https://my.oschina.net/u/2342969/blog/1817652         @Test         public void testSuccess() {             // 如果不抛异常,则返回成功的字符串             assertEquals("success", new HystrixFailsFast(false).execute());         }          @Test         public void testFailure() {             try {                 // 程序抛异常                 new HystrixFailsFast(true).execute();                 // 判断异常情况                 fail("we should have thrown an exception");             } catch (HystrixRuntimeException e) {                 // 抓获异常,断言异常信息                 assertEquals("failure from HystrixFailsFast", e.getCause().getMessage());                 e.printStackTrace();             }         }

HystrixObservableCommand 等价

    对于 HystrixObservableCommand 的快速失败解决方案是调用重写 resumeWithFallback 方法,示例如下:

// 转帖请注明原贴地址:https://my.oschina.net/u/2342969/blog/1817652        @Override     protected Observable<String> resumeWithFallback() {         if (throwException) {             return Observable.error(new Throwable("failure from CommandThatFailsFast"));         } else {             return Observable.just("success");         }     }

静默失败

    静默失败相当于返回空响应或删除功能(这是与静态降级的区别)。它可以通过返回null、空Map、空List或其他此类响应来完成。 HystrixCommand 实例下,可以通过实现一个 getFallback() 方法,示例如下:

(执行示意图)

// 转帖请注明原贴地址:https://my.oschina.net/u/2342969/blog/1817652 public class HystrixFailsSilently extends HystrixCommand<List<String>> {      private final boolean throwException;      public HystrixFailsSilently(boolean throwException) {         super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));         this.throwException = throwException;     }      @Override     protected List<String> run() {         if (throwException) {             // 模拟出现异常,以便触发降级逻辑             throw new RuntimeException("failure from HystrixFailsSilently");         } else {             // 模拟正常逻辑             ArrayList<String> values = new ArrayList<String>();             values.add("success");             return values;         }     }      @Override     protected List<String> getFallback() {         // 触发降级,返回空List         return Collections.emptyList();     } }

点击查看完整源码

        // 转帖请注明原贴地址:https://my.oschina.net/u/2342969/blog/1817652         @Test         public void testSuccess() {             // 单元测试正常逻辑执行             assertEquals("success", new HystrixFailsSilently(false).execute().get(0));         }          @Test         public void testFailure() {             try {                 // 单元测试异常逻辑,返回list元素个数                 assertEquals(0, new HystrixFailsSilently(true).execute().size());             } catch (HystrixRuntimeException e) {                 fail("we should not get an exception as we fail silently with a fallback");             }         }

HystrixObservableCommand 等价

     对于 HystrixObservableCommand 的静默失败解决方案是调用重写 resumeWithFallback() 方法,示例如下:

// 转帖请注明原贴地址:https://my.oschina.net/u/2342969/blog/1817652     @Override     protected Observable<String> resumeWithFallback() {         return Observable.empty();     }

声明

    转帖请注明原贴地址:https://my.oschina.net/u/2342969/blog/1817652

本文发表于2018年05月24日 00:00
(c)注:本文转载自https://my.oschina.net/u/2342969/blog/1817652,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1756 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1