手记

gateWay(3)内置过滤器工厂

内置谓词工厂共有26种
https://www.imooc.com/article/290816

9种内置全局过滤器!
https://www.imooc.com/article/290821      

通过手记进行测试即可

routes:
  - id: aftre-routes
    uri: lb://user-center
    predicates:
      - TimeBetween=上午0:00,下午11:59
    filters:
      - AddRequestHeader=X-Request-Foo, Bar

配置形式


配置形式



AbstractNameValueGatewayFilterFactory继承了AbstractGatewayFilterFactory,也就是第二种方式是第一种方式的简化



eg:

package com.itmuch.gateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
@Component
@Slf4j
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    @Override
    public GatewayFilter apply(NameValueConfig config) {

        return ((exchange, chain) ->{
            log.info("请求进来了,{},{}",config.getName(),config.getValue());//过滤器业务逻辑
            ServerHttpRequest build = exchange.getRequest().mutate().build();//拿到request并修改请求
            ServerWebExchange build1 = exchange.mutate().request(build).build();//将修改过后的请求传进去就可以修改exchange

            return chain.filter(build1);
            
        });
    }
}
routes:
  - id: aftre-routes
    uri: lb://user-center
    predicates:
      - TimeBetween=上午0:00,下午11:59
    filters:
      - AddRequestHeader=X-Request-Foo, Bar
      - PreLog=a,b

与自定义过滤器的命名还有配置文件的配置与自定义谓词工厂类似

在访问服务user-center时候就会打印

2021-07-31 14:37:00.149  INFO 7732 --- [ctor-http-nio-2] c.i.gateway.PreLogGatewayFilterFactory   : 请求进来了,a,b


gateway 监控端点
https://www.imooc.com/article/290822      


调试排除三板斧:
https://www.imooc.com/article/290824      

过滤器执行顺序:

1.oder越小越靠前执行
2.过滤器工厂的order按配置顺序从1开始递增
3.如果配置了默认过滤器,则先执行相同的order的默认过滤器
4.如需自行控制order,可返回OrderedGatewayFilter:

package com.itmuch.gateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

@Component
@Slf4j
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
    @Override
    public GatewayFilter apply(NameValueConfig config) {

        //如果需要自行控制这个过滤器的order,从而调整执行顺序,可以这样返回一个GatewayFilter
        GatewayFilter gatewayFilter = ((exchange, chain) -> {
            log.info("请求进来了,{},{}", config.getName(), config.getValue());//过滤器业务逻辑
            ServerHttpRequest build = exchange.getRequest().mutate().build();//拿到request并修改请求
            ServerWebExchange build1 = exchange.mutate().request(build).build();//将修改过后的请求传进去就可以修改exchange

            return chain.filter(build1);//将修改好的exchange交给下一个过滤器
        });
        return new OrderedGatewayFilter(gatewayFilter, 1000);

        //      //普通自定义过滤器
//        return ((exchange, chain) ->{
//            log.info("请求进来了,{},{}",config.getName(),config.getValue());//过滤器业务逻辑
//            ServerHttpRequest build = exchange.getRequest().mutate().build();//拿到request并修改请求
//            ServerWebExchange build1 = exchange.mutate().request(build).build();//将修改过后的请求传进去就可以修改exchange
//
//            return chain.filter(build1);//将修改好的exchange交给下一个过滤器
//        });
    }
}


gateway限流手记,gateway限流比sentile限流更简单

https://www.imooc.com/article/290828

0人推荐
随时随地看视频
慕课网APP