我們通過直接擴展HystrixCommand類來使用hystrix功能。 但是對於一些業務異常,hystrix的回退方法正在被觸發。如何說hystrix不觸發hystrix命令中某些例外的後備
我不想觸發某些業務特定異常的hystrix後退。如何在沒有註釋的情況下實現它?
在此先感謝..!
我們通過直接擴展HystrixCommand類來使用hystrix功能。 但是對於一些業務異常,hystrix的回退方法正在被觸發。如何說hystrix不觸發hystrix命令中某些例外的後備
我不想觸發某些業務特定異常的hystrix後退。如何在沒有註釋的情況下實現它?
在此先感謝..!
使用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中使用
如果將您的邏輯封裝在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);
}
}
表示與提供的參數或狀態,而不是執行失敗的錯誤的異常。 與HystrixCommand引發的所有其他異常不同,這不會觸發故障預測,不會計入故障指標,因此不會觸發斷路器。
注意:只有當錯誤是由於用戶輸入引起的,例如IllegalArgumentException,否則它就會失敗以達到容錯和回退行爲的目的。
根據我的理解。在HystrixCommandBuilder的ignoreexceptions方法中,他只是複製例外列表。但根據方法評論,我明白這些被忽略的異常可以包含在Hystrixbadrequestexception中。但是,你能讓我知道如何做到這一點,並以更好的方式用代碼片段 – LazyGuy
「只需在命令中設置該屬性」 - 「HystrixCommand」中沒有這樣的屬性 –