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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloud Zuul(八)之ERROR Filter

發(fā)布時間:2023/12/3 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud Zuul(八)之ERROR Filter 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、ERROR Filter

錯誤過濾器用來處理zuul異常,一般使作為打印異常堆棧、跳轉(zhuǎn)異常頁面、轉(zhuǎn)換異常信息格式返回等操作。

Zuul已定義的錯誤過濾器SendErrorFilter,如果RequestContext.getThrowable()不為null,則轉(zhuǎn)發(fā)到/error(默認(rèn)情況下)。您可以通過設(shè)置error.path屬性來更改默認(rèn)轉(zhuǎn)發(fā)路徑(/error)。

?

二、自定義錯誤過濾器

本文自定義錯誤過濾器用來將json格式請求的異常信息轉(zhuǎn)換成json格式返回。

?

三、實現(xiàn)代碼

@Component @Slf4j public class ErrorFilter extends ZuulFilter {//按類型對過濾器進(jìn)行分類。Zuul中的標(biāo)準(zhǔn)類型是"pre"用于預(yù)路由篩選,"route"用于路由到原點,"post"用于后路由篩選,"error"用于錯誤處理。//我們還支持靜態(tài)響應(yīng)的"static"類型請參閱StaticResponseFilter。可以通過調(diào)用FilterProcessor.runFilters(type)//前置過濾器必須返回error@Overridepublic String filterType() {return FilterConstants.ERROR_TYPE;}//必須為過濾器定義filterOrder。如果優(yōu)先級對篩選器不重要,則過濾器可能具有相同的過濾器順序//過濾器順序不需要是連續(xù)的@Overridepublic int filterOrder() {return FilterConstants.SEND_ERROR_FILTER_ORDER - 10;}//默認(rèn)情況下,zuulfilter是靜態(tài)的;它們不攜帶狀態(tài)。這可以通過將isStaticFilter屬性重寫為false來重寫@Overridepublic boolean isStaticFilter() {return super.isStaticFilter();}//要禁用此篩選器的Archaius屬性的名稱。默認(rèn)情況下,它是zuul.[classname].[filtertype].disable@Overridepublic String disablePropertyName() {return super.disablePropertyName();}//如果為true,則過濾器已被archaius禁用,不會運(yùn)行@Overridepublic boolean isFilterDisabled() {return super.isFilterDisabled();}//此方法返回的"true"表示應(yīng)該調(diào)用run方法//如果應(yīng)該調(diào)用run方法,則返回true。false不會調(diào)用run方法@Overridepublic boolean shouldFilter() {RequestContext ctx = RequestContext.getCurrentContext();String contentType = ctx.getRequest().getContentType();if (contentType == null) {return true;}MediaType mediaType = MediaType.valueOf(contentType);if(mediaType == null){return true;}return MediaType.APPLICATION_JSON.includes(mediaType);}//如果shouldFilter方法為true,則將調(diào)用此方法。這種方法是ZuulFilter的核心方法//返回一些可以返回的任意工件。當(dāng)前的實現(xiàn)忽略了它。//如果在執(zhí)行期間發(fā)生錯誤,則引發(fā)ZuulException@Overridepublic Object run() throws ZuulException {RequestContext context = RequestContext.getCurrentContext();ZuulException exception = findZuulException(context.getThrowable());context.remove("throwable");//去掉已處理的錯誤信息try {HttpServletResponse response = context.getResponse();response.setContentType("application/json; charset=utf8");response.setStatus(exception.nStatusCode);PrintWriter writer = null;try {writer = response.getWriter();Map<String, Object> map = new HashMap<>();map.put("code", exception.nStatusCode);map.put("msg", exception.errorCause);map.put("detail", exception.getMessage());String retStr = JSON.toJSONString(map);writer.print(retStr);writer.flush();} catch (IOException e) {e.printStackTrace();} finally {if (writer != null) {writer.close();}}}catch (Exception e){log.error("error filter exception", e);}return null;}protected ZuulException findZuulException(Throwable throwable) {if (throwable.getCause() instanceof ZuulRuntimeException) {Throwable cause = null;if (throwable.getCause().getCause() != null) {cause = throwable.getCause().getCause().getCause();}if (cause instanceof ClientException && cause.getCause() != null&& cause.getCause().getCause() instanceof SocketTimeoutException) {ZuulException zuulException = new ZuulException("", 504,ZuulException.class.getName() + ": Hystrix Readed time out");return zuulException;}// this was a failure initiated by one of the local filtersif (throwable.getCause().getCause() instanceof ZuulException) {return (ZuulException) throwable.getCause().getCause();}}if (throwable.getCause() instanceof ZuulException) {// wrapped zuul exceptionreturn (ZuulException) throwable.getCause();}if (throwable instanceof ZuulException) {// exception thrown by zuul lifecyclereturn (ZuulException) throwable;}// fallbackreturn new ZuulException(throwable, HttpStatus.INTERNAL_SERVER_ERROR.value(), null);} }

?

總結(jié)

以上是生活随笔為你收集整理的SpringCloud Zuul(八)之ERROR Filter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。