gateway断言配置详解

一、Predicate - 断⾔

1、简单用法

XML 复制代码
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

2、自定义断言

新建类VipRoutePredicateFactory,注意VipRoutePredicateFactory名字的前缀和yml文件的配置必须一致,即yml文件必须配置为前缀Vip

java 复制代码
import jakarta.validation.constraints.NotEmpty;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
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 VipRoutePredicateFactory extends AbstractRoutePredicateFactory<VipRoutePredicateFactory.Config> {


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

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return new GatewayPredicate() {
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {
                // localhost/search?q=haha&user=leifengyang
                ServerHttpRequest request = serverWebExchange.getRequest();

                String first = request.getQueryParams().getFirst(config.param);

                return StringUtils.hasText(first) && first.equals(config.value);
            }
        };
    }

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

    /**
     * 可以配置的参数
     */
    @Validated
    public static class Config {

        @NotEmpty
        private String param;


        @NotEmpty
        private String value;

        public @NotEmpty String getParam() {
            return param;
        }

        public void setParam(@NotEmpty String param) {
            this.param = param;
        }

        public @NotEmpty String getValue() {
            return value;
        }

        public void setValue(@NotEmpty String value) {
            this.value = value;
        }
    }
}

yml文件配置:

XML 复制代码
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]
        - Vip=user,wangcj  #表示传了参数user为wangcj的才满足要求
相关推荐
Xpower 171 天前
OpenClaw实战:从零开发电商小程序(2)
人工智能·语言模型·小程序·gateway
脱脱克克2 天前
云端 OpenClaw 远程执行本地进程原理机制详解:Gateway、approvals 与 system.run 到底谁在判定、谁在执行?
linux·gateway·openclaw
七夜zippoe3 天前
OpenClaw Gateway 服务:启动、停止、监控
微服务·架构·gateway·监控·openclaw
arvin_xiaoting3 天前
OpenClaw学习总结_I_核心架构系列_Gateway架构详解
学习·架构·llm·gateway·ai-agent·飞书机器人·openclaw
柯儿的天空3 天前
【OpenClaw 全面解析:从零到精通】第007篇:流量枢纽——OpenClaw Gateway 网关深度解析
人工智能·gpt·ai作画·gateway·aigc·ai编程·ai写作
Zhao_yani3 天前
微服务核心组件:Gateway
java·微服务·gateway
gallonyin3 天前
【企业级龙虾】LLM Gateway 工程化落地:配置中心、429故障转移与统计持久化实战
gateway·openclaw
道清茗4 天前
【Kubernetes知识点问答题】Kustomize、CRD 与 Gateway API
容器·kubernetes·gateway
专注_每天进步一点点4 天前
xxop网关 → APISIX集群(ApisixRoute) → 业务gateway模块 和 Serverless架构 区别和联系
架构·serverless·gateway
专注_每天进步一点点4 天前
流量从bcop网关到apisixroute,再到应用的gateway模块,再到其他服务
docker·kubernetes·gateway