springcloud-Finchley学习笔记-gateway路由匹配配置

本文详细介绍了SpringCloud Gateway的路由匹配配置,包括After、Before、Between路由匹配,以及Cookie、Header、Host、Method和Path、Query路由匹配的使用,帮助理解如何根据时间、Cookie、HTTP头、主机名、HTTP方法、路径和查询参数来定向请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

路由匹配

  • After路由匹配:匹配指定时间之后的请求
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: http://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]
  • Before路由匹配:匹配指定时间之前的请求
spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: http://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]
  • Between路由匹配:匹配指定时间区间的请求
spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: http://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路由匹配:匹配指定Cookie的值匹配设定正则表达式的请求
spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: http://example.org
        predicates:
        - Cookie=chocolate, ch.p #匹配cookie名为chocolate的值为ch.p的请求
  • Header路由匹配:匹配指定header的值匹配设定正则表达式的请求
spring:
 cloud:
   gateway:
     routes:
     - id: header_route
       uri: http://example.org
       predicates:
       - Header=X-Request-Id, \d+  #匹配头信息X-Request-Id的值匹配\d+的正则表达式
  • Host路由匹配:Host匹配设定的表达式
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://example.org
        predicates:
        - Host=**.somehost.org
  • Method路由匹配:匹配HttpMethod方法为设定值的请求
spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://example.org
        predicates:
        - Method=GET
  • Path路由匹配:请求路径匹配设定表达式(a Spring PathMatcher pattern)的请求
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://example.org
        predicates:
        - Path=/foo/{segment}   #请求路径匹配路径匹配表达式/foo/{segment}
  • Query路由匹配:请求参数匹配设定值 或表达式的请求
spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://example.org
        predicates:
        - Query=baz #请求参数中包含baz参数
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值