Detailed explanation of spring cloud hystrix exception handling method
This article mainly introduces the detailed explanation of spring cloud hystrix exception handling methods. The example code is introduced in great detail, which has certain reference value for everyone's study or work. Friends in need can refer to it
When calling the run () method implemented by the hsytrix command to execute the service and throwing an exception, except for the hystrixbadrequestexception, other exceptions will be considered as the execution failure of the hystrix command and trigger the service degradation processing logic
exception handling
When the hystrix command enters the service degradation logic because of exceptions (except the hystrixbadrequestexception exception exception), it is often necessary to deal with different exceptions, so it is necessary to catch exceptions. For the use of @ hystrixcommand annotation, you only need to add the definition of throwable e object in the degradation function
/** * 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(); }
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.