java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > GateWay路由规则

GateWay路由规则与动态路由详细介绍

作者:悠然予夏

这篇文章主要介绍了GateWay路由规则与GateWay动态路由,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

1、路由规则

Spring Cloud GateWay 帮我们内置了很多 Predicates功能,实现了各种路由匹配规则(通过 Header、请求参数等作为条件)匹配到对应的路由。

时间点后匹配

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

时间点前匹配

spring:
    cloud:
        gateway:
            routes:
                - id: before_route
                uri: https://example.org
                predicates:
                    - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

时间区间匹配

spring:
    cloud:
        gateway:
            routes:
                - id: between_route
                uri: https://example.org
                predicates:
                    - Between=2017-01-20T17:42:47.789-07:00[America/Denver],2017-01-21T17:42:47.789-07:00[America/Denver]

指定Cookie正则匹配指定值

spring:
    cloud:
        gateway:
            routes:
                - id: cookie_route
                uri: https://example.org
                predicates:
                    - Cookie=chocolate, ch.p

指定Header正则匹配指定值

spring:
    cloud:
        gateway:
            routes:
                - id: header_route
                uri: https://example.org
                predicates:
                    - Header=X-Request-Id, \d+

请求Host匹配指定值

spring:
    cloud:
        gateway:
            routes:
                - id: host_route
                uri: https://example.org
                predicates:
                    - Host=**.somehost.org,**.anotherhost.org

请求Method匹配指定请求方式

spring:
    cloud:
        gateway:
            routes:
                - id: method_route
                uri: https://example.org
                predicates:
                    - Method=GET,POST

请求路径正则匹配

spring:
    cloud:
        gateway:
            routes:
                - id: path_route
                uri: https://example.org
                predicates:
                    - Path=/red/{segment},/blue/{segment}

请求包含某参数

spring:
    cloud:
        gateway:
            routes:
                - id: query_route
                uri: https://example.org
                predicates:
                    - Query=green

请求包含某参数并且参数值匹配正则表达式

spring:
    cloud:
        gateway:
            routes:
                - id: query_route
                uri: https://example.org
                predicates:
                    - Query=red, gree.

远程地址匹配

spring:
    cloud:
        gateway:
            routes:
                - id: remoteaddr_route
                uri: https://example.org
                predicates:
                    - RemoteAddr=192.168.1.1/24

2、动态路由

GateWay支持自动从注册中心中获取服务列表并访问,即所谓的动态路由

实现步骤如下

注意:动态路由设置时,uri以 lb: //开头(lb代表从注册中心获取服务),后面是需要转发到的服务名称

到此这篇关于GateWay路由规则与动态路由详细介绍的文章就介绍到这了,更多相关GateWay路由规则内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文