SpringCloudGateway配置介绍

做过微服务开发,或者了解、学习过微服务的朋友对Spring Cloud Netflix Zuul肯定不陌生!随着微服务的不断发展Spring Cloud 推出了Spring Cloud Gateway,他相比zuul的功能更强大,入门也相对简单!接下来就开始对Gateway的学习吧

一.Gateway工作方式

在这里插入图片描述
客户端向Spring Cloud Gateway发出请求。如果网关处理程序映射确定请求与路由匹配,则将其发送到网关Web处理程序。该处理程序通过特定于请求的过滤器链来运行请求。筛选器由虚线分隔的原因是,筛选器可以在发送代理请求之前和之后运行逻辑。所有“前置”过滤器逻辑均被执行。然后发出代理请求。发出代理请求后,将运行“后”过滤器逻辑。这里的处理相比Zuul强大很多,可以通过Filter来自定义一些处理方式!

二.配置方式

2.1 根据时间进行请求过滤

After 匹配在指定日期时间之后发生的请求
Before匹配在指定日期时间之前发生的请求
Between 匹配在指定日期时间之内发生的请求

spring:
  cloud:
    gateway:
      routes:
      - id: after_route # 唯一表示,有意义即可
        uri: https://example.org # 服务的访问路径
        predicates:
        - After=2020-02-20T17:42:47.789-07:00[America/Denver]

2.2 Cookie / Header 线路匹配

Cookie / Header工厂采用两个参数,该cookie name和regexp(其是Java正则表达式)

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

此路由匹配具有名称为chocolate与ch.p正则表达式匹配的cookie的请求。

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

如果请求具有名为X-Request-Id其值与\d+正则表达式匹配的标头(即,其值为一个或多个数字),则此路由匹配

2.3 Method 路线匹配

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

是匹配GET ,POST请求

2.4 路径路线匹配

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

如果请求路径是,例如匹配/red/1或/red/blue或/blue/green

2.5 重量路线匹配

该Weight路线谓词工厂有两个参数:group和weight(一个int)。权重是按组计算的

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

这条路线会将大约80%的流量转发到weighthigh.org,将大约20%的流量转发到weightlow.org。

2.6 filters 线路匹配

PrefixPath

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

这将/mypath作为所有匹配请求的路径的前缀。因此,/hello将向发送请求/mypath/hello。
StripPrefix

spring:
  cloud:
    gateway:
      routes:
      - id: nameRoot
        uri: https://nameservice
        predicates:
        - Path=/name/**
        filters:
        - StripPrefix=2

通过网关/name/blue/red发出请求时,发出的请求nameservice看起来像nameservice/red
GatewayFilter工厂
该Retry GatewayFilter工厂支持以下参数:

  • retries:应尝试的重试次数。

  • statuses:应重试的HTTP状态代码,使用表示org.springframework.http.HttpStatus。

  • methods:应该重试的HTTP方法,以表示org.springframework.http.HttpMethod。

  • series:要重试的一系列状态代码,使用表示org.springframework.http.HttpStatus.Series。

  • exceptions:应重试的引发异常的列表。

  • backoff:为重试配置的指数补偿。重试在的退避间隔后执行firstBackoff * (factor ^ n),其中n为迭代。如果maxBackoff已配置,则应用的最大退避限制为- maxBackoff。如果basedOnPreviousValue为true,则使用计算退避prevBackoff * factor。

Retry如果启用了以下默认过滤器配置:

  • retries:3次

  • series:5XX系列

  • methods:GET方法

  • exceptions:IOException和TimeoutException

  • backoff:禁用

以下清单配置了Retry GatewayFilter

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

2.6 根据注册中心内服务名转发

上面的案例都是根据url进行服务转发,我们也可根据服务名进行服务的转发

spring:
  cloud:
    gateway:
      routes:
      - id: myRoute
        uri: lb://service 
        predicates:
        - Path=/service/**

lb:// 这是固定的写法,后面写服务的名称

2.6 超时配置

要配置全局http超时:
connect-timeout必须以毫秒为单位指定。
response-timeout必须指定为java.time.Duration

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

每个路由超时

   - id: per_route_timeouts
        uri: https://example.org
        predicates:
          - name: Path
            args:
              pattern: /delay/{timeout}
        metadata:
          response-timeout: 200
          connect-timeout: 200

2.7 CORS配置

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

在前面的示例中,允许从docs.spring.io所有GET请求路径的源请求发出CORS请求

Gateway的配置方式远远不止这些,更多的配置可以看官方介绍,或者API介绍

本文的分享暂时就到这里,希望对您有所帮助
关注 Java有货领取更多资料

联系小编。微信:372787553,带您进群互相学习
左侧小编微信,右侧获取免费资料
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_38937840/article/details/104682897