Spring Cloud Gateway过滤器订购

时间:2019-05-21 06:46:11

标签: filter spring-cloud-gateway

我正在使用Spring Cloud Gateway过滤器,并希望通过GatewayFilterFactory过滤请求, 但订单不适用于@Order注释

我尝试交换过滤器的顺序,但是不起作用。


@Slf4j
@Order(1)
@Component
public class AccessFilter implements GatewayFilterFactory<AccessFilter.Config> {

    @Override
    public GatewayFilter apply(AccessFilter.Config config) {
        return (exchange, chain) -> { 
           log.info("in AccessFilter second");
           // do something
        }
    }
}

@Order(-1)
@Slf4j
@Component
public class AddTraceIdGlobalFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("in globalfilter first");
        // do something
    }

}

我的Yaml文件:

spring:
  cloud:
    gateway:
      routes:
      - id: user_1
        uri: http://user:8080
        predicates:
        - Path=/v1/user/info
        filters:
        - AccessFilter

我希望输出:

in globalfilter first
in AccessFilter second

但实际输出:

in AccessFilter second
in globalfilter first

1 个答案:

答案 0 :(得分:0)

GatewayFilterFactory无法应用@OrderOrdered。 您必须使用OrderedGatewayFilter

@Component
public class AccessFilter implements GatewayFilterFactory<AccessFilter.Config> {

    @Override
    public GatewayFilter apply(AccessFilter.Config config) {
        return new OrderedGatewayFilter((exchange, chain) -> { 
           log.info("in AccessFilter second");
           // do something
        }, 1)
    }
}