网关(Gateway)- 自定义断言工厂

自定义断言工厂类

DemoRoutePredicateFactory

java 复制代码
package com.learning.springcloud.custom;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.server.ServerWebExchange;

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

 @Component
public class DemoRoutePredicateFactory extends AbstractRoutePredicateFactory<DemoRoutePredicateFactory.Config> {


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

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return new GatewayPredicate() {
            public boolean test(ServerWebExchange exchange) {
                if ("YES".equals(config.getName())) {
                    return true;
                }
                return false;
            }
        };
    }

    // 和 内部类 config的相互对应 返回对应的配置信息
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("name");
    }


    // 用于接收 配置文件中的断言信息
    @Validated
    public static class Config {

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

路由配置说明

    • Demo=YES
java 复制代码
server:
  port: 8088
spring:
  application:
    name: api-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8847
        username: nacos
        password: nacos
    gateway:
      routes:
        - id: order_route # 路由唯一标识
          #uri: http://localhost:8020 # 需要转发的地址
          uri: lb://order-service # 需要转发的地址
          # 断言规则  用于路由规则的匹配
          predicates:
            - Demo=YES
          filters:
            - StripPrefix=1 # 转发去掉第一层路径
            # http://localhost:8020/order-serv/order/add => http://localhost:8020/order/add

访问效果

    • Demo=YES
    • Demo=NO

实现说明

  • 命名必须需要以 RoutePredicateFactory 结尾
  • 继承 AbstractRoutePredicateFactory 类
  • 必须为spring的组件bean(@Component)
  • 必须要有内部类 Config 以及 对应的 shortcutFieldOrder 方法
  • 重写 apply 方法的逻辑
  • 可通过 exchange.getRequest() 获取到ServerHttpRequest对象
  • 从而获取到请求的参数、请求方式、请求头等信息
相关推荐
小郎君6581 天前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·spring cloud
jktantan1 天前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·后端·算法·spring cloud·系统架构
JJJennie7772 天前
MAI Gateway私有化部署全攻略:大模型网关接入DeepSeek?附架构图
大模型·gateway·ai网关·魔芋ai
胡写代码3 天前
把数据库连接搬进配置中心,服务起不来:spring.config.import 的 4 个必填项
spring cloud·微服务
SL_staff4 天前
私有化低代码不是减配版开发:JVS双模式建模如何重建IT交付信任
java·低代码·spring cloud
SL_staff5 天前
JVS-APS的7个刚性约束设计:从无限产能幻觉到可执行排产的工程实践
java·spring cloud·全栈
JJJennie7775 天前
MAI Gateway私有化部署全攻略:大模型网关如何接入?附架构图
算法·gateway·ai网关·魔芋ai
Raas1005 天前
MAI Gateway实战部署:AI网关私有化部署?从0到1上手指南
人工智能·gateway·mai gateway·企业级ai产品
Raas1005 天前
AI网关如何接入?MAI Gateway部署实操教程
java·开发语言·人工智能·gateway·ai网关·mai gateway·企业级产品
逃逸线LOF6 天前
Ribbon负载均衡原理(Irule组件、源码分析)
后端·spring cloud·ribbon