spring cloud:Edgware.RELEASE版本中zuul回退方法的变化

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();
        //输出详细的回退原因
        ...
}
posted @ 2017-12-23 15:06  菩提树下的杨过  阅读(2106)  评论(0编辑  收藏  举报