社会 >   >  正文

天天播报:Spring Cloud Gateway过滤器的执行顺序

评论

在Spring Cloud Gateway中,过滤器的执行顺序对于实现请求处理流程的正确性和效率至关重要。Spring Cloud Gateway中的过滤器分为全局过滤器和局部过滤器两种类型,不同类型的过滤器在执行顺序上有所不同。

全局过滤器执行顺序


【资料图】

全局过滤器是指在所有路由规则中都会执行的过滤器,可以用于实现一些全局性的功能,如请求的日志记录、响应头信息的设置等。Spring Cloud Gateway提供了一些内置的全局过滤器,如请求路径的重写、请求日志的记录等。在Spring Cloud Gateway中,全局过滤器的执行顺序是由GatewayFilterAdapter的ORDER常量值确定的,该常量值为-2147483648,表示全局过滤器将在所有的局部过滤器之前执行。

局部过滤器执行顺序

局部过滤器是指只在特定路由规则中才会执行的过滤器,可以用于实现一些特定的功能,如请求鉴权、请求转发等。Spring Cloud Gateway中的局部过滤器可以通过自定义过滤器工厂类来实现,该工厂类需要继承AbstractGatewayFilterFactory抽象类,并实现其中的apply方法和泛型参数指定配置类。在Spring Cloud Gateway中,局部过滤器的执行顺序是由配置文件中的filters属性确定的,该属性可以通过spring.cloud.gateway.routes.filters参数进行配置,不同的过滤器在列表中的位置就决定了它们的执行顺序。

以下是一个示例,其中定义了一个全局过滤器和两个局部过滤器,演示了不同类型过滤器的执行顺序:

@Componentpublic class GlobalFilter implements GatewayFilter, Ordered {    @Override    public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {        System.out.println("GlobalFilter before...");        return chain.filter(exchange).then(Mono.fromRunnable(() -> {            System.out.println("GlobalFilter after...");        }));    }    @Override    public int getOrder() {        return -1;    }}@Componentpublic class LocalFilter1 extends AbstractGatewayFilterFactory {    public LocalFilter1() {        super(Config.class);    }    @Override    public GatewayFilter apply(Config config) {        return (exchange, chain) -> {            System.out.println("LocalFilter1 before...");            return chain.filter(exchange).then(Mono.fromRunnable(() -> {                System.out.println("LocalFilter1 after...");            }));        };    }    public static class Config {        // 配置参数    }}@Componentpublic class LocalFilter2 extends AbstractGatewayFilterFactory {    public LocalFilter2() {        super(Config.class);    }    @Override    public GatewayFilter apply(Config config) {        return (exchange, chain) -> {            System.out.println("LocalFilter2 before...");            return chain.filter(exchange).then(Mono.fromRunnable(() -> {                System.out.println("LocalFilter2 after...");            }));        };    }    public static class Config {        // 配置参数    }}

在这个示例中,我们定义了一个全局过滤器GlobalFilter和两个局部过滤器LocalFilter1和LocalFilter2。其中,GlobalFilter实现了GatewayFilter和Ordered接口,用于实现全局过滤器的逻辑。LocalFilter1和LocalFilter2都继承了AbstractGatewayFilterFactory抽象类,并通过实现apply方法实现了局部过滤器的逻辑。在apply方法中,我们可以实现自己的过滤逻辑,并返回一个GatewayFilter对象。在GatewayFilter对象中,我们可以继续调用chain.filter方法来执行下一个过滤器,或者直接返回结果。这里我们使用Mono.fromRunnable方法来在请求结束时输出一些信息。

在上述示例中,我们定义了全局过滤器和两个局部过滤器。在执行顺序方面,由于全局过滤器的ORDER常量值最小,因此它会在所有的局部过滤器之前执行。而在局部过滤器的执行顺序方面,它们的执行顺序是由配置文件中的filters属性决定的,如下所示:

spring:  cloud:    gateway:      routes:      - id: example        uri: http://example.org        predicates:        - Path=/example/**        filters:        - LocalFilter2        - LocalFilter1

在这个配置文件中,我们为example路由规则指定了两个局部过滤器,分别是LocalFilter2和LocalFilter1。在执行顺序方面,LocalFilter2将会先于LocalFilter1执行,因为它们在filters列表中的位置是从前往后的。也就是说,请求先经过LocalFilter2,再经过LocalFilter1,最后再到达后端服务。

需要注意的是,在GatewayFilterChain中的filter方法调用中,如果其中一个过滤器返回了错误,那么整个请求处理过程会立即停止并返回错误。因此,在设计过滤器时需要格外小心,确保每个过滤器都不会抛出异常,以免影响整个系统的稳定性。

此外,还有一些其他的过滤器类型,如:

Pre Filter:在请求被路由之前调用。可以用来实现身份认证、IP过滤等逻辑。Post Filter:在请求被路由之后调用。可以用来实现响应头处理、日志记录等逻辑。Error Filter:在请求处理过程中发生错误时调用。可以用来实现异常处理、错误日志记录等逻辑。

这些过滤器类型可以通过实现不同的接口来实现。例如,实现Ordered和GatewayFilter接口的就是Pre Filter和Global Filter类型的过滤器。而实现Ordered和WebFilter接口的则是Error Filter类型的过滤器。

标签:

今日热点

热点排行

最近更新

所刊载信息部分转载自互联网,并不代表本网赞同其观点和对其真实性负责。邮箱:5855973@qq.com

联系我们| 中国品牌网 | 沪ICP备2022005074号-18 营业执照  Copyright © 2018@. All Rights Reserved.