日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

zuul转发的一些常见异常

發布時間:2023/12/4 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 zuul转发的一些常见异常 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

##序 使用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:

@Overridepublic 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<String, Object> 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<String, Object> 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/994069

總結

以上是生活随笔為你收集整理的zuul转发的一些常见异常的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。