Is there a recommended order range for Filters in Spring Cloud Gateway?
I would like to know if there is a recommended order range for Gateway Filters (global or not) in Spring Cloud Gateway because there are Spring Gateway filters with orders less than 1 and greater than 9999 in gateway chain. See:
{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]"
My custom global filters implements Ordered interface:
@Component
public class GlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ... }
@Override
public int getOrder() {
return 2; // here
}
}
And my Gateway filters are OrderedGatewayFilter:
@Component
public class CustomFilter implements GatewayFilterFactory<CustomFilter.Config> {
@Override
public GatewayFilter apply(Config config) {
return new OrderedGatewayFilter((exchange, chain) -> {
...
}, 3); // here
}
...
}
Should my filters be inside 1..9999 range? Or is there no problem in put my custom filters before or after these Spring Cloud Gateway filters?
Solution 1:
The filter chain is sorted by the org.springframework.core.Ordered interface. According to the Spring Framework documentation on Annotation Type Order:
The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest priority (losing to any other specified order value).
But
Lower values have higher priority.
If you check that in your IDE, the lowest possible value (Which has the highest precedence) is -2147483648. The highest possible value is 2147483647.
Judging from the documentation on Global Filters and a bit of experience, -1 is a starting point and you can arrange the order you need around that. Or if you have one filter that always needs to be executed first, use Ordered.HIGHEST_PRECEDENCE and order the other filters from there, like Ordered.HIGHEST_PRECEDENCE+1 etc.