2017-05-29 53 views

回答

0

使用ignoreExceptions注释PARAM

@HystrixCommand(ignoreExceptions = { BaseException.class, MissingServletRequestParameterException.class, TypeMismatchException.class }) 

https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#error-propagation

我看你延长HystrixCommand,而不是使用注释,但是,这并不重要,只是设置,在命令属性和它应该有相同的效果。

不幸的是,Hystrix命令是通过Builder模式创建的,所以你将不得不做一些黑客攻击。 ignoreExceptions被添加到DefaultProperties.java中,在HystrixCommandBuilder中使用

+0

根据我的理解。在HystrixCommandBuilder的ignoreexceptions方法中,他只是复制例外列表。但根据方法评论,我明白这些被忽略的异常可以包含在Hystrixbadrequestexception中。但是,你能让我知道如何做到这一点,并以更好的方式用代码片段 – LazyGuy

+0

“只需在命令中设置该属性” - “HystrixCommand”中没有这样的属性 –

0

如果将您的逻辑封装在try/catch中并在HystrixBadRequestException中重新抛出任何异常,那么它将不会触发回退。

@Override 
protected Object run() throws Exception { 
    try { 
     return //call goes here 
    } 
    catch (Throwable e) { 
     //We wrap any exceptions in a HystrixBadRequestException because this way any other errors will not 
     //trip the short circuit 
     throw new HystrixBadRequestException("Exception thrown hystrix call", e); 
    } 
} 

从文档: http://netflix.github.io/Hystrix/javadoc/com/netflix/hystrix/exception/HystrixBadRequestException.html

表示与提供的参数或状态,而不是执行失败的错误的异常。 与HystrixCommand引发的所有其他异常不同,这不会触发故障预测,不会计入故障指标,因此不会触发断路器。

注意:只有当错误是由于用户输入引起的,例如IllegalArgumentException,否则它就会失败以达到容错和回退行为的目的。