其他分享
首页 > 其他分享> > Spring Cloud Gateway实战之四:内置predicate小结

Spring Cloud Gateway实战之四:内置predicate小结

作者:互联网

欢迎访问我的GitHub

https://github.com/zq2599/blog_demos

内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;

本篇概览

After

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: http://127.0.0.1:8082
        predicates:
        - After=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
[
    {
        "id": "after_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "After",
                "args": {
                    "datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]"
                }
            }
        ]
    }
]

Before

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: http://127.0.0.1:8082
        predicates:
        - Before=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
[
    {
        "id": "before_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Before",
                "args": {
                    "datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]"
                }
            }
        ]
    }
]

Between

spring:
  application:
    name: hello-gateway
  cloud:
    gateway:
      routes:
        - id: between_route
          uri: http://127.0.0.1:8082
          predicates:
            - Between=2021-08-16T07:36:00.000+08:00[Asia/Shanghai], 2021-08-16T08:15:00.000+08:00[Asia/Shanghai]
[
    {
        "id": "path_route_addr",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Between",
                "args": {
                    "datetime1": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]",
                    "datetime2": "2021-08-16T08:18:00.000+08:00[Asia/Shanghai]"
                }
            }
        ]
    }
]
spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p
[
    {
        "id": "cookie_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Cookie",
                "args": {
                	"name": "chocolate",
                    "regexp": "ch.p"
                }
            }
        ]
    }
]
spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+
[
    {
        "id": "header_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Header",
                "args": {
                    "header": "X-Request-Id",
                    "regexp": "\\d+"
                }
            }
        ]
    }
]

在这里插入图片描述

Host

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://127.0.0.1:8082
        predicates:
        - Host=test.com:8081,**.anotherhost.org
[
    {
        "id": "header_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Host",
                "args": {
                    "regex": "test.com:8086"
                }
            }
        ]
    }
]

Method

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://127.0.0.1:8082
        predicates:
        - Method=GET,POST
[
    {
        "id": "method_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Method",
                "args": { 
                    "methods": "GET"
                }
            }
        ]
    }
]

Path

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: http://127.0.0.1:8082
        predicates:
        - Path=/hello/{segment},/lbtest/{segment}
[
    {
        "id": "path_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Path",
                "args": { 
                    "pattern": "/hello/{segment}"
                }
            }
        ]
    }
]

Query

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://127.0.0.1:8082
        predicates:
        - Query=name
spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://127.0.0.1:8082
        predicates:
        - Query=name,aaa.
[
    {
        "id": "query_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "Query",
                "args": { 
                    "param": "name",
                    "regexp": "aaa."
                }
            }
        ]
    }
]

在这里插入图片描述

RemoteAddr

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: http://127.0.0.1:8082
        predicates:
        - RemoteAddr=192.168.50.134/24
[
    {
        "id": "remoteaddr_route",
        "uri": "http://127.0.0.1:8082",
        "predicates":[
            {
                "name": "RemoteAddr",
                "args": { 
                    "sources": "192.168.50.134/24"
                }
            }
        ]
    }
]

在这里插入图片描述

Weight

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: http://192.168.50.134:8082
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: http://192.168.50.135:8082
        predicates:
        - Weight=group1, 2

你不孤单,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

欢迎关注公众号:程序员欣宸

微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos

标签:predicate,http,8082,Spring,route,uri,predicates,之四,id
来源: https://www.cnblogs.com/bolingcavalry/p/15565407.html