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

雨燕双飞 提交于 2019-12-01 12:12:46

目录

  • 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!