异常的传播和捕获

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

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

package org.hope.hystrix.example.exception;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service; @Service
public class HystrixHandleException {
/**
* 当设置ignoreExceptions参数时,
* 抛出对应的异常就不会触发降级(也就是不会调用failMethod()方法).
*/
@HystrixCommand(
ignoreExceptions = {NullPointerException.class, ArithmeticException.class},
fallbackMethod = "failMethod"
)
public String getUserName(Long id) {
Long re = id/0; //会抛ArithmeticException
String param = null;
param.trim(); // 此处会抛NullPointException
return "张三";
} private String failMethod(Long id, Throwable e) {
return e.getMessage();
} }

单元测试:

package org.hope.hystrix.example.exception;

import org.hope.hystrix.example.HystrixApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = HystrixApplication.class)
public class HystrixHandleExceptionTest { @Autowired
private HystrixHandleException hystrixHandleException;
@Test
public void test() {
String name = hystrixHandleException.getUserName(10L);
System.out.println("==================" + name);
} }

运行结果

java.lang.ArithmeticException: / by zero

    at org.hope.hystrix.example.exception.HystrixHandleException.getUserName(HystrixHandleException.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute(MethodExecutionAction.java:116)
at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.executeWithArgs(MethodExecutionAction.java:93)
at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute(MethodExecutionAction.java:78)
at com.netflix.hystrix.contrib.javanica.command.GenericCommand$1.execute(GenericCommand.java:48)
at com.netflix.hystrix.contrib.javanica.command.AbstractHystrixCommand.process(AbstractHystrixCommand.java:145)
at com.netflix.hystrix.contrib.javanica.command.GenericCommand.run(GenericCommand.java:45)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298)
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. js 闭包的用法详解

    一.闭包 实现可重用的局部变量,且保护其不受污染的机制. 外层函数包裹受保护的变量和内层函数. 内层函数专门负责操作外层函数的局部变量. 将内层函数返回到外层函数外部,反复调用. 二.作用域 子函数会 ...

  2. Go从三个站点中返回响应最快的

    利用协程可以轻松实现 package main import ( "fmt" "github.com/imroc/req" ) func mirroredQue ...

  3. Python模块学习------ 多线程threading(1)

    # Method 1: 创建一个Thread实例,传给它一个函数:import threading from time import sleep, ctime loops = [4,2] def lo ...

  4. [python]使用xlrd对Excel表格进行读写操作

    2.1 导入模块 import xlrd 2.2 打开Excel文件读取数据 data = xlrd.open_workbook("excelFile.xls") 2.3 使用技巧 ...

  5. CSS中的块级元素(block)与行内元素(inline)

    css中有3种基本的定位机制:普通流(相对定位实际上看做普通流定位模型的一部分)浮动(float)绝对定位(固定定位是绝对定位的一种)所以在学习浮动之前,我们先要了解块级元素与内联元素(行内元素).块 ...

  6. .NET(c#) 移动开发平台 - Smobiler(1)

    如果说基于.net的移动开发平台,目前比较流行的可能是xamarin了,不过除了这个,还有一个比xamarin更好用的国内的.net移动开发平台,smobiler,不用学习另外一套开发模式或者搭建复杂 ...

  7. js 前端操作的分页路由设计

    //分页条获得分页数字,然后跳转到拼接字符串的页面 function getPage(page) { var window_href = location.pathname; var newWindo ...

  8. SPI通讯协议

    一.SPI概述 SPI,是英语Serial Peripheral Interface的缩写,顾名思义就是串行外围设备接口.SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线, ...

  9. 关于React中状态保存的研究

    在使用react搭配react-router做应用的时候,你可能遇到这样的问题,当我从第一个页面过渡到第二个页面,然后返回之后,发现之前的页面的状态全部不见了,即回到了初始的状态. 这点在页面存在多个 ...

  10. python调用metasploit里的MS-17-010模块进行漏洞攻击

    起因:看各位大佬们写的shellcode厉害的一匹,可惜自己没学C和汇编 也看不懂shellcode,只能写一个调用metasploit里的模块进行攻击了. 0x01 攻击机:192.168.223. ...