7. 全局过滤器

接口与接口的签名相同1。这些都是对所有路由有条件地应用的特殊过滤器。spring-doc.cadn.net.cn

此接口及其用法可能会在未来的里程碑版本中发生变化。

7.1. 组合全局过滤器和GatewayFilter顺序

当请求与路由匹配时,筛选器 Web 处理程序会将所有实例添加到筛选器链中。GlobalFilter和所有特定于路由的实例GatewayFilter。此组合筛选器链按接口进行排序org.springframework.core.Ordered,您可以通过实现该方法设置getOrder()方法。spring-doc.cadn.net.cn

Spring Cloud Gateway 将过滤器逻辑执行的“pre”和“post”阶段区分开来(参见如何工作\),具有最高优先级的过滤器是“pre”阶段的第一个,也是“post”阶段的最后一个。spring-doc.cadn.net.cn

以下清单配置了一个过滤器链:spring-doc.cadn.net.cn

示例 56. ExampleConfiguration.java
@Bean
public GlobalFilter customFilter() {
    return new CustomGlobalFilter();
}

public class CustomGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("custom global filter");
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -1;
    }
}

7.2. 前进路由过滤器

ForwardRoutingFilter在交换属性ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR中查找URI。如果URL具有forward方案(例如forward:///localendpoint),则它将使用Spring DispatcherHandler来处理请求。请求的路径部分被重写为转发URL中的路径。原始URL的未修改版本被追加到属性ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR列表中。spring-doc.cadn.net.cn

0

The ReactiveLoadBalancerClientFilter looks for a URI in the exchange attribute named ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the URL has a lb scheme (such as lb://myservice), it uses the Spring Cloud ReactorLoadBalancer to resolve the name (myservice in this example) to an actual host and port and replaces the URI in the same attribute. The unmodified original URL is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute. The filter also looks in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR attribute to see if it equals lb. If so, the same rules apply. The following listing configures a ReactiveLoadBalancerClientFilter:spring-doc.cadn.net.cn

示例57. application.yml
spring:
  cloud:
    gateway:
      routes:
      - id: myRoute
        uri: lb://service
        predicates:
        - Path=/service/**
默认情况下,当ReactorLoadBalancer找不到服务实例时,将返回503

spring-doc.cadn.net.cn

您可以通过设置spring.cloud.gateway.loadbalancer.use404=true配置网关以返回404spring-doc.cadn.net.cn

ReactiveLoadBalancerClientFilter 返回的 ServiceInstanceisSecure 值会覆盖向网关发出的请求中指定的协议。 例如,如果请求通过 HTTPS 进入网关,但 ServiceInstance 表明它不安全,则下游请求将通过 HTTP 发出。 相反的情况也可能适用。 然而,如果在网关配置中为路由指定了 GATEWAY_SCHEME_PREFIX_ATTR,前缀将被剥离,并且路由 URL 的最终协议将覆盖 ServiceInstance 配置。
网关支持所有负载均衡器的功能。您可以在 Spring Cloud 公共模块文档 中了解更多相关信息。

7.4. Netty 路由过滤器

如果位于 ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR 交换属性中的 URL 具有 httphttps 方案,则运行 Netty 路由筛选器。它使用 Netty HttpClient 来进行下游代理请求。ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR 交换属性用于后续筛选器中响应的放置。(还有一个实验性的 WebClientHttpRoutingFilter 执行相同的功能,但不需要 Netty。)spring-doc.cadn.net.cn

7.5. Netty 写入响应过滤器

如果 NettyWriteResponseFilter 中存在 Netty 的 HttpClientResponse,则在 ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR 交换属性中运行。它会在所有其他过滤器完成后执行,并将代理响应写回网关客户端响应。(另有一个实验性 WebClientWriteResponseFilter 可以实现相同的功能,但不需要 Netty。)spring-doc.cadn.net.cn

7.6. RouteToRequestUrl 过滤器

如果 ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR 交换属性中存在 Route 对象,则运行 RouteToRequestUrlFilter。它基于请求 URI 创建一个新的 URI,但会使用 Route 对象的 URI 属性进行更新。
将新 URI 放入 ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR 交换属性。spring-doc.cadn.net.cn

如果 URI 具有方案前缀,例如 lb:ws://serviceid,则会从 URI 中删除 lb 方案,并将其放置在 ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR 中供稍后在过滤器链中使用。spring-doc.cadn.net.cn

7.7. WebSocket 路由过滤器

如果位于ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR交换属性中的URL具有wswss方案,则运行websocket路由过滤器。它使用Spring WebSocket基础设施将websocket请求转发到下游。spring-doc.cadn.net.cn

您可以通过在URI前加上lb来平衡WebSockets的负载,例如lb:ws://serviceidspring-doc.cadn.net.cn

如果您使用SockJS作为常规HTTP的回退方案,则应同时配置普通HTTP路由以及WebSocket路由。

以下列表配置了 WebSocket 路由筛选器:spring-doc.cadn.net.cn

示例 58. application.yml
spring:
  cloud:
    gateway:
      routes:
      # SockJS route
      - id: websocket_sockjs_route
        uri: http://localhost:3001
        predicates:
        - Path=/websocket/info/**
      # Normal Websocket route
      - id: websocket_route
        uri: ws://localhost:3001
        predicates:
        - Path=/websocket/**

7.8. 网关指标过滤器

要启用网关指标,请将 spring-boot-starter-actuator 添加为项目依赖。然后,默认情况下,只要属性spring.cloud.gateway.metrics.enabled未设置为false,网关指标过滤器就会运行。此过滤器添加了一个名为spring.cloud.gateway.requests的计时器指标,并带有以下标签:spring-doc.cadn.net.cn

此外,通过属性spring.cloud.gateway.metrics.tags.path.enabled(默认情况下设置为false),您可以激活带有以下标记的额外指标:spring-doc.cadn.net.cn

这些指标可以被从/actuator/metrics/spring.cloud.gateway.requests抓取,并且可以轻松地与Prometheus集成,创建Grafana仪表盘spring-doc.cadn.net.cn

要启用 Prometheus 端点,请将micrometer-registry-prometheus添加为项目依赖项。

7.9. 标记交换机为已路由

网关在路由一个 ServerWebExchange 后,会通过向交换属性添加 gatewayAlreadyRouted 来标记该交换为“已路由”。一旦请求被标记为已路由,其他路由过滤器将不会再次路由该请求,实际上跳过了该过滤器。有一些便捷的方法可供使用,用于标记交换为已路由或检查交换是否已经被路由。spring-doc.cadn.net.cn

  • ServerWebExchangeUtils.isAlreadyRouted 接受一个 ServerWebExchange 对象并检查它是否已被“路由”。spring-doc.cadn.net.cn

  • ServerWebExchangeUtils.setAlreadyRouted 接受一个 ServerWebExchange 对象并将其标记为“已路由”。spring-doc.cadn.net.cn