全局过滤器(GloabalFilter)

注意:

  • 过滤器是指gateway在路由过程中(A地址路由到B地址)生效进行过滤操作的,所有首先你得先配一个地址路由。比如我站在这里设置将http://localhost:8000/gateway/* 请求路由到http://localhost:8010/user/*,那么下面的两个样例过滤器遍对这个过程进行过滤
        - id: user_route
          uri: lb://server-user
          #uri: http://localhost:8010
          predicates:
            - Path=/gateway/**
          filters:
            - RewritePath=/gateway/(?<segment>.*),/user/$\{segment}
  • 开启gateway的日志,方便后面查看过程
logging:
  level:
    org.springframework.cloud.gateway: DEBUG

前置过滤器

@Component
public class MyHeadFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("123123");
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

后置过滤器

@Component
public class MyTailFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return chain.filter(exchange)
                .then(Mono.fromRunnable(
                        () -> System.out.println("abcabc")
                ));
    }
    @Override
    public int getOrder() {
        return 0;
    }
}

运行结果

然后访问http://localhost:8000/gateway/listAll,路由到http://localhost:8010/user/listAll成功
控制台输出
在这里插入图片描述

局部过滤器

有两种方式:

  • 在上面全局模式的基础上,改为实现gatewayFilter和Ordered接口
  • 继承AbstractGatewayFilterFactory类(推荐)

我这里就实现了一下第二种方法。参考自A博客B博客

局部过滤器实现

public class MyPartGatewayFilterFactory extends AbstractGatewayFilterFactory<MyPartGatewayFilterFactory.Config> {

    private static final Log log = LogFactory.getLog(GatewayFilter.class);
    private static final String NAME = "name";

    public MyPartGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList(NAME);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            return chain.filter(exchange).then(
                    Mono.fromRunnable(() -> {
                        StringBuilder sb = new StringBuilder("这是20227258点半之后的请求,请求者name(入参):");
                        sb.append(config.getName());
                        log.info(sb.toString());
                    })
            );
        };
    }
    public static class Config{
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
}

在gateway的启动类上进行注册

@Bean
    public MyPartGatewayFilterFactory myGatewayFilterFactory(){
        return new MyPartGatewayFilterFactory();
    }

对应的配置文件部分

        - id: my_route
          uri: http://127.0.0.1:8010
          filters:
            - name: MyPart
              args:
                name: yuan
          predicates:
#            - After=2017-01-20T17:42:47.789-07:00[America/Denver]
            - After=2022-07-25T08:30:00.003+08:00[Asia/Shanghai]

运行结果

访问http://localhost:8000/user/test

  • 8000是我的gateaway运行端口
    在这里插入图片描述
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐