网关(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对象
  • 从而获取到请求的参数、请求方式、请求头等信息
相关推荐
亚林瓜子6 小时前
AWS API Gateway配置日志
云计算·gateway·aws·log·cloudwatch
eternal__day12 小时前
Spring Cloud 多机部署与负载均衡实战详解
java·spring boot·后端·spring cloud·负载均衡
惊鸿一博13 小时前
java_网络服务相关_gateway_nacos_feign区别联系
java·开发语言·gateway
记得开心一点嘛21 小时前
使用MinIO搭建自己的分布式文件存储
分布式·spring cloud·minio
hanniuniu131 天前
网络安全厂商F5推出AI Gateway,化解大模型应用风险
人工智能·web安全·gateway
LI JS@你猜啊1 天前
window安装docker
java·spring cloud·eureka
stormsha1 天前
Proxmox Mail Gateway安装指南:从零开始配置高效邮件过滤系统
服务器·网络·网络安全·gateway
背太阳的牧羊人2 天前
backend 服务尝试连接 qdrant 容器,但失败了,返回 502 Bad Gateway 问题排查
docker·gateway·qdrant
14L2 天前
互联网大厂Java面试:从Spring Cloud到Kafka的技术考察
spring boot·redis·spring cloud·kafka·jwt·oauth2·java面试
小马爱记录2 天前
sentinel规则持久化
java·spring cloud·sentinel