在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类型的过滤器。
X 关闭
2023-04-12 01:23:41
2023-04-11 21:59:06
2023-04-11 21:21:34
2023-04-11 20:49:08
2023-04-11 19:40:58
2023-04-11 18:45:46
2023-04-11 17:39:14
2023-04-11 16:58:53
2023-04-11 16:00:35
2023-04-11 15:08:17
2023-04-11 14:17:21
2023-04-11 12:59:16
2023-04-11 12:12:47
2023-04-11 11:28:56
2023-04-11 10:46:32
2023-04-11 09:48:10
2023-04-11 08:52:46
2023-04-11 07:55:10
2023-04-11 06:20:59
2023-04-11 02:49:04
2023-04-10 22:38:31
2023-04-10 21:12:43
2023-04-10 19:54:01
2023-04-10 18:43:17
2023-04-10 17:46:30
2023-04-10 17:01:44
2023-04-10 15:47:41
2023-04-10 14:51:49
2023-04-10 13:55:51
2023-04-10 12:41:41
2023-04-10 12:00:50
2023-04-10 11:04:41
2023-04-10 10:09:17
2023-04-10 09:33:31
2023-04-10 08:24:57
2023-04-10 06:30:21
2023-04-10 02:52:40
2023-04-09 22:16:21
2023-04-09 20:01:58
2023-04-09 18:16:28
2023-04-09 16:52:40
2023-04-09 14:57:55
2023-04-09 12:55:42
2023-04-09 11:11:17
2023-04-09 09:45:34
2023-04-09 07:45:29
2023-04-09 04:52:59
2023-04-08 23:01:03
2023-04-08 20:42:56
2023-04-08 18:40:02
2023-04-08 16:52:30
2023-04-08 15:04:14
2023-04-08 13:14:28
2023-04-08 11:43:47
2023-04-08 10:04:39
2023-04-08 08:50:42
2023-04-08 06:45:15
2023-04-08 03:44:12
2023-04-07 23:01:24
2023-04-07 21:21:33
2023-04-07 20:09:12
2023-04-07 18:51:21
2023-04-07 17:39:13
2023-04-07 16:44:06
2023-04-07 15:55:35
2023-04-07 15:00:48
2023-04-07 14:09:03
2023-04-07 12:38:03
2023-04-07 11:39:34
2023-04-07 10:47:29
2023-04-07 10:09:19
2023-04-07 09:23:33
2023-04-07 08:29:54
2023-04-07 06:58:42
2023-04-07 04:46:12
2023-04-07 00:06:02
2023-04-06 22:01:00
2023-04-06 20:08:14
2023-04-06 19:17:45
2023-04-06 18:00:44
2023-04-06 17:19:24
2023-04-06 16:19:16
2023-04-06 15:52:16
2023-04-06 14:40:45
2023-04-06 13:36:23
2023-04-06 12:20:45
2023-04-06 11:21:21
2023-04-06 10:51:55
2023-04-06 10:18:19
2023-04-06 09:14:51
2023-04-06 08:05:15
2023-04-06 06:38:57
2023-04-06 03:45:22
2023-04-05 22:49:52
2023-04-05 20:57:34
2023-04-05 18:52:25
2023-04-05 17:27:40
2023-04-05 16:04:37
2023-04-05 14:15:59
2023-04-05 12:34:37
Copyright © 2015-2022 北冰洋化工网版权所有 备案号:沪ICP备2020036824号-3 联系邮箱:562 66 29@qq.com