Spring Cloud Gateway路由及过滤规则

路由

Spring Cloud Gateway包含许多内置的路由断言Factories。这些断言都匹配HTTP请求的不同属性。

After 路由断言

After Route Predicate 在该日期时间之后发生的请求都将被匹配(UTC日期个数xxxx-xx-xxTxx:xx:xx+08:00[Asia/Shanghai])

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: http://localhost:8080
        predicates:
        - After=2022-03-30T18:00:00+08:00[Asia/Shanghai]

Before 路由断言

Before Route Predicate 在该日期时间之前发生的请求都将被匹配

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: http://localhost:8080
        predicates:
        - Before=2022-03-30T18:00:00+08:00[Asia/Shanghai]

Between 路由断言

Before Route Predicate 有两个参数,x和y。在x和y之间的请求将被匹配。y参数的实际时间必须在x之后。

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: http://localhost:8080
        predicates:
        - Between=2022-03-30T18:00:00+08:00[Asia/Shanghai], 2022-03-31T18:00:00+08:00[Asia/Shanghai]

Cookie路由断言

Cookie Route Predicate 有两个参数,cookie名称和正则表达式。请求包含次cookie名称且正则表达式匹配的将会被匹配。

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: http://localhost:8080
        predicates:
        - Cookie=token, \d

Header路由断言

Header Route Predicate 有两个参数,header名称和正则表达式。请求包含header名称且正则表达式匹配的将会被匹配。

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: http://localhost:8080
        predicates:
        - Header=X-Request-Id, \d+

Host 路由断言

Host Route Predicate 包括一个参数:host name列表。使用Ant path匹配规则。‘,’作为分隔符。

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://localhost:8080
        predicates:
        - Host=**.somehost.org,**.anotherhost.org

Ant path匹配规则:

符号描述
匹配任何单字符
*匹配0或者任意数量的字符
**匹配0或者更多的目录

Method 路由断言

Method Route Predicate 包括一个参数: 需要匹配的HTTP请求方式。‘,’作为分隔符。

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://localhost:8080
        predicates:
        - Method=GET,POST

Path 路由断言

Path Route Predicate 有2个参数: 一个Spring PathMatcher表达式列表和可选matchOptionalTrailingSeparator标识 .

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: http://localhost:8080
        predicates:
        - Path=/foo/{segment},/bar/{segment}

URI 模板变量 (如上例中的 segment ) 将以Map的方式保存于ServerWebExchange.getAttributes()。key为ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE. 这些值将在GatewayFilter Factories使用

Map<String, String> map = ServerWebExchangeUtils.getPathPredicateVariables(exchange);
String segment = map .get("segment");

Query 路由断言

Query Route Predicate 有2个参数: 必选项 param 和可选项 regexp.

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://localhost:8080
        predicates:
        - Query=abc,ac. //请求包含abc参数,并且值匹配为ac.正则表达式 可以访问

RemoteAddr路由断言

RemoteAddr Route Predicate 的参数为IP地址

spring:
  cloud:
    gateway:
      routes:
      - id: remoteAddr_route
        uri: http://localhost:8080
        predicates:
        - RemoteAddr=192.168.1.1

Weight路由断言

Weight Route Predicate 的参数有两个group和weight。按组分配权重

spring:
  cloud:
    gateway:
      routes:
      - id: weight_route1
        uri: http://localhost:8080
        predicates:
        - Weight=group1,8 # 80%
      - id: weight_route2
        uri: http://localhost:8080
        predicates:
        - Weight=group2,2 # 20%

过滤

Gateway分为pre类型的过滤器和post类型的过滤器

  • pre类型的过滤在转发到后端微服务之前执行,可以做鉴权限流等操作
  • post类型的过滤在请求完成之后、将结果范围给客户端之前执行

大概可以分为以下几类:

  1. Header
  2. Parameter
  3. Path
  4. Body
  5. Status
  6. Session
  7. Redirect
  8. Retry
  9. RateLimiter
  10. Hystrix

常用的四种过滤器:

AddRequestParameter Gateway Filter

添加请求参数,属于前置过滤。两个参数:key和value

spring:
  cloud:
    gateway:
      routes:
      - id: route1
        uri: http://localhost:8080
        predicates:
        - Path=/product/**
        filters:
        - AddRequestParameter=info,hehe

RewritePath Gateway Filter

将请求路径重写如:/api-gateway/xxx/重写为/xxx/,属于前置过滤。

spring:
  cloud:
    gateway:
      routes:
      - id: route1
        uri: http://localhost:8080
        predicates:
        - Path=/product/**,/api-gateway/**
        filters:
        - RewritePath=/api-gateway(?<segment>/?.*),$\{segment}

SetStatus Gateway Filter

将响应的HTTP请求头设置为454,属于后置过滤

spring:
  cloud:
    gateway:
      routes:
      - id: route1
        uri: http://localhost:8080
        predicates:
        - Path=/product/**,/api-gateway/**
        filters:
        - SetStatus=454

AddResponsHeader Gateway Filter

添加头信息,属于后置过滤

spring:
  cloud:
    gateway:
      routes:
      - id: route1
        uri: http://localhost:8080
        predicates:
        - Path=/product/**,/api-gateway/**
        filters:
        - AddResponsHeader=X-Response-Author,test
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值