教程:一块儿学习Hystrix--Hystrix经常使用场景--失败

目录

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

Hystrix本系列博文

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

     点击查看 Hystrix入门java

    点击查看 Hystrix命令执行git

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

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

    点击查看 Hystrix命令名称、Hystrix请求处理单元测试

快速失败

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

就像下面示例同样:this

// 转帖请注明原贴地址: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";
        }
    }
}

点击查看完整代码spa

以上代码的单元测试以下:.net

// 转帖请注明原贴地址: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

相关文章
相关标签/搜索