Gateway网关异常处理

一、webflux框架全局异常处理

通过实现ErrorWebExceptionHandler接口,重写handle方法,替换掉框架默认的异常处理实现类DefaultErrorWebExceptionHandler

public class GatewayErrorWebExceptionHandler implements ErrorWebExceptionHandler {/*** 处理给定的异常* @param exchange* @param ex* @return*/@Overridepublic Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {ServerHttpResponse response = exchange.getResponse();BaseResponse baseResponse = new BaseResponse();if (ex instanceof ResponseStatusException) {baseResponse.setStatus(((ResponseStatusException) ex).getStatus().value());baseResponse.setMessage(((ResponseStatusException) ex).getReason());} else {baseResponse.setStatus(500);baseResponse.setMessage(ex.getMessage());}DataBuffer dataBuffer = response.bufferFactory().allocateBuffer().write(JSONUtils.toJSONString(baseResponse).getBytes());response.setStatusCode(HttpStatus.OK);//基于流形式response.getHeaders().setContentType(MediaType.APPLICATION_NDJSON);return response.writeAndFlushWith(Mono.just(ByteBufMono.just(dataBuffer)));}
}

二、circuitBreaker断路器异常处理

SpringCloudCircuitBreakerFilterFactory拦截器工厂类apply方法中addExceptionDetails方法是处理断路器熔断时异常,handleErrorWithoutFallback方法是在未配置断路器跳转地址的时候异常处理;

 private void addExceptionDetails(Throwable t, ServerWebExchange exchange) {ofNullable(t).ifPresent(exception -> exchange.getAttributes().put(CIRCUITBREAKER_EXECUTION_EXCEPTION_ATTR, exception));}

断路器异常会存在ServerWebExchange的属性CIRCUITBREAKER_EXECUTION_EXCEPTION_ATTR中,所以我们可以在断路器跳转地址中从此属性中获取异常信息;

    @GetMapping("fallback")public String fallback(ServerWebExchange exchange){Throwable throwable = exchange.getAttribute(ServerWebExchangeUtils.CIRCUITBREAKER_EXECUTION_EXCEPTION_ATTR);ServerWebExchange delegate = ((ServerWebExchangeDecorator) exchange).getDelegate();logger.error("服务调用失败,URL={}", delegate.getRequest().getURI(), throwable);return "Service Unavailable";}

handleErrorWithoutFallback方法的具体实现在SpringCloudCircuitBreakerResilience4JFilterFactory类中,其会在指定了断路器但是未指定跳转方法的时候处理异常,具体会被全局异常处理,如下:

 @Overrideprotected Mono<Void> handleErrorWithoutFallback(Throwable t, boolean resumeWithoutError) {if (java.util.concurrent.TimeoutException.class.isInstance(t)) {return Mono.error(new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, t.getMessage(), t));}if (CallNotPermittedException.class.isInstance(t)) {return Mono.error(new ServiceUnavailableException());}if (resumeWithoutError) {return Mono.empty();}return Mono.error(t);}

GitHub地址:https://github.com/mingyang66/EmilyGateway

Gateway网关异常处理相关推荐

  1. SpringCloud整合Sa-Token登录认证+Gateway网关拦截

    Sa-Token介绍:Sa-Token 是一个轻量级 Java 权限认证框架,主要解决:登录认证.权限认证.单点登录.OAuth2.0.分布式Session会话.微服务网关鉴权 等一系列权限相关问题 ...

  2. Spring Cloud Gateway网关

    Spring Cloud Gateway网关 1. 简介 Spring Cloud Gateway是Spring官网基于Spring 5.0. Spring Boot 2.0.Project Reac ...

  3. SpringCloud + Consul服务注册中心 + gateway网关

    1  启动Consul 2  创建springcloud-consul项目及三个子模块 2.1 数据模块consul-producer 2.2 数据消费模块consul-consumer 2.3 ga ...

  4. Spring Cloud Gateway中异常处理

    Spring Cloud Gateway中异常处理 参考文章: (1)Spring Cloud Gateway中异常处理 (2)https://www.cnblogs.com/viaiu/p/1040 ...

  5. SpringCloud微服务架构之,Hystrix 熔断器,Gateway 网关

    Hystrix 概述 Hystix 是 Netflix 开源的一个延迟和容错库,用于隔离访问远程服务.第三方库,防止出现级联失败(雪崩). pom依耐 <!-- hystrix -->&l ...

  6. Gateway网关-网关作用介绍

    Gateway服务网关 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project ...

  7. SpringCloud Greenwich(四)注册中心之eureka、Zuul和 gateway网关配置

    本项目是搭建基于eureka注册中心的springcloud,使用zuul网关和gateway网关 一.框架搭建 (1)项目结构 eureka-server  eureka注册中心 micro-ser ...

  8. SpringCloud Greenwich(三)注册中心之zookeeper、Zuul和 gateway网关配置

    本项目是搭建基于zookeeper注册中心的springcloud,使用zuul网关和gateway网关 一.框架搭建 (1)项目结构 micro-service  服务提供者 zuul-gatewa ...

  9. SpringCloud Greenwich(二)注册中心之consul、Zuul和 gateway网关配置

    本项目是搭建基于consul注册中心的springcloud,使用zuul网关和gateway网关 一.框架搭建 (1)项目结构 micro-service  服务提供者 zuul-gateway  ...

最新文章

  1. 修改MySQL字符集
  2. redis启动后 允许访问_解决Redis开启远程访问及密码问题
  3. 【⚡小丑竟然是我自己⚡】安防三年,今天才知道什么是ONVIF
  4. 万字长文带你了解蚁群算法及求解复杂约束问题【源码实现】
  5. 一个分支强制替代另一个分支
  6. “睡服”面试官系列第十五篇之对象的扩展(建议收藏学习)
  7. gettimeofday函数使用《代码》
  8. 美国政府在将Web业务赶出美国
  9. 用Nginx分流绕开Github反爬机制
  10. Java基础练习之流程控制(二)
  11. CI 图片上传路径问题的解决
  12. flask综合整理2
  13. oracle内连接左连接右连接,ORACLE 左连接 右连接 内连接 区别
  14. 设计模式之Builder模式
  15. java之uml类图的介绍
  16. 学计算机应该买多大尺寸笔记本电脑,笔记本电脑买几寸的比较合适?
  17. 前端SPA(single page web application单页面应用not水疗)
  18. JS工具库moment —— 实现日历
  19. oracle+utl_file+shanchu,utl_file.fremove
  20. 忆旧路:雄关漫道真如铁,而今迈步从头越

热门文章

  1. 关于mysql数据库分库分表、事务一致性问题的学习
  2. 【uni-app】常用图标、头像汇总
  3. 【Linux软件管理】FPM打包工具
  4. 信息学奥赛一本通 1327:【例7.6】黑白棋子的移动 | 洛谷 P1259 黑白棋子的移动
  5. JAVA读取excel表格中的数据,.xlsm文件或者.xlsx文件。
  6. java毕业设计醋坊定制销售平台Mybatis+系统+数据库+调试部署
  7. 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称 。请检查名称
  8. 通达进销存管理系统操作说明书
  9. 3、MySQL——SQLyog图形化客户端、表的字段名,常见数据类型的属性
  10. CSS的tree shaking