Spring Cloud Gateway断言及过滤器使用

导读:本篇文章讲解 Spring Cloud Gateway断言及过滤器使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

目录

1. 引入Spring Cloud Gateway

2. 核心概念

3. 工作原理

4. 配置路由断言工厂和网关过滤器工厂

5. 路由断言工厂

​5.1 断言某个时间之后

5.2 断言某个时间之前

5.3 断言某个时间之间

5.4 根据cookie值断言

5.5 根据请求的标头断言

5.6 根据主机名来断言

5.7 根据请求方法来断言

5.8 根据请求路径来断言

5.9 根据查询参数来断言

5.10 根据远程用户的IP地址来断言

5.11 根据权重来断言

6. 网关过滤器工厂

6.1 AddRequestHeader 网关过滤器

6.2 AddRequestParameter 网关过滤器

 6.3 AddResponseHeader 网关过滤器

 6.4 Spring Cloud Breaker GatewayFilter 过滤器

6.5 删除重复的响应头过滤器

6.6 更新请求标头过滤器

6.7 前缀路径过滤器

 6.8 删除请求头过滤器

6.9 删除请求参数过滤器

6.10 删除响应头过滤器

6.11 重写路径过滤器


官方文档:Spring Cloud Gateway

1. 引入Spring Cloud Gateway

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

 是否开启网关配置:spring.cloud.gateway.enabled=true/false

2. 核心概念

(1)Route 路由

        网关的基本构建基块。 它由 ID、目标 URI、断言集合和筛选器集合定义。如果聚合断言为 true,则匹配路由。

(2)Predicate 断言

        这是一个Java 8函数断言。输入类型是Spring Framework ServerWebExchange。这允许您匹配HTTP请求中的任何内容,例如标头或参数。

(3)Filter 过滤器

        这些是使用特定工厂构建的GatewayFilter实例。在这里,您可以在发送下游请求之前或之后修改请求和响应。

3. 工作原理

Spring Cloud Gateway断言及过滤器使用

        客户端向 Spring Cloud 网关发出请求。如果网关处理程序映射(Gateway Handler Mapping)确定请求与路由匹配,则会将其发送到网关 Web 处理程序(Gateway Web Handler)。 此处理程序通过特定于请求的过滤器链筛选请求。 过滤器用虚线划分的原因是过滤器可以在发送代理请求之前和之后运行逻辑。 执行所有“预”过滤器逻辑。然后发出代理请求。发出代理请求后,将运行“post”过滤器逻辑。

4. 配置路由断言工厂和网关过滤器工厂

        有两种方法可以配置断言和过滤器:快捷方式和完全展开的参数。下面的大多数示例都使用快捷方式。

(1)快捷方式配置

spring:
  cloud:
    gateway:
      routes:
      - id: after_route  #id 自定义
        uri: https://example.org
        predicates:
        - Cookie=mycookie,mycookievalue  #键是mycookie 值是mycookievalue

(2)展开方式配置

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - name: Cookie
          args:
            name: mycookie
            regexp: mycookievalue

5. 路由断言工厂

        Spring Cloud Gateway将路由匹配为Spring WebFlux基础架构的一部分。 Spring Cloud Gateway 包括许多内置的路由断言工厂。 所有这些断言都匹配 HTTP 请求的不同属性。 可以将多个路由断言工厂与逻辑语句组合在一起。

在yml中加入以下日志配置,可以查看重定向到哪里的日志:

logging:
  level:
    org:
      springframework:
        cloud:
          gateway: trace

Spring Cloud Gateway断言及过滤器使用 5.1 断言某个时间之后

        在本地80端口启动若依项目,然后配置断言,在配置时间之后的请求,都转发到 http://localhost 上。

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost  
          predicates:
            - After=2021-01-20T17:42:47.789-07:00[America/Denver]

测试结果:

Spring Cloud Gateway断言及过滤器使用

 5.2 断言某个时间之前

在配置的时间之前,才能访问。

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost
          predicates:
            - Before=2021-01-20T17:42:47.789-07:00[America/Denver]

如果不在这个时间前,访问会出现:

Spring Cloud Gateway断言及过滤器使用

把时间改成在当前时间之后,可以正常访问。

            - Before=2023-03-20T17:42:47.789-07:00[America/Denver]

5.3 断言某个时间之间

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost
          predicates:
          - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2024-01-21T17:42:47.789-07:00[America/Denver]

5.4 根据cookie值断言

根据请求标头中cookie的值进行断言

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost
          predicates:
            - Cookie=Idea-b5baabb9, 16077984-579b-44d2-a1a2-92f5837858ef

Spring Cloud Gateway断言及过滤器使用

 5.5 根据请求的标头断言

请求头中包含了这个请求头,且值符合某个规则

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost
          predicates:
            - Header=studentId, \d+  #\d+表示全是数字

全是数字的情况:

Spring Cloud Gateway断言及过滤器使用 

 如果包含了字母,断言不会通过,访问404:

Spring Cloud Gateway断言及过滤器使用

5.6 根据主机名来断言

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost
          predicates:
            - Host=**.somehost.org,**.anotherhost.org

请求标头中Host值通过断言,即可成功访问

Spring Cloud Gateway断言及过滤器使用

 5.7 根据请求方法来断言

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost
          predicates:
            - Method=GET  #设置只允许GET

GET请求:

Spring Cloud Gateway断言及过滤器使用 POST请求:

Spring Cloud Gateway断言及过滤器使用 

 5.8 根据请求路径来断言

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost
          predicates:
            - Path=/red/{segment},/blue/{segment}

Spring Cloud Gateway断言及过滤器使用

Spring Cloud Gateway断言及过滤器使用 

 5.9 根据查询参数来断言

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost
          predicates:
            - Query=name

Spring Cloud Gateway断言及过滤器使用

 注意

当有两个参数时,必须两个参数都带上,才能请求成功

– Query=name

– Query=id

Spring Cloud Gateway断言及过滤器使用 

5.10 根据远程用户的IP地址来断言

如果IP地址为192.168.1.1 就会转发到 https://example.org

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24  #掩码是24位

5.11 根据权重来断言

此路由会将 ~80% 的流量转发给 weighthigh.org,将 ~20% 的流量转发给 weighlow.org

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

6. 网关过滤器工厂

        路由过滤器允许以某种方式修改传入的 HTTP 请求或传出的 HTTP 响应。 路由过滤器的作用域为特定路由。 Spring Cloud Gateway 包括许多内置的 GatewayFilter 工厂。

对请求头、响应头、请求参数的一些增删改查

6.1 AddRequestHeader 网关过滤器

        增加请求头,可用于请求染色,这个配置会将请求参数 X-Request-zy = blue,加入到请求标头中。

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

6.2 AddRequestParameter 网关过滤器

        增加请求参数

Spring Cloud Gateway断言及过滤器使用

 6.3 AddResponseHeader 网关过滤器

        增加响应头

server:
  port: 8090
spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://localhost
          predicates:
            - Path=/api/{segment}
          filters:
            - AddResponseHeader=X-Response-Red, Blue

Spring Cloud Gateway断言及过滤器使用

 6.4 Spring Cloud Breaker GatewayFilter 过滤器

        Spring Cloud Breaker GatewayFilter 工厂使用 Spring Cloud Breaker API 将网关路由包装在 断路器。Spring Cloud Breaker支持多个可与Spring Cloud Gateway一起使用的库。Spring Cloud 支持开箱即用的 Resilience4J。

在pom.xml中引入依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
        </dependency>

如果出现了异常,就会将请求降级到http://localhost:9994上 

spring:
  cloud:
    gateway:
      routes:
      - id: ingredients
        uri: lb://ingredients
        predicates:
        - Path=//ingredients/**
        filters:
        - name: CircuitBreaker
          args:
            name: fetchIngredients
            fallbackUri: forward:/fallback
      - id: ingredients-fallback
        uri: http://localhost:9994
        predicates:
        - Path=/fallback

6.5 删除重复的响应头过滤器

spring:
  cloud:
    gateway:
      routes:
      - id: dedupe_response_header_route
        uri: https://example.org
        filters:
        - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin

6.6 更新请求标头过滤器

这会使用传入 HTTP 请求标头中的更新值将标头添加到下游请求。

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

6.7 前缀路径过滤器

spring:
  cloud:
    gateway:
      routes:
        - id: prefixpath_route
          uri: http://localhost
          predicates:
            - Path=/api/{segment}
          filters:
            - PrefixPath=/mypath

Spring Cloud Gateway断言及过滤器使用

 6.8 删除请求头过滤器

这会在标头发送到下游之前将其删除

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

6.9 删除请求参数过滤器

这将在将参数发送到下游之前将其删除

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

6.10 删除响应头过滤器

这将在将标头返回到网关客户端之前从响应中删除标头。(用于删除敏感信息

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

6.11 重写路径过滤器

对于请求路径,这会在发出下游请求之前将路径中的api去掉

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/87920.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!