[SpringCloudGateway]:端点重定向超时后不执行后置过滤器

问题描述 投票:0回答:1

我们在 Spring Cloud Gateway 应用程序中实现了几个自定义的有序全局前置和后置过滤器。在重定向到配置的端点之前,将对任何传入请求执行这些预全局过滤器。所有后置全局过滤器都会在端点调用成功完成并收到响应后执行。

我们观察到这些后置过滤器正在针对其他 4xx 执行,例如 404(未找到)、401(未经授权)和 5xx,例如 503(服务不可用)、500(内部服务器错误)、来自端点调用的错误响应。

但是,我们观察到,当端点调用超时(即收到 504 Gateway Timeout 响应)时,这些后置全局过滤器不会被执行。全局响应超时配置为 30 秒,如下所示,端点调用需要超过 30 秒来处理请求:

spring:
  cloud:
    gateway:
      httpclient:
        connect-timeout: 30000
        response-timeout: 30000

全局后置过滤器按以下模式实现:

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    return chain.filter(exchange).then(Mono.fromRunnable(() -> {
        final ResponseCookie  resCookie = ResponseCookie.from(this.cookieName, "invalidated").httpOnly(true).secure(true).path("/").maxAge(0).build();
        exchange.getResponse().addCookie(resCookie);
   }));
}

@Override
public int getOrder() {
   return POST_FILTER_BASE_ORDER - 3;  // POST_FILTER_BASE_ORDER = 1000
}

此后置过滤器针对来自端点的 404 和 503 响应状态执行,但是当 API 网关超时端点调用时(端点需要超过 30 秒响应),该后置过滤器将无法执行。

这是 Spring Cloud Gateway 框架的预期行为还是缺陷?

非常感谢任何解决该问题的帮助。

spring-boot spring-mvc spring-webflux spring-cloud spring-cloud-gateway
1个回答
0
投票

您可以尝试将订单值返回为-1,如下所示:

@Override
public int getOrder() {
   return -1;
}
© www.soinside.com 2019 - 2024. All rights reserved.