Spring Cloud Gateway全局通用異常處理的實(shí)現(xiàn)
為什么需要全局異常處理
在傳統(tǒng) Spring Boot 應(yīng)用中, 我們 @ControllerAdvice 來(lái)處理全局的異常,進(jìn)行統(tǒng)一包裝返回
// 摘至 spring cloud alibaba console 模塊處理@ControllerAdvicepublic class ConsoleExceptionHandler { @ExceptionHandler(AccessException.class) private ResponseEntity<String> handleAccessException(AccessException e) { return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrMsg()); }}
例如: ③ 處應(yīng)用調(diào)用數(shù)據(jù)庫(kù)異常,通過(guò) @ControllerAdvice 包裝異常請(qǐng)求響應(yīng)給客戶端
但在微服務(wù)架構(gòu)下, 例如 ② 處 網(wǎng)關(guān)調(diào)用業(yè)務(wù)微服務(wù)失敗(轉(zhuǎn)發(fā)失敗、調(diào)用異常、轉(zhuǎn)發(fā)失敗),在應(yīng)用設(shè)置的 @ControllerAdvice 將失效,因?yàn)榱髁扛緵](méi)有轉(zhuǎn)發(fā)到應(yīng)用上處理。
如上圖: 模擬所有路由斷言都不匹配 404 , 和 spring boot 默認(rèn)保持一致的錯(cuò)誤輸出頁(yè)面。 顯然我們?cè)诰W(wǎng)關(guān)同樣配置 @ControllerAdvice 是不能解決問(wèn)題,因?yàn)?spring cloud gateway 是基于 webflux 反應(yīng)式編程。
解決方法
默認(rèn)處理流程
ExceptionHandlingWebHandler 作為 spring cloud gateway 最核心 WebHandler 的一部分會(huì)進(jìn)行異常處理的過(guò)濾
public class ExceptionHandlingWebHandler extends WebHandlerDecorator { @Override public Mono<Void> handle(ServerWebExchange exchange) { Mono<Void> completion; try { completion = super.handle(exchange); } catch (Throwable ex) { completion = Mono.error(ex); } // 獲取全局的 WebExceptionHandler 執(zhí)行 for (WebExceptionHandler handler : this.exceptionHandlers) { completion = completion.onErrorResume(ex -> handler.handle(exchange, ex)); } return completion; }}
默認(rèn)實(shí)現(xiàn) DefaultErrorWebExceptionHandler
public class DefaultErrorWebExceptionHandler { @Override protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) { // 根據(jù)客戶端 `accpet` 請(qǐng)求頭決定返回什么資源,如上瀏覽器返回的是 頁(yè)面 return route(acceptsTextHtml(), this::renderErrorView).andRoute(all(), this::renderErrorResponse); }}
// 模擬指定 `accpet` 情況curl --location --request GET ’http://localhost:9999/adminx/xx’ 18:09:23 --header ’Accept: application/json’{'timestamp':'2020-05-24 18:09:24','path':'/adminx/xx','status':404,'error':'Not Found','message':null,'requestId':'083c48e3-2'}⏎
重寫(xiě) ErrorWebExceptionHandler
/** * @author lengleng * @date 2020/5/23 * <p> * 網(wǎng)關(guān)異常通用處理器,只作用在webflux 環(huán)境下 , 優(yōu)先級(jí)低于 {@link ResponseStatusExceptionHandler} 執(zhí)行 */@Slf4j@Order(-1)@RequiredArgsConstructorpublic class GlobalExceptionConfiguration implements ErrorWebExceptionHandler { private final ObjectMapper objectMapper; @Override public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) { ServerHttpResponse response = exchange.getResponse(); if (response.isCommitted()) { return Mono.error(ex); } // header set response.getHeaders().setContentType(MediaType.APPLICATION_JSON); if (ex instanceof ResponseStatusException) { response.setStatusCode(((ResponseStatusException) ex).getStatus()); } return response.writeWith(Mono.fromSupplier(() -> { DataBufferFactory bufferFactory = response.bufferFactory(); try { return bufferFactory.wrap(objectMapper.writeValueAsBytes(R.failed(ex.getMessage()))); } catch (JsonProcessingException e) { log.warn('Error writing response', ex); return bufferFactory.wrap(new byte[0]); }})); }}
總結(jié)
重寫(xiě)的 DefaultErrorWebExceptionHandler 優(yōu)先級(jí)一定要小于內(nèi)置 ResponseStatusExceptionHandler 經(jīng)過(guò)它處理的獲取對(duì)應(yīng)錯(cuò)誤類(lèi)的 響應(yīng)碼 其他擴(kuò)展 可以參考 SentinelBlockExceptionHandler sentinel 整合網(wǎng)關(guān)的處理,不過(guò)整體和默認(rèn)的異常處理沒(méi)有什么區(qū)別 基礎(chǔ)環(huán)境說(shuō)明:Spring Cloud Hoxton.SR4 & Spring Boot 2.3.0 具體實(shí)現(xiàn)代碼參考:https://gitee.com/log4j/pig到此這篇關(guān)于Spring Cloud Gateway全局通用異常處理的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring Cloud Gateway全局異常內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. phpstudy apache開(kāi)啟ssi使用詳解2. uniapp解決軟鍵盤(pán)彈出問(wèn)題方法詳解3. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器5. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令6. ASP.NET MVC使用異步Action的方法7. jsp文件下載功能實(shí)現(xiàn)代碼8. 詳解瀏覽器的緩存機(jī)制9. 爬取今日頭條Ajax請(qǐng)求10. AJAX的跨域問(wèn)題解決方案
