Spring Cloud Gateway 中的过滤器是否有推荐的顺序范围?
Is there a recommended order range for Filters in Spring Cloud Gateway?
我想知道 Spring Cloud Gateway 中是否有网关过滤器(全局或非全局)的推荐顺序范围,因为 Spring 网关过滤器的顺序小于或大于 1在网关链中大于 9999。参见:
{OrderedGatewayFilter@20770} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RemoveCachedBodyFilter@5eb974}, order = -2147483648]"
{OrderedGatewayFilter@20771} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@ae5eeee}, order = -2147482648]"
{OrderedGatewayFilter@20772} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@492c1b1b}, order = -1]"
{OrderedGatewayFilter@20774} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardPathFilter@3927de0e}, order = 0]"
{OrderedGatewayFilter@20775} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.GatewayMetricsFilter@36dab184}, order = 0]"
// my custom filters should be here
{OrderedGatewayFilter@20791} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@72f112d1}, order = 10000]"
{OrderedGatewayFilter@20792} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.config.GatewayNoLoadBalancerClientAutoConfiguration$NoLoadBalancerClientFilter@71b9b035}, order = 10150]"
{OrderedGatewayFilter@20793} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@4c475547}, order = 2147483646]"
{OrderedGatewayFilter@20794} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@189f5ed8}, order = 2147483647]"
{OrderedGatewayFilter@20795} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@10faebc9}, order = 2147483647]"
我的自定义全局过滤器实现了 Ordered 接口:
@Component
public class GlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ... }
@Override
public int getOrder() {
return 2; // here
}
}
我的网关过滤器是 OrderedGatewayFilter:
@Component
public class CustomFilter implements GatewayFilterFactory<CustomFilter.Config> {
@Override
public GatewayFilter apply(Config config) {
return new OrderedGatewayFilter((exchange, chain) -> {
...
}, 3); // here
}
...
}
我的过滤器应该在 1..9999 范围内吗?或者在这些 Spring 云网关过滤器之前或之后放置我的自定义过滤器没有问题吗?
过滤器链按org.springframework.core.Ordered接口排序。根据 Spring 关于 Annotation Type Order 的框架文档:
The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest
priority (losing to any other specified order value).
但是
Lower values have higher priority.
如果您在 IDE 中检查,可能的最低值(具有最高优先级)是 -2147483648。最高可能值为 2147483647。
根据 Global Filters 上的文档和一些经验判断,-1 是一个起点,您可以围绕它安排您需要的顺序。
或者,如果您有一个始终需要首先执行的过滤器,请使用 Ordered.HIGHEST_PRECEDENCE 并从那里订购其他过滤器,例如 Ordered.HIGHEST_PRECEDENCE+1 等
编辑:Spring Cloud Gateway 内部有一些硬编码的过滤器订单,例如NettyWriteResponseFilter. If you're worried about interference, check the source code 中的“WRITE_RESPONSE_FILTER_ORDER = -1”用于排序。
我想知道 Spring Cloud Gateway 中是否有网关过滤器(全局或非全局)的推荐顺序范围,因为 Spring 网关过滤器的顺序小于或大于 1在网关链中大于 9999。参见:
{OrderedGatewayFilter@20770} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RemoveCachedBodyFilter@5eb974}, order = -2147483648]"
{OrderedGatewayFilter@20771} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@ae5eeee}, order = -2147482648]"
{OrderedGatewayFilter@20772} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@492c1b1b}, order = -1]"
{OrderedGatewayFilter@20774} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardPathFilter@3927de0e}, order = 0]"
{OrderedGatewayFilter@20775} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.GatewayMetricsFilter@36dab184}, order = 0]"
// my custom filters should be here
{OrderedGatewayFilter@20791} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@72f112d1}, order = 10000]"
{OrderedGatewayFilter@20792} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.config.GatewayNoLoadBalancerClientAutoConfiguration$NoLoadBalancerClientFilter@71b9b035}, order = 10150]"
{OrderedGatewayFilter@20793} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@4c475547}, order = 2147483646]"
{OrderedGatewayFilter@20794} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@189f5ed8}, order = 2147483647]"
{OrderedGatewayFilter@20795} "[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@10faebc9}, order = 2147483647]"
我的自定义全局过滤器实现了 Ordered 接口:
@Component
public class GlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ... }
@Override
public int getOrder() {
return 2; // here
}
}
我的网关过滤器是 OrderedGatewayFilter:
@Component
public class CustomFilter implements GatewayFilterFactory<CustomFilter.Config> {
@Override
public GatewayFilter apply(Config config) {
return new OrderedGatewayFilter((exchange, chain) -> {
...
}, 3); // here
}
...
}
我的过滤器应该在 1..9999 范围内吗?或者在这些 Spring 云网关过滤器之前或之后放置我的自定义过滤器没有问题吗?
过滤器链按org.springframework.core.Ordered接口排序。根据 Spring 关于 Annotation Type Order 的框架文档:
The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest priority (losing to any other specified order value).
但是
Lower values have higher priority.
如果您在 IDE 中检查,可能的最低值(具有最高优先级)是 -2147483648。最高可能值为 2147483647。
根据 Global Filters 上的文档和一些经验判断,-1 是一个起点,您可以围绕它安排您需要的顺序。 或者,如果您有一个始终需要首先执行的过滤器,请使用 Ordered.HIGHEST_PRECEDENCE 并从那里订购其他过滤器,例如 Ordered.HIGHEST_PRECEDENCE+1 等
编辑:Spring Cloud Gateway 内部有一些硬编码的过滤器订单,例如NettyWriteResponseFilter. If you're worried about interference, check the source code 中的“WRITE_RESPONSE_FILTER_ORDER = -1”用于排序。