Spring Cloud Hystrix异常处理方法详解
2020-02-13 10:34
1511 查看
这篇文章主要介绍了Spring Cloud Hystrix异常处理方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在调用服务执行HsytrixCommand实现的run()方法抛出异常时,除HystrixBadRequestException之外,其他异常都会认为是Hystrix命令执行失败并触发服务降级处理逻辑.
异常处理
当Hystrix命令因为异常(除了HystrixBadRequestException异常)进入服务降级逻辑之后,往往需要对不同的异常做针对处理,那么就要捕获异常。对于使用@HystrixCommand注解只需要在降级函数中增加Throwable e对象的定义
/** * HystrixBadRequestException: * 与由HystrixCommand抛出的所有其他异常不同,这不会触发回退,也不会对失败度量进行计数,因此不会触发断路器。 * @return */ @HystrixCommand(fallbackMethod="helloBackMethodFirst",ignoreExceptions=HystrixBadRequestException.class) public String helloService() { logger.info("start invoke service"); //URI需要使用虚拟主机名(即服务名称,而不是主机名) //return restTemplate.getForEntity("http://service-provide/hello", String.class).getBody(); throw new RuntimeException("consumer exception"); } /** * 通用降级函数 * @return */ @HystrixCommand(fallbackMethod="helloBackMethodSecond") public String helloBackMethodFirst(Throwable e){ /* * 一些异常判断 * if(e instanceof CheckEception){ * } * if(e instanceof IllegalStateException){ * } */ //此处可能是另外一个网络请求,所以也可能出现错误 return "error1:"+e.getMessage(); }
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
- SpringCloud之熔断监控Hystrix Dashboard的实现
- SpringCloud之熔断器Hystrix的实现
- SpringCloud微服务之Hystrix组件实现服务熔断的方法
- SpringCloud中的断路器(Hystrix)和断路器监控(Dashboard)
- 详解springcloud 基于feign的服务接口的统一hystrix降级处理
- springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)
- springcloud使用Hystrix进行微服务降级管理
- SpringCloud Zuul在何种情况下使用Hystrix及问题小结
- Spring Cloud Hystrix入门和Hystrix命令原理分析
- SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解决方案
相关文章推荐
- Spring Cloud Gateway全局异常处理的方法详解
- 详解springcloud 基于feign的服务接口的统一hystrix降级处理
- 笔记:Spring Cloud Hystrix 异常处理、缓存和请求合并
- Spring Cloud zuul自定义统一异常处理实现方法
- Spring Cloud Config对特殊字符加密处理的方法详解
- 详解SpringCloud Finchley Gateway 统一异常处理
- Spring-JdbcTemplate的queryForObject方法EmptyResultDataAccessException异常产生的原理及处理方法
- SpringBoot框架中REST接口的异常处理方法
- Spring boot项目中异常拦截设计和处理详解
- spring的事务处理详解:调用一个方法前的事务处理过程(源代码分析)
- Spring Boot & Spring MVC 异常处理的N种方法
- 详解spring cloud hystrix请求缓存(request cache)
- SpringBoot错误处理机制以及自定义异常处理详解
- Spring访问数据库异常的处理方法(转)
- Spring Cloud实战小贴士:Zuul统一异常处理(二)
- 微服务之springCloud-hystrix参数详解(八)
- Spring Boot中使用AOP统一处理web层异常的方法
- 详解Spring Cloud Finchley版中Consul多实例注册的问题处理
- 详解spring cloud hystrix 请求合并collapsing
- Spring Cloud微服务如何设计异常处理机制?