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的才满足要求
相关推荐
飞火流星020272 天前
SkyWalking异步采集spring gateway日志
gateway·skywalking·日志监控·gateway链路监控
要开心吖ZSH2 天前
【Spring Cloud Gateway 实战系列】高级篇:服务网格集成、安全增强与全链路压测
spring cloud·微服务·gateway·istio
德育处主任Pro2 天前
解放生产力:Amazon API Gateway 与 Amazon Lambda 的优雅组合
gateway·aws·亚马逊
java叶新东老师2 天前
spring gateway 配置http和websocket路由转发规则
spring·http·gateway
java叶新东老师2 天前
五、搭建springCloudAlibaba2021.1版本分布式微服务-gateway网关
分布式·微服务·gateway
云和数据.ChenGuang3 天前
`neutron router-gateway-set` 操作失败的可能原因及解决方案
运维·gateway·运维技术总结·运维技术
要开心吖ZSH5 天前
【Spring Cloud Gateway 实战系列】终极篇:演进方向与未来架构
spring cloud·gateway
@小了白了兔5 天前
统一服务入口——Spring Cloud Gateway
java·spring cloud·gateway
要开心吖ZSH5 天前
【Spring Cloud Gateway 实战系列】进阶篇:过滤器高级用法、动态路由配置与性能优化
spring cloud·性能优化·gateway·负载均衡
亲爱的非洲野猪6 天前
Spring Cloud Gateway 电商系统实战指南:架构设计与深度优化
java·spring cloud·gateway