spring gateway-自定义路由断言工厂与局部GatewayFiler

自定义 RoutePredicateFactory

类名称一定要是 xxxRoutePredicateFactory并继承AbstractRoutePredicateFactory才可以

package com.liu.predicate;


import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;


import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * 判断请求参数中的age 》=minAge and age <=maxAge
 */
@Component
public class AgeRoutePredicateFactory extends AbstractRoutePredicateFactory<AgeRoutePredicateFactory.Config> {

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

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("minAge", "maxAge");
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return exchange -> {
            String ageParam = exchange.getRequest().getQueryParams().getFirst("age");

            if(StringUtils.isNotBlank(ageParam)){
                Integer age = Integer.valueOf(ageParam);
                if(age>= config.getMinAge() && age<= config.getMaxAge()){
                    return true;
                }

            }
            return false;
        };
    }






    public static class Config {
        private int minAge;
        private int maxAge;

        public int getMinAge() {
            return minAge;
        }

        public void setMinAge(int minAge) {
            this.minAge = minAge;
        }

        public int getMaxAge() {
            return maxAge;
        }

        public void setMaxAge(int maxAge) {
            this.maxAge = maxAge;
        }
    }
}

在配置文件中只要类的xxxRoutePredicateFactory 中的xxx首字母大写

  • 配置文件
  predicates: # 断言:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默 认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
            - Path=/autodeliver/**,/demo/**,/other/**
            - Age=18,50

自定义局部GatewayFiler

类名和上面的规则一样,xxxAbstractGatewayFilterFactory 并继承AbstractGatewayFilterFactory类

package com.liu.filter;


import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;


import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {



    public LogGatewayFilterFactory() {
        super(LogGatewayFilterFactory.Config.class);

    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("conLog","cacheLog");
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            if(config.isConLog()){
                log.info("控制台日志开始打印...");
            }
            if(config.isCacheLog()){
                log.info("缓存日志开始打印...");
            }


            return chain.filter(exchange);
        };


    }

    public static class Config {
        private boolean conLog;
        private boolean cacheLog;

        public boolean isConLog() {
            return conLog;
        }

        public void setConLog(boolean conLog) {
            this.conLog = conLog;
        }

        public boolean isCacheLog() {
            return cacheLog;
        }

        public void setCacheLog(boolean cacheLog) {
            this.cacheLog = cacheLog;
        }
    }
}

  • 配置文件
    使用Filters标签来配置,名称也是xxxAbstractGatewayFilterFactory 中的 xxx=
  cloud:
    gateway:
      routes: # 路由可以有多个
        - id: service-autodeliver-router # 我们自定义的路由 ID,保持唯一
          # uri: http://127.0.0.1:8096  # 目标服务地址  自动投递微服务(部署多实例)  动态路由:uri配置的应该是一个服务名称,而不应该是一个具体的服务实例的地址
          uri: lb://edu-service-autodeliver # gateway网关从服务注册中心获取实例信息然后负载后路由
          predicates: # 断言:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默 认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
            - Path=/autodeliver/**,/demo/**,/other/**
            - Age=18,50
          filters:
            - Log=true,true

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值