异常的传播和捕获

  传播:在HystrixCommand实现的run()方法中跑出异常时,除了HystrixBadRequestException之外,其他异常均会被Hystrix认为命令执行失败并处罚服务降级的处理逻辑。下面的例子通过@HystrixCommand注解的ignoreException参数来设置。

  捕获:我们可以在降级方法中添加Throwable参数来捕获异常。

  1. package org.hope.hystrix.example.exception;
  2.  
  3. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  4. import org.springframework.stereotype.Service;
  5.  
  6. @Service
  7. public class HystrixHandleException {
  8. /**
  9. * 当设置ignoreExceptions参数时,
  10. * 抛出对应的异常就不会触发降级(也就是不会调用failMethod()方法).
  11. */
  12. @HystrixCommand(
  13. ignoreExceptions = {NullPointerException.class, ArithmeticException.class},
  14. fallbackMethod = "failMethod"
  15. )
  16. public String getUserName(Long id) {
  17. Long re = id/0; //会抛ArithmeticException
  18. String param = null;
  19. param.trim(); // 此处会抛NullPointException
  20. return "张三";
  21. }
  22.  
  23. private String failMethod(Long id, Throwable e) {
  24. return e.getMessage();
  25. }
  26.  
  27. }

单元测试:

  1. package org.hope.hystrix.example.exception;
  2.  
  3. import org.hope.hystrix.example.HystrixApplication;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9.  
  10. @RunWith(SpringJUnit4ClassRunner.class)
  11. @SpringBootTest(classes = HystrixApplication.class)
  12. public class HystrixHandleExceptionTest {
  13.  
  14. @Autowired
  15. private HystrixHandleException hystrixHandleException;
  16. @Test
  17. public void test() {
  18. String name = hystrixHandleException.getUserName(10L);
  19. System.out.println("==================" + name);
  20. }
  21.  
  22. }

运行结果

  1. java.lang.ArithmeticException: / by zero
  2.  
  3. at org.hope.hystrix.example.exception.HystrixHandleException.getUserName(HystrixHandleException.java:20)
  4. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  5. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  6. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  7. at java.lang.reflect.Method.invoke(Method.java:498)
  8. at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute(MethodExecutionAction.java:116)
  9. at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.executeWithArgs(MethodExecutionAction.java:93)
  10. at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute(MethodExecutionAction.java:78)
  11. at com.netflix.hystrix.contrib.javanica.command.GenericCommand$1.execute(GenericCommand.java:48)
  12. at com.netflix.hystrix.contrib.javanica.command.AbstractHystrixCommand.process(AbstractHystrixCommand.java:145)
  13. at com.netflix.hystrix.contrib.javanica.command.GenericCommand.run(GenericCommand.java:45)
  14. at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302)
  15. at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298)
  16. at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)

参考:

[1]Github,https://github.com/Netflix/Hystrix/wiki/How-it-Works

[2] 《SpringCloud微服务实战》,电子工业出版社,翟永超

Hystrix-异常处理的更多相关文章

  1. 笔记:Spring Cloud Hystrix 异常处理、缓存和请求合并

    异常处理 在 HystrixCommand 实现的run方法中抛出异常,除了 HystrixBadRequestException之外,其他异常均会被Hystrix 认为命令执行失败并触发服务降级处理 ...

  2. Spring Cloud 微服务笔记(六)Spring Cloud Hystrix

    Spring Cloud Hystrix Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力. 一.快速入门 1)依赖: <dep ...

  3. 在Feign中添加自定义配置

    首先先创建一个FeignConfig类,代码如下: package com.xing.config; import org.springframework.context.annotation.Bea ...

  4. Spring Cloud Feign+Hystrix自定义异常处理

    开启Hystrix spring-cloud-dependencies Dalston版本之后,默认Feign对Hystrix的支持默认是关闭的,需要手动开启. feign.hystrix.enabl ...

  5. hystrix源码小贴士之调用异常处理

    executeCommandAndObserve方法处理onerror异常. return execution.doOnNext(markEmits) .doOnCompleted(markOnCom ...

  6. SpringCloud学习之Zuul统一异常处理及回退

    一.Filter中统一异常处理 其实在SpringCloud的Edgware SR2版本中对于ZuulFilter中的错误有统一的处理,但是在实际开发当中对于错误的响应方式,我想每个团队都有自己的处理 ...

  7. Spring Cloud微服务如何设计异常处理机制?

    导读 今天和大家聊一下在采用Spring Cloud进行微服务架构设计时,微服务之间调用时异常处理机制应该如何设计的问题.我们知道在进行微服务架构设计时,一个微服务一般来说不可避免地会同时面向内部和外 ...

  8. Hystrix入门与分析(一):初识Hystrix

    在以前的文章中,我们介绍过使用Gauva实现限流的功能,现在我们来了解一下如何在服务框架中实现熔断和降级的方法. 简介Hystrix 大型系统架构的演进基本上都是这样一个方向:从单体应用到分布式架构. ...

  9. java框架之SpringCloud(5)-Hystrix服务熔断、降级与监控

    前言 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败.不做任何处理的情况下,很容易导致服务雪崩. 服务雪崩:多个微服务之间调用的时候,假设 ...

  10. Spring Cloud 入门 之 Hystrix 篇(四)

    原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...

随机推荐

  1. iOS微信运动 刷分

    修改 iOS微信运动的数据  很简单,这里记录下实现步骤. 首先要安装Theos,具体安装步骤就不说了.网上很多. 大体安装步骤: sudo brew install dpkg sudo brew i ...

  2. android开发遇到的问题

    1.虚拟机运行出下面的错Failed to allocate memory: 8 Failed to allocate memory: 8This application has requested ...

  3. springboot之集成mybatis mongo shiro druid redis jsp

    闲来无事,研究一下spingboot  发现好多地方都不一样了,第一个就是官方默认不支持jsp  于是开始狂找资料  终于让我找到了 首先引入依赖如下: <!-- tomcat的支持.--> ...

  4. Array对象的方法详情

    题外话:从事前端开发有很长一段时间了,一直在不断的扩充各种框架的学习,总觉得要学的东西好多,但是技能并没有得到很大的提升,后发现自己一味去追求的它的广度,并没用去深究其深度,所以决定打算从零开始,从最 ...

  5. ubuntu 使用sudo apt-get update 出现 被配置多次导致无法升级错误解决方法

    这个周六周末在考虑升级自己GPU开发机,在琢磨使用docker来配置tensorflowGPU环境,在升级软件的时候爆出了如下错误 在 /etc/apt/sources.list.d/sogoupin ...

  6. orm查询

    all:models.表名.objects.all() 结果是queryset集合 filter: models.表名.objects.filter() 结果是queryset集合 get: mode ...

  7. vue-cli完整地引入element-ui

    1因为该组件会依赖于jQuery,所以先安装jQuery所需依赖: 进入npm控制台,输入指令: cnpm install jquery --save-dev 然后在入口文件main.js中引入: i ...

  8. go golang 判断base64数据 获取随机字符串 截取字符串

    go golang 判断base64数据 获取随机字符串 截取字符串 先少写点,占个坑,以后接着加. 1,获取指定长度随机字符串 func RandomDigits(length int) strin ...

  9. 最小生成树&最短路基础算法总结

    [最短路问题] 解决最短路问题有以下算法:Dijkstra算法,Bellman-Ford算法,Floyd算法,和SPFA算法和启发式搜索算法A*; 每个算法都有它的特点可以解决某些特定的问题,例如:F ...

  10. See you~(二维树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1892 See you~ Time Limit: 5000/3000 MS (Java/Others)  ...