亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring Cloud Gateway全局通用異常處理的實現

瀏覽:2日期:2023-09-04 13:24:50

為什么需要全局異常處理

在傳統 Spring Boot 應用中, 我們 @ControllerAdvice 來處理全局的異常,進行統一包裝返回

// 摘至 spring cloud alibaba console 模塊處理@ControllerAdvicepublic class ConsoleExceptionHandler { @ExceptionHandler(AccessException.class) private ResponseEntity<String> handleAccessException(AccessException e) { return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrMsg()); }}

例如: ③ 處應用調用數據庫異常,通過 @ControllerAdvice 包裝異常請求響應給客戶端

Spring Cloud Gateway全局通用異常處理的實現

但在微服務架構下, 例如 ② 處 網關調用業務微服務失敗(轉發失敗、調用異常、轉發失敗),在應用設置的 @ControllerAdvice 將失效,因為流量根本沒有轉發到應用上處理。

Spring Cloud Gateway全局通用異常處理的實現

如上圖: 模擬所有路由斷言都不匹配 404 , 和 spring boot 默認保持一致的錯誤輸出頁面。 顯然我們在網關同樣配置 @ControllerAdvice 是不能解決問題,因為 spring cloud gateway 是基于 webflux 反應式編程。

Spring Cloud Gateway全局通用異常處理的實現

解決方法

默認處理流程

ExceptionHandlingWebHandler 作為 spring cloud gateway 最核心 WebHandler 的一部分會進行異常處理的過濾

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 執行 for (WebExceptionHandler handler : this.exceptionHandlers) { completion = completion.onErrorResume(ex -> handler.handle(exchange, ex)); } return completion; }}

默認實現 DefaultErrorWebExceptionHandler

Spring Cloud Gateway全局通用異常處理的實現

public class DefaultErrorWebExceptionHandler { @Override protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) { // 根據客戶端 `accpet` 請求頭決定返回什么資源,如上瀏覽器返回的是 頁面 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'}⏎

重寫 ErrorWebExceptionHandler

/** * @author lengleng * @date 2020/5/23 * <p> * 網關異常通用處理器,只作用在webflux 環境下 , 優先級低于 {@link ResponseStatusExceptionHandler} 執行 */@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]); }})); }}

總結

重寫的 DefaultErrorWebExceptionHandler 優先級一定要小于內置 ResponseStatusExceptionHandler 經過它處理的獲取對應錯誤類的 響應碼 其他擴展 可以參考 SentinelBlockExceptionHandler sentinel 整合網關的處理,不過整體和默認的異常處理沒有什么區別 基礎環境說明:Spring Cloud Hoxton.SR4 & Spring Boot 2.3.0 具體實現代碼參考:https://gitee.com/log4j/pig

到此這篇關于Spring Cloud Gateway全局通用異常處理的實現的文章就介紹到這了,更多相關Spring Cloud Gateway全局異常內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产亚洲精品国产一区 | 亚洲高清美女一区二区三区 | 久久er国产精品免费观看1 | 青青热久免费精品视频精品 | 色天天色综合 | 中文在线日韩 | 一级毛片在线播放免费 | aaa在线观看高清免费 | 免费无遮挡十八女禁污污网站 | 免费网站观看 | 日韩片在线观看 | 免费看爱爱视频 | 毛片在线看免费版 | 午夜影院在线观看视频 | 久久成人国产精品免费 | 日本一级大黄毛片一级 | 深夜释放自己黄瓜视频 | 国产精品午夜激爽毛片 | 免费在线观看小视频 | 国产精品色图 | 国产不卡毛片 | 成人精品亚洲人成在线 | 免费影院在线 | 亚洲一级生活片 | 日韩一区二区三区在线 | 真人一级毛片免费观看视频 | 2022色婷婷综合久久久 | 在线观看精品自拍视频 | 成 人免费va视频 | 一级中国乱子伦视频 | 国产欧美日韩第一页 | 免费超爽大片黄网站 | 一级黄色片黄色片 | 久久久久伊人 | 国产图片综合 | 久久99国产综合精品 | 国产精品天天看天天爽 | 国产在线精品成人一区二区三区 | 黑人破乌克兰美女处 | 久久国产精品自线拍免费 | 手机在线观看你懂得 |