继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

gateWay(3)内置过滤器工厂

fox鑫
关注TA
已关注
手记 19
粉丝 0
获赞 1

内置谓词工厂共有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

http://img3.mukewang.com/6104e6e10001986308010301.jpghttp://img3.mukewang.com/6104e7050001f86b07970352.jpg

配置形式

http://img1.mukewang.com/6104e718000102e308600548.jpg

http://img1.mukewang.com/6104e7530001ac4c08950488.jpg

配置形式

http://img.mukewang.com/6104e7690001aff808990467.jpg


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

http://img3.mukewang.com/6104e8460001715c08740456.jpg


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

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP