返回介绍

9.2 核心配置

发布于 2025-04-21 20:58:46 字数 9435 浏览 0 评论 0 收藏

Spring Cloud 网关路由匹配是 Spring WebFlux HandlerMapping 处理的一部分。Spring Cloud 网关包含许多内置的路由断言工厂,这些内置断言可以匹配 HTTP 请求的不同属性,同时多个断言可以组合在一起使用。本节主要讲解 Spring Cloud Gateway 中的一些常用配置。

9.2.1 Route Predicate 配置

1. After Route Predicate Factory 配置

After Route Predicate Factory 接收一个时间参数,当在指定时间之后进行请求时,匹配 After Route Predicate 断言。下面是 After Route Predicate 断言配置示例,代码如下:

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

2. Before Route Predicate Factory 配置

Before Route Predicate Factory 接收一个时间参数,当在指定时间之前进行请求时,匹配 Before Route Predicate 断言。下面是 Before Route Predicate 断言配置示例代码:

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

3. Between Route Predicate Factory 配置

Between Route Predicate Factory 接收两个时间参数。当请求时间在两个时间参数之间时,匹配 Between Route Predicate 断言。代码如下:

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]

4. Cookie Route Predicate Factory 配置

Cookie Route Predicate Factory 接收两个参数,一个是 cookie 名称,另一个是正则表达式。当请求中的 cookie 名称和值匹配断言设置的 cookie 名称和正则表达式时通过。下面的这个例子中,断言要求的 cookie 名称是 chocolate,cookie 的值匹配 ch.p 正则表达式。

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

5. Header Route Predicate Factory 配置

Header Route Predicate Factory 接收两个参数,一个是 header 名称,另一个是正则表达式。当请求中的 cookie 名称和值匹配断言设置的 header 名称和正则表达式时通过。下面的这个例子中,断言要求的 header 名称是 X-Request-Id,header 属性值匹配\d+正则表达式。

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

6. Host Route Predicate Factory 配置

Host Route Predicate Factory 接收一个参数,参数值包含一系列域名。下面的示例代码可以匹配 www.somehost.org、beta.somehost.org 或 www.anotherhost.org 域名。

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

7. Method Route Predicate Factory 配置

Method Route Predicate Factory 接收一个 HTTP 方法参数,可以有一个或多个值。匹配 GET 和 POST 方法的请求,示例代码如下:

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

8. Path Route Predicate Factory 配置

Path Route Predicate Factory 匹配请求路径的表达式。匹配/red/1、/red/blue 和/blue/green 请求的示例代码如下:

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

9. Query Route Predicate Factory 配置

Query Route Predicate Factory 接收两个参数,一个是必传参数,另一个是可选表达式。在下面的例子中,要求请求需要匹配 red 参数及 gree.表达式参数。

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

10. RemoteAddr Route Predicate Factory 配置

Remoteaddr Route Predicate Factory 接收一系列 IPv4/IPv6 地址。匹配 192.168.1.10 地址请求的示例代码如下:

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

11. Weight Route Predicate Factory 配置

Weight Route Predicate Factory 接收两个参数,即 group 和 weight。下面的例子表示 80%的请求被路由到 weighthigh.org 域名,20%的请求被路由调用到 weighlow.org 域名。

spring:
  cloud:
    gateway:
      routes:
      -id: weight_high
        uri: https://weighthigh.org
        predicates:
        -Weight=group1, 8
      -id: weight_low
        uri: https://weightlow.org
        predicates:
        -Weight=group1, 2

9.2.2 GatewayFilter 配置

路由过滤器允许修改 HTTP 请求和 HTTP 响应数据。Spring Cloud 网关内置了很多过滤器。本节主要介绍这些内置的过滤器与匹配方式。

1. AddRequestHeader GatewayFilter Factory 配置

AddRequestHeader GatewayFilter Factory 接收两个参数:name 和 value。为请求添加 X-Request-red:blue header 属性的示例代码如下:

spring:
  cloud:
    gateway:
      routes:
      -id: add_request_header_route
        uri: https://example.org
        filters:
        -AddRequestHeader=X-Request-red, blue.

2. AddRequestParameter GatewayFilter Factory 配置

AddRequestParameter GatewayFilter Factory 可以为 HTTP 请求增加请求参数。为请求添加 red=blue 参数的示例代码如下:

spring:
  cloud:
    gateway:
      routes:
      -id: add_request_parameter_route
        uri: https://example.org
        filters:
        -AddRequestParameter=red, blue.

3. AddResponseHeader GatewayFilter Factory 配置

AddResponseHeader GatewayFilter Factory 为 HTTP 响应头添加属性。为请求响应头添加 X-Response-Foo:Bar 属性的示例代码如下:

spring:
  cloud:
    gateway:
      routes:
      -id: add_response_header_route
        uri: https://example.org
        filters:
        -AddResponseHeader=X-Response-Foo, Bar

4. MapRequestHeader GatewayFilter Factory 配置

MapRequestHeader GatewayFilter Factory 会将 fromHeader 中的值添加到 toHeader 中。将 Blue 的属性值添加到 X-Request-Red 头属性中,示例代码如下:

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

5. PrefixPath GatewayFilter Factory 配置

PrefixPath GatewayFilter Factory 为请求路径添加前缀。为请求的路径添加/mypath 路径,例如请求/hello,则被修改为/mypath/hello,示例代码如下:

spring:
  cloud:
    gateway:
      routes:
      -id: prefixpath_route
        uri: https://example.org
        filters:
        -PrefixPath=/mypath

6. RedirectTo GatewayFilter Factory 配置

RedirectTo GatewayFilter Factory 是重定向配置。例如,返回状态码 302,并且重定向到 https://acme.org,代码如下:

spring:
  cloud:
    gateway:
      routes:
      -id: prefixpath_route
        uri: https://example.org
        filters:
        -RedirectTo=302, https://acme.org

7. RemoveRequestHeader GatewayFilter Factory 配置

RemoveRequestHeader GatewayFilter Factory 用于删除请求中的特定 header。例如,删除 X-Request-Foo 属性,代码如下:

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

8. RemoveResponseHeader GatewayFilter Factory 配置

RemoveResponseHeader GatewayFilter Factory 用于删除响应头属性。例如,删除响应头中的 X-Response-Foo 属性,代码如下:

spring:
  cloud:
    gateway:
      routes:
      -id: removeresponseheader_route
        uri: https://example.org
        filters:
        -RemoveResponseHeader=X-Response-Foo

9. RemoveRequestParameter GatewayFilter Factory 配置

RemoveRequestParameter GatewayFilter Factory 用于删除特定的请求参数。例如,删除请求参数 red,代码如下:

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

10. Retry GatewayFilter Factory 配置

Retry GatewayFilter Factory 提供请求重试配置。例如,一个简单的重试配置方式的代码如下:

spring:
  cloud:
    gateway:
      routes:
      -id: retry_test
        uri: http://localhost:8080/flakey
        predicates:
        -Host=*.retry.com
        filters:
        -name: Retry
          args:
            retries: 3
            statuses: BAD_GATEWAY
            methods: GET,POST
            backoff:
              firstBackoff: 10ms
              maxBackoff: 50ms
              factor: 2
              basedOnPreviousValue: false

11. FallbackHeaders GatewayFilter Factory 配置

FallbackHeaders GatewayFilter Factory 可以提供熔断降级功能。Fallback 配置方式的示例代码如下:

spring:
  cloud:
    gateway:
      routes:
      -id: retry_test
        uri: http://localhost:8080/flakey
        predicates:
        -Host=*.retry.com
        filters:
        -name: Retry
          args:
            retries: 3
            statuses: BAD_GATEWAY
            methods: GET,POST
            backoff:
              firstBackoff: 10ms
              maxBackoff: 50ms
              factor: 2
              basedOnPreviousValue: false

12. RequestRateLimiter GatewayFilter Factory 配置

RequestRateLimiter GatewayFilter Factory 提供请求重试配置。一个简单的重试配置方式的示例代码如下:

spring:
  cloud:
    gateway:
      routes:
      -id: retry_test
        uri: http://localhost:8080/flakey
        predicates:
        -Host=*.retry.com
        filters:
        -name: Retry
          args:
            retries: 3
            statuses: BAD_GATEWAY
            methods: GET,POST
            backoff:
              firstBackoff: 10ms
              maxBackoff: 50ms
              factor: 2
              basedOnPreviousValue: false

9.2.3 全局配置

Spring Cloud 网关除了核心配置外,还提供了其他的全局配置信息,如超时配置和 CORS 配置。

超时配置方式的示例代码如下:

spring:
  cloud:
    gateway:
      httpclient:
        connect-timeout: 1000
        response-timeout: 5s

CORS 配置方式的示例代码如下:

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "https://docs.spring.io"
            allowedMethods:
            -GET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。