博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zuul转发的一些常见异常
阅读量:6978 次
发布时间:2019-06-27

本文共 5010 字,大约阅读时间需要 16 分钟。

  hot3.png

##序 使用zuul作为api网关的话,经常会碰见一些异常,这里小结一下。

##ZuulException 这个是最外层的异常

public class ZuulException extends Exception {    public int nStatusCode;    public String errorCause;    /**     * Source Throwable, message, status code and info about the cause     * @param throwable     * @param sMessage     * @param nStatusCode     * @param errorCause     */    public ZuulException(Throwable throwable, String sMessage, int nStatusCode, String errorCause) {        super(sMessage, throwable);        this.nStatusCode = nStatusCode;        this.errorCause = errorCause;        incrementCounter("ZUUL::EXCEPTION:" + errorCause + ":" + nStatusCode);    }    /**     * error message, status code and info about the cause     * @param sMessage     * @param nStatusCode     * @param errorCause     */    public ZuulException(String sMessage, int nStatusCode, String errorCause) {        super(sMessage);        this.nStatusCode = nStatusCode;        this.errorCause = errorCause;        incrementCounter("ZUUL::EXCEPTION:" + errorCause + ":" + nStatusCode);    }    /**     * Source Throwable,  status code and info about the cause     * @param throwable     * @param nStatusCode     * @param errorCause     */    public ZuulException(Throwable throwable, int nStatusCode, String errorCause) {        super(throwable.getMessage(), throwable);        this.nStatusCode = nStatusCode;        this.errorCause = errorCause;        incrementCounter("ZUUL::EXCEPTION:" + errorCause + ":" + nStatusCode);    }    private static final void incrementCounter(String name) {        CounterFactory.instance().increment(name);    }}

##RibbonRoutingFilter spring-cloud-netflix-core-1.2.6.RELEASE-sources.jar!/org/springframework/cloud/netflix/zuul/filters/route/RibbonRoutingFilter.java 这个类抛了很多ZuulException:

@Override	public Object run() {		RequestContext context = RequestContext.getCurrentContext();		this.helper.addIgnoredHeaders();		try {			RibbonCommandContext commandContext = buildCommandContext(context);			ClientHttpResponse response = forward(commandContext);			setResponse(response);			return response;		}		catch (ZuulException ex) {			context.set(ERROR_STATUS_CODE, ex.nStatusCode);			context.set("error.message", ex.errorCause);			context.set("error.exception", ex);		}		catch (Exception ex) {			context.set("error.status_code",					HttpServletResponse.SC_INTERNAL_SERVER_ERROR);			context.set("error.exception", ex);		}		return null;	}

###forward

protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {		Map
info = this.helper.debug(context.getMethod(), context.getUri(), context.getHeaders(), context.getParams(), context.getRequestEntity()); RibbonCommand command = this.ribbonCommandFactory.create(context); try { ClientHttpResponse response = command.execute(); this.helper.appendDebug(info, response.getStatusCode().value(), response.getHeaders()); return response; } catch (HystrixRuntimeException ex) { return handleException(info, ex); } }

这里有一个HystrixRuntimeException,主要是跟hystrix相关的,比如超时等。

###handleException

protected ClientHttpResponse handleException(Map
info, HystrixRuntimeException ex) throws ZuulException { int statusCode = HttpStatus.INTERNAL_SERVER_ERROR.value(); Throwable cause = ex; String message = ex.getFailureType().toString(); ClientException clientException = findClientException(ex); if (clientException == null) { clientException = findClientException(ex.getFallbackException()); } if (clientException != null) { if (clientException .getErrorType() == ClientException.ErrorType.SERVER_THROTTLED) { statusCode = HttpStatus.SERVICE_UNAVAILABLE.value(); } cause = clientException; message = clientException.getErrorType().toString(); } info.put("status", String.valueOf(statusCode)); throw new ZuulException(cause, "Forwarding error", statusCode, message); }

###findClientException

protected ClientException findClientException(Throwable t) {		if (t == null) {			return null;		}		if (t instanceof ClientException) {			return (ClientException) t;		}		return findClientException(t.getCause());	}

###com.netflix.client.ClientException

public class ClientException extends Exception{	/**	 * 	 */	private static final long serialVersionUID = -7697654244064441234L;		/**     * define your error codes here     *      */    public enum ErrorType{        GENERAL,         CONFIGURATION,         NUMBEROF_RETRIES_EXEEDED,         NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED,         SOCKET_TIMEOUT_EXCEPTION,         READ_TIMEOUT_EXCEPTION,        UNKNOWN_HOST_EXCEPTION,        CONNECT_EXCEPTION,        CLIENT_THROTTLED,        SERVER_THROTTLED,        NO_ROUTE_TO_HOST_EXCEPTION,        CACHE_MISSING;                static String getName(int errorCode){            if (ErrorType.values().length >= errorCode){                return ErrorType.values()[errorCode].name();            }else{                return "UNKNOWN ERROR CODE";            }        }    }}

转载于:https://my.oschina.net/go4it/blog/994077

你可能感兴趣的文章
Exceptions Interview Questions
查看>>
《少年派的奇幻漂流》观后感
查看>>
Extjs:添加查看全部按钮
查看>>
UNIX/Linux系统管理技术手册(3)----bash 数组和算术运算
查看>>
LINQ之路19:LINQ to XML之X-DOM更新、和Value属性交互
查看>>
笔记之远程桌面服务(RDS)
查看>>
怎样操作vue.js使用3DES加密
查看>>
js实现点击<li>标签弹出其索引值
查看>>
DIV限制宽度,字符断行,避免变形
查看>>
通过进程ID获得该进程主窗口的句柄
查看>>
快速把web项目部署到weblogic上
查看>>
.Net 文件流 System.IO之Stream
查看>>
Jmeter 笔记
查看>>
一个JS对话框,可以显示其它页面,
查看>>
IDEA ctrl+alt+L 格式化快捷键无效时解决
查看>>
前端小知识
查看>>
子弹实例化的代码
查看>>
URAL 2027 URCAPL, Episode 1 (模拟)
查看>>
hadoop install start-dfs.sh 失败
查看>>
各种小记
查看>>