2017-05-29 22 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,否則它就會失敗以達到容錯和回退行爲的目的。