Spring Cloud Gateway--路由的匹配规则

原文网址:Spring Cloud Gateway--路由的匹配规则_IT利刃出鞘的博客-CSDN博客

简介

说明

        本文介绍Spring Cloud Gateway的路由的匹配规则。

概述

        Spring Cloud Gateway的主要功能之⼀是转发请求,转发规则的定义主要包含三个部分,如表所示。

说明

Route(路由)

路由是⽹关的基本单元,由ID、URI、⼀组Predicate、⼀组Filter组成,根据Predicate进⾏匹配转发。

Predicate(谓语、断⾔)

路由转发的判断条件,⽬前SpringCloud Gateway⽀持多种⽅式,常⻅如:Path、Query、Method、Header等,写法必须遵循 key=vlue的形式

Filter(过滤器)

过滤器是路由转发请求时所经过的过滤逻辑,可⽤于修改请求、响应内容

Predicate 断⾔条件

        Predicate 就是为了实现⼀组匹配规则,⽅便让请求过来找到对应的 Route 进⾏处理,接下来我们接下 Spring Cloud GateWay 内置⼏种 Predicate 的使⽤。转发规则(predicates),假设 转发uri都设定为http://localhost:9001,常⻅Predicate,如表所示

规则

实例

说明

Path

- Path=/gate/,/rule/

当请求的路径为gate、rule开头的时,转发到 http://localhost:9001服务器上

Before

- Before=2017-01-20T17:42:47.789-07:00[America/Denver]

在某个时间之前的请求才会被转发到 http://localhost:9001 服务器上

After

- After=2017-01-20T17:42:47.789- 07:00[America/Denver]

在某个时间之后的请求才会被转发

Between

- Between=2017-01-20T17:42:47.789-07:00[America/Denver],2017-01- 21T17:42:47.789-07:00[America/Denver]

在某个时间段之间的才会被转发

Cookie

- Cookie=chocolate, ch.p

名为chocolate的表单或者满⾜正则ch.p的表单才会被匹配到 进⾏请求转发

Header

- Header=X-Request-Id, \d+

携带参数X-Request-Id或者满⾜\d+的请求头才会匹配

Host

- Host=www.hd123.com

当主机名为www.hd123.com的时候直接转发到 http://localhost:9001服务器上

Method

- Method=GET

只有GET⽅法才会匹配转发请求,还可以限定POST、PUT等

通过请求参数匹配

Query Route Predicate ⽀持传⼊两个参数,⼀个是属性名⼀个为属性值,属性值可以是正则表达 式

spring:
  cloud:
    gateway:
      routes:
        - id: service3
          uri: https://www.baidu.com
          order: 0
          predicates:
             - Query=smile

        这样配置,只要请求中包含 smile 属性的参数即可匹配路由。使⽤ curl 测试,命令⾏输⼊:curl localhost:9005?smile=x&id=2,经过测试发现只要请求汇总带有 smile 参数即会匹配路由,不带 smile 参数则不会匹配。 还可以将 Query 的值以键值对的⽅式进⾏配置,这样在请求过来时会对属性值和正则进⾏匹配,匹配上才会⾛路由。

spring:
  cloud:
    gateway:
      routes:
        - id: service3
          uri: https://www.baidu.com
          order: 0
          predicates:
             - Query=keep, pu.

        这样只要当请求中包含 keep 属性并且参数值是以 pu 开头的⻓度为三位的字符串才会进⾏匹配和路由。 使⽤ curl 测试,命令⾏输⼊:curl localhost:8080?keep=pub,测试可以返回⻚⾯代码,将 keep 的属 性值改为 pubx 再次访问就会报 404,证明路由需要匹配正则表达式才会进⾏路由。

通过Header匹配

        Header Route Predicate 和 Query Route Predicate ⼀样,也是接收 2 个参数,⼀个 header 中属性名称和⼀个正则表达式,这个属性值和正则表达式匹配则执⾏。

spring:
  cloud:
    gateway:
      routes:
        - id: service4
          uri: https://www.baidu.com
          order: 0
          predicates:
             - Header=X-Request-Id, \d+

        使⽤ curl 测试,命令⾏输⼊:curl http://localhost:9005 -H “X-Request-Id:88”,则返回⻚⾯代 码证明匹配成功。将参数-H "X-Request-Id:88"改为-H “X-Request-Id:spring”,再次执⾏时返回 404证明没有匹配。

通过Cookie匹配

        Cookie Route Predicate 可以接收两个参数,⼀个是 Cookie name ,⼀个是正则表达式,路由规则会通过获取对应的 Cookie name 值和正则表达式去匹配,如果匹配上就会执⾏路由,如果没有匹配 上则不执⾏。

spring:
  cloud:
    gateway:
      routes:
        - id: service5
          uri: https://www.baidu.com
          predicates:
             - Cookie=sessionId, test

        使⽤ curl 测试,命令⾏输⼊,curl http://localhost:9005 --cookie “sessionId=test”,则会返回⻚⾯代码,如果去掉–cookie “sessionId=test”,后台汇报 404 错误。

通过Host匹配

        Host Route Predicate 接收⼀组参数,⼀组匹配的域名列表,这个模板是⼀个 ant 分隔的模板, ⽤.号作为分隔符。它通过参数中的主机地址作为匹配规则。

spring:
  cloud:
    gateway:
      routes:
        - id: service6
          uri: https://www.baidu.com
          predicates:
             - Host=**.baidu.com

        使⽤ curl 测试,命令⾏输⼊,curl http://localhost:9005 -H "Host: www.baidu.com"或者curl http://localhost:8080 -H “Host: md.baidu.com”,经测试以上两种 host 均可匹配到 host_route 路由,去掉 host 参数则会报 404 错误。

通过请求⽅式匹配

spring:
  cloud:
    gateway:
      routes:
        - id: service7
          uri: https://www.baidu.com
          predicates:
             - Method=PUT

        使⽤ curl 测试,命令⾏输⼊,curl -X PUT http://localhost:9005,测试返回⻚⾯代码,证明匹配 到路由,以其他⽅式,返回 404 没有找到,证明没有匹配上路由

通过请求路径匹配

Path RoutePredicate 接收⼀个匹配路径的参数来判断是否路由。

spring:
  cloud:
    gateway:
      routes:
        - id: service8
          uri: https://www.baidu.com
          predicates:
             - Path=/payment/{segment}

        如果请求路径符合要求,则此路由将匹配, curl 测试,命令⾏输⼊,curl http://localhost:9005/payment/1, 可以正常获取到⻚⾯返回值,curl http://localhost:9005/payment2/1,报404,证明路由是通过指定路由来匹配

组合匹配

spring:
  cloud:
    gateway:
      routes:
        - id: service9
          uri: http://127.0.0.1:9001
          predicates:
             - Host=**.foo.org
             - Path=/headers
             - Method=GET
             - Header=X-Request-Id, \d+
             - Query=foo, ba.
             - Query=baz
             - Cookie=chocolate, ch.p

        各种 Predicates 同时存在于同⼀个路由时,请求必须同时满⾜所有的条件才被这个路由匹配。 ⼀个请求满⾜多个路由的断⾔条件时,请求只会被⾸个成功匹配的路由转发

过滤器规则

列举⼏个过滤器,如表所示。

过滤规则

实例

说明

PrefixPath

- PrefixPath=/app

在请求路径前加上app

RewritePath

- RewritePath=/test, /app/test

访问localhost:9022/test,请求会转发到 localhost:8001/app/test

SetPath

SetPath=/app/{path}

通过模板设置路径,转发的规则时会在路径前增加 app,{path}表示原请求路径

RedirectTo

重定向

RemoveRequestHea

去掉某个请求头信息\1. PrefixPath

PrefixPath

对所有的请求路径添加前缀

spring:
  cloud:
    gateway:
      routes:
        - id: service10
          uri: http://127.0.0.1:9001
          predicates:
             - Path=/payment/{segment}
          filters:
           - PrefixPath=/payment

访问/123请求被发送到http://127.0.0.1:9001/payment/123。

StripPrefix

跳过指定的路径

spring:
  cloud:
    gateway:
      routes:
        - id: service11
          uri: http://127.0.0.1:9001
          predicates:
             - Path=/payment/{segment}
          filters:
           - StripPrefix=1
           - PrefixPath=/payment

        此时访问http://localhost:9005/api/123,⾸先StripPrefix过滤器去掉⼀个/api,然后PrefixPath 过滤器加上⼀个/payment,能够正确访问到微服务。

RewritePath

spring:
  cloud:
    gateway:
      routes:
        - id: service12
          uri: http://127.0.0.1:9001
          predicates:
             - Path=/payment/{segment}
          filters:
           - RewritePath=/api/(?<segment>.*), /$\{segment}

请求http://localhost:9005/api/payment/123路径,RewritePath过滤器将路径重写为 http://localhost:9005/payment/123,能够正确访问微服务。

SetPath

SetPath和Rewrite类似,代码如下

spring:
  cloud:
    gateway:
      routes:
        - id: service13
          uri: http://127.0.0.1:9001
          predicates:
             - Path=/payment/{segment}
          filters:
           - SetPath=/payment/{segment}

        请求http://localhost:9005/api/payment/123路径,SetPath过滤器将路径设置为 http://localhost:9005/payment/123,能够正确访问微服务。

RemoveRequestHeader

去掉某个请求头信息。

spring:
  cloud:
    gateway:
      routes:
        - id: removerequestheader_route
          uri: https://example.org  
          filters:
           - RemoveRequestHeader=X-Request-Foo 12345678

去掉请求头X-Request-Foo

RemoveResponseHeader

去掉某个回执头信息

spring:
  cloud:
    gateway:
      routes:
        - id: removerequestparameter_route
          uri: https://example.org  
          filters:
           - RemoveRequestParameter=red

SetRequestHeader

设置请求头信息

spring:
  cloud:
    gateway:
      routes:
        - id: setrequestheader_route
          uri: https://example.org  
          filters:
           - SetRequestHeader=X-Request-Red, Blue

default-filters

对所有的请求添加过滤器

spring:
  cloud:
    gateway:
      routes:
        - id: service14
          uri: http://127.0.0.1:9001
          predicates:
                - Path=/9001/{segment}
        - id: service15
          uri: http://127.0.0.1:9000
          predicates:
                - Path=/9000/{segment}
      default-filters:
        - StripPrefix=1
        - PrefixPath=/payment

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/125560954