Edgware.RELEASE以前的版本中,zuul網關中有一個ZuulFallbackProvider接口,代碼如下:
public interface ZuulFallbackProvider {
/**
* The route this fallback will be used for.
* @return The route the fallback will be used for.
*/
public String getRoute();
/**
* Provides a fallback response.
* @return The fallback response.
*/
public ClientHttpResponse fallbackResponse();
}
其中fallbackResponse()方法允許程序員在回退處理中重建輸出對象,通常是輸出“xxx服務不可用,請稍候重試”之類的提示,但是無法捕獲到更詳細的出錯信息,排錯很不方便。
估計spring-cloud團隊意識到了這個問題,在Edgware.RELEASE中將該接口標記為過時@Deprecated,同時在它下面派生出了一個新接口:
public interface FallbackProvider extends ZuulFallbackProvider {
/**
* Provides a fallback response based on the cause of the failed execution.
*
* @param cause cause of the main method failure
* @return the fallback response
*/
ClientHttpResponse fallbackResponse(Throwable cause);
}
提供了一個新的重載版本,把異常信息也當作參數傳進來了,這樣就友好多了,在處理回退時可以輸出更詳細的信息。參考下面的代碼:
if (cause != null && cause.getCause() != null) {
String reason = cause.getCause().getMessage();
//輸出詳細的回退原因
...
}
