Hystrix-异常处理
异常的传播和捕获
传播:在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-异常处理的更多相关文章
- 笔记:Spring Cloud Hystrix 异常处理、缓存和请求合并
异常处理 在 HystrixCommand 实现的run方法中抛出异常,除了 HystrixBadRequestException之外,其他异常均会被Hystrix 认为命令执行失败并触发服务降级处理 ...
- Spring Cloud 微服务笔记(六)Spring Cloud Hystrix
Spring Cloud Hystrix Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力. 一.快速入门 1)依赖: <dep ...
- 在Feign中添加自定义配置
首先先创建一个FeignConfig类,代码如下: package com.xing.config; import org.springframework.context.annotation.Bea ...
- Spring Cloud Feign+Hystrix自定义异常处理
开启Hystrix spring-cloud-dependencies Dalston版本之后,默认Feign对Hystrix的支持默认是关闭的,需要手动开启. feign.hystrix.enabl ...
- hystrix源码小贴士之调用异常处理
executeCommandAndObserve方法处理onerror异常. return execution.doOnNext(markEmits) .doOnCompleted(markOnCom ...
- SpringCloud学习之Zuul统一异常处理及回退
一.Filter中统一异常处理 其实在SpringCloud的Edgware SR2版本中对于ZuulFilter中的错误有统一的处理,但是在实际开发当中对于错误的响应方式,我想每个团队都有自己的处理 ...
- Spring Cloud微服务如何设计异常处理机制?
导读 今天和大家聊一下在采用Spring Cloud进行微服务架构设计时,微服务之间调用时异常处理机制应该如何设计的问题.我们知道在进行微服务架构设计时,一个微服务一般来说不可避免地会同时面向内部和外 ...
- Hystrix入门与分析(一):初识Hystrix
在以前的文章中,我们介绍过使用Gauva实现限流的功能,现在我们来了解一下如何在服务框架中实现熔断和降级的方法. 简介Hystrix 大型系统架构的演进基本上都是这样一个方向:从单体应用到分布式架构. ...
- java框架之SpringCloud(5)-Hystrix服务熔断、降级与监控
前言 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败.不做任何处理的情况下,很容易导致服务雪崩. 服务雪崩:多个微服务之间调用的时候,假设 ...
- Spring Cloud 入门 之 Hystrix 篇(四)
原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...
随机推荐
- MicroPython可视化编程开发板—TurnipBit自制MP3教程实例
转载请以链接形式注明文章来源(MicroPythonQQ技术交流群:157816561,公众号:MicroPython玩家汇) 当前我们都生活在一个有声有色的社会当中,欣赏美丽的景色,享受动人的音乐, ...
- 搭建CAS服务器,并实现一个简单的单点登录的demo
官网:http://jasig.github.io/cas/Cas Server下载:http://developer.jasig.org/cas/Cas Client下载:http://develo ...
- [Spark内核] 第32课:Spark Worker原理和源码剖析解密:Worker工作流程图、Worker启动Driver源码解密、Worker启动Executor源码解密等
本課主題 Spark Worker 原理 Worker 启动 Driver 源码鉴赏 Worker 启动 Executor 源码鉴赏 Worker 与 Master 的交互关系 [引言部份:你希望读者 ...
- Java学习笔记5---命令行下用javac,java编译运行含package语句的类
对于笔记3中的HelloWorld程序,编译时只要输入javac HelloWorld.java即可生成类文件:再用java HelloWorld即可运行. 如果源程序使用了包声明,编译运行时要使用某 ...
- 【读书笔记】【深入理解ES6】#9-JavaScript中的类
大多数面向对象的编程语言都支持类和类继承的特性,而JavaScript却不支持这些特性,只能通过其他方法定义并关联多个相似的对象.这个状态一直从ECMAScript 1持续到ECMAScript 5. ...
- dos命令的小总结
DOS命令与批处理:目的:简单高效.为我们以后学习linux操作系统做准备进行DOS命令窗口: 运行---输入cmd主要包括目录操作类命令.磁盘操作类命令.文件操作类命令和其它命令 1.在d盘创建一个 ...
- 新浪新闻页面抓取(JAVA-Jsoup)
1.使用gradle建立工程: 工程格式如下: include ':spider-demo' rootProject.name = 'my-spider-demo' settings def void ...
- c/s与b/s 动态网站与静态网站 (网站编码统一“UTF-8”)
1.c/s和b/s 第一张图是b/s 可以随时随地的浏览 (在服务器增加网页就能增加功能,只要改变网页就能使用户同步更新,共享性也强,开发也简单,在广域网和局域网都能建造b/s结构,然后通过int ...
- CTF---编程入门第一题 循环
循环分值:10 来源: 北邮天枢战队 难度:易 参与人数:1478人 Get Flag:467人 答题人数:523人 解题通过率:89% 给出一个循环公式,对于一个整数n,当n为奇数时,n=3n+1, ...
- Java A+B(个人模版)
JavaA+B: import java.io.*; import java.util.*; import java.math.BigInteger; import java.util.Scanner ...