• spring cloud gateway Route配置


    gateway routes规则

    1、After Route

    所有匹配的请求必须在这个时间点之后

    spring:
      cloud:
        gateway:
          routes:
          - id: after_route
            uri: https://example.org
            predicates:
            - After=2022-05-23T19:52:33.736+08:00[Asia/Shanghai] #时间格式为ZonedDateTime.now()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2、Before Route

    所有匹配的请求必须在这个时间点之前

    spring:
      cloud:
        gateway:
          routes:
          - id: before_route
            uri: https://example.org
            predicates:
            - Before=2021-05-23T19:52:33.736+08:00[Asia/Shanghai]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3、Between Route

    所有匹配的请求必须在这两个时间点之内

    spring:
      cloud:
        gateway:
          routes:
          - id: between_route
            uri: https://example.org
            predicates:
            - Between=2022-05-23T19:52:33.736+08:00[Asia/Shanghai], 2023-05-23T19:52:33.736+08:00[Asia/Shanghai] #两个时间点之间不能用空格隔开
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、Cookie Route

    接收Cookie参数,且值需要与正则表达是匹配

    spring:
      cloud:
        gateway:
          routes:
          - id: cookie_route
            uri: https://example.org
            predicates:
            - Cookie=chocolate, ch.p
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5、Header Route

    在请求Header中,需要带有X-Request-Id的参数,且值需要和正则表达式匹配

    spring:
      cloud:
        gateway:
          routes:
          - id: header_route
            uri: https://example.org
            predicates:
            - Header=X-Request-Id, d+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    6、Host Route

    接收一个参数,主机名模式,判断请求的host是否满足匹配规则

    spring:
      cloud:
        gateway:
          routes:
          - id: host_route
            uri: https://example.org
            predicates:
            - Host=**.somehost.org,**.anotherhost.org
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    7、Method Route

    接收一个参数Method,表示请求的方式匹配参数

    spring:
      cloud:
        gateway:
          routes:
          - id: method_route
            uri: https://example.org
            predicates:
            - Method=GET,POST
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    8、Path Route

    接收一个路径集合,表示请求的路径需要匹配Path中的路径

    spring:
      cloud:
        gateway:
          routes:
          - id: path_route
            uri: https://example.org
            predicates:
            - Path=/red/{segment},/blue/{segment}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    9、Query Route

    接收两个参数,请求param和正则表达式,判断请求参数是否具有给定名称且值与正则表达式

    spring:
      cloud:
        gateway:
          routes:
          - id: query_route
            uri: https://example.org
            predicates:
            - Query=green
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    10、RemoteAddr Route

    路由谓词工厂采用的RemoteAddr列表(最小大小为 1)sources,它们是 CIDR 表示法(IPv4 或 IPv6)字符串,例如192.168.0.1/16(其中192.168.0.1是 IP 地址和16子网掩码)。以下示例配置 RemoteAddr 路由谓词:

    spring:
      cloud:
        gateway:
          routes:
          - id: remoteaddr_route
            uri: https://example.org
            predicates:
            - RemoteAddr=192.168.1.1/24
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    11、 Weight Route

    接收一组【组名,权重】,然后对于同一个组内的路由按照权重转发

    spring:
      cloud:
        gateway:
          routes:
          - id: weight_high
            uri: https://weighthigh.org
            predicates:
            - Weight=group1, 8
          - id: weight_low
            uri: https://weightlow.org
            predicates:
            - Weight=group1, 2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    12、 XForwardedRemoteAddr Route

    路由谓词工厂采用的XForwarded Remote Addr列表(最小大小为 1)sources,它们是 CIDR 表示法(IPv4 或 IPv6)字符串,例如192.168.0.1/16(其中192.168.0.1是 IP 地址和16子网掩码)。

    此路由谓词允许根据X-Forwarded-ForHTTP 标头过滤请求。

    这可以与反向代理一起使用,例如负载平衡器或 Web 应用程序防火墙,其中仅当请求来自这些反向代理使用的受信任的 IP 地址列表时才允许请求。

    spring:
      cloud:
        gateway:
          routes:
          - id: xforwarded_remoteaddr_route
            uri: https://example.org
            predicates:
            - XForwardedRemoteAddr=192.168.1.1/24
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    11、自定义路由断言工厂

    1、必须是Spring的Bean

    2、类必须以RoutePredicateFactory结尾(约定大于配置)

    3、必须继承AbstractRoutePredicateFactory

    4、必须声明静态内部类 声明属性来接收 配置文件中对应的断言的信息

    5、需要结合shortcutFieldOrder进行绑定

    6、通过apply进行逻辑判断 true就是匹配成功 false匹配失败

    package com.wwh.blog.config;
    
    import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
    import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
    import org.springframework.stereotype.Component;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.server.ServerWebExchange;
    
    import javax.validation.constraints.NotEmpty;
    import java.util.Collections;
    import java.util.List;
    import java.util.Objects;
    import java.util.function.Predicate;
    
    @Component
    public class TokenRoutePredicateFactory extends AbstractRoutePredicateFactory {
    
        public TokenRoutePredicateFactory() {
            super(Config.class);
        }
    
        @Override
        public Predicate apply(Config config) {
            return new GatewayPredicate() {
                @Override
                public boolean test(ServerWebExchange exchange) {
                    String token = config.getToken();
                    if (Objects.isNull(token) && !token.equals("auth")) {
                        return false;
                    }
                    // there is a value and since regexp is empty, we only check existence.
                    return true;
                }
    
                @Override
                public Object getConfig() {
                    return config;
                }
    
                @Override
                public String toString() {
                    return String.format("Token: %s ", config.token);
                }
    
            };
        }
    
        @Override
        public List shortcutFieldOrder() {
            return Collections.singletonList("token");
        }
    
        @Validated
        public static class Config {
            @NotEmpty
            private String token;
    
            public String getToken() {
                return token;
            }
    
            public void setToken(String token) {
                this.token = token;
            }
    
            public Config(String token) {
                this.token = token;
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    MMSegmentation训练自己的语义分割数据集
    把握现在,热爱生活
    两个线程交替执行的几种方式
    在 Apache Sedona 中处理空间栅格数据
    apk获取MD5方式记录
    【springboot】1、快速入门
    大小端问题(存储模式)
    安科瑞基于物联网技术的基站能耗监控解决方案-Susie 周
    【Linux】屏蔽项目服务非正常日志输出到message文件中
    Conda 创建、激活、克隆、删除虚拟环境
  • 原文地址:https://blog.csdn.net/hjseo_seg/article/details/126114117