Spring Cloud Gateway

Spring Cloud Gateway

版本 : 2.2.2 简介

Spring Cloud Gateway 是在Spring生态系统之上构建的API网关,包括:Spring 5,Spring Boot 2和Project Reactor。Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到API,并为它们提供跨领域的关注点,例如:安全性,监视/指标和弹性。

1.如何引入Spring Cloud Gateway


在maven项目中引入 spring-cloud-starter-gateway

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

其包的路径为org.springframework.cloud.gateway。
在这里插入图片描述

在引入gateway的情况下,可以通过spring.cloud.gateway.enabled=false控制是否启用。

说明:
Spring Cloud Gateway是基于Spring Boot 2.x,Spring WebFlux和Project Reactor 构建的。所以,它与Spring Data和Spring Security等模式不同。
Spring Cloud Gateway基于Spring Boot和Spring Webflux运行。它不能在传统的Servlet容器中或作为WAR构建时使用,也就是说引入了spring-cloud-starter-gateway的maven项目就不能引入spring-boot-starter-web,一旦引入在启动或者运行时就会报一下错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
 No qualifying bean of type 'org.springframework.core.convert.ConversionService' 
 available:  expected at least 1 bean which qualifies as autowire candidate. 
 Dependency annotations: {@org.springframework.beans.factory.annotation.
 Qualifier(value=webFluxConversionService)

2.涉及到的术语


Route:网关的基本构建块。它由ID,destination URI,a collection of predicates和collection of filters定义。如果 predicate 返回值是 true,则匹配路由。

Predicate:这是Java 8 Function Predicate.。输入类型是Spring Framework ServerWebExchange。这使您可以匹配HTTP请求中( HTTP request)的所有内容,例如标头(headers)或参数(parameters)。

Filter:这些是使用特定工厂构造的Spring FrameworkGatewayFilter实例。在这里,您可以在发送请求之前或之后修改请求和响应。

3.工作原理


下图从总体上概述了Spring Cloud Gateway的工作方式:

Spring Cloud网关图:
客户端向Spring Cloud Gateway发出请求。如果网关处理程序映射确定请求与路由匹配(Gateway Handler Mapping),则将其发送到网关Web处理程序(Gateway Web Handler)。该处理程序通过特定于请求的过滤器链(filter chain)来运行请求。筛选器由虚线分隔表明筛选器可以在发送代理请求之前和之后运行逻辑处理。所有“前置”过滤器逻辑均被执行。然后发出代理请求。发出代理请求后,将运行“后置”过滤器逻辑。

在没有端口的路由中定义的URI,HTTP和HTTPS URI的默认端口值分别为80和443。

4.配置Route Predicate工厂和Gateway Filter工厂


有两种配置 Predicate和 Filte的方法:快捷方式和完全展开(shortcuts and fully expanded arguments.)。下面的大多数示例都使用快捷方式。

名称和自变量名称将code在每个部分的第一个或两个中列出。参数通常按快捷方式配置所需的顺序列出。

4.1。Shortcut Configuration
快捷方式配置由过滤器名称识别,后跟等号(=),后跟以逗号(,)分隔的参数值。

application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - Cookie=mycookie,mycookievalue

先前的示例中,Cookie代表 Cookie Route Predicate Factory,有两个参数(cookie名称mycookie和需要匹配的值 mycookievalue)。

4.2。Fully Expanded Arguments
完全展开看起来更像带有名称/值对的标准Yaml配置。通常,将有一个键 name和一个 键 args。args的值是 predicate 或者 filter 键值对 map。

application.yml

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

这是Cookie上面显示的predicate 的快捷方式配置的完整配置。

5.Route Predicate Factories


Spring Cloud Gateway将使用Spring WebFlux HandlerMapping进行匹配路由。Spring Cloud Gateway包括许多内置的route predicate factories。所有这些predicate 都与HTTP请求的不同属性匹配。您可以将多个route predicate factories与 “and”语句结合使用。

5.1。The After Route Predicate Factory
所述工厂有一个参数,datetime(其是Java ZonedDateTime)。该谓词匹配在指定日期时间之后发生的请求。下面的示例配置路由后谓词:

例子1. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2020-03-21T19:46:00.975+08:00[Asia/Shanghai]

这路由在2020年03月21日19:46(上海时间)之后生效。

日期生成示例:

        ZonedDateTime zonedDateTime = ZonedDateTime.now(); // 默认时区
        System.out.println(zonedDateTime);

控制台:

       2020-03-21T19:46:00.975+08:00[Asia/Shanghai]

5.2。The Before Route Predicate Factory
所述Before路线谓词工厂有一个参数,一个datetime(其是Java ZonedDateTime)。该谓词匹配在指定之前发生的请求datetime。以下示例配置了路由前谓词:

例子2. application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - After=2020-03-21T19:46:00.975+08:00[Asia/Shanghai]

这路由在2020年03月21日19:46(上海时间)之前有效。

5.3。The Between Route Predicate Factory
该工厂有两个参数,datetime1和datetime2 ,这是Java ZonedDateTime对象。该匹配在datetime1之后和datetime2之前生效。datetime1必须是datetime2之前的时间。以下示例配置了路由之间的谓词:

例子3. application.yml

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]

该路由在2017年1月20日(丹佛)之后和2017年1月21日17:42(丹佛)之前生效。这对于维护时段可能很有用。

5.4。The Cookie Route Predicate Factory
该工厂采用两个参数,该cookie name和regexp(Java正则表达式)。给定name(chocolate)、regexp ( ch.p)。示例:

例子4. application.yml

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

当含有cookie 且名字为chocolate、符合ch.p正则的请求转发到 https://example.org。

5.5。The Header Route Predicate Factory

例子5. application.yml

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

5.6。The Host Route Predicate Factory
该Host路线谓词工厂需要一个参数:主机名的列表patterns。该模式是带有.分隔符的Ant样式的模式。谓词与Host匹配模式的标头匹配。以下示例配置主机路由谓词:

例子6. application.yml

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

{sub}.myhost.org还支持URI模板变量(例如)。

如果请求具有这种路由匹配Host用的头值www.somehost.org或beta.somehost.org或www.anotherhost.org。

在GatewayFilter中可以使用ServerWebExchange.getAttributes() 获取map,通过key ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE 即可取出value;

5.7。The Method Route Predicate Factory

例子7. application.yml

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

5.8。The Path Route Predicate Factory

例子8. application.yml

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

5.9。 The Query Route Predicate Factory

例子9. application.yml

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

如果请求包含green查询参数,则前面的路由匹配。

application.yml

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

如果请求包含一个前述路线匹配red,其值相匹配的查询参数gree.的regexp,所以green和greet将匹配。

5.10。The RemoteAddr Route Predicate Factory
192.168.0.1/16(其中192.168.0.1是一个IP地址和16一个子网掩码)。以下示例配置一个RemoteAddr路由谓词:

例子10. application.yml

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

如果请求的远程地址为,则此路由匹配192.168.1.10。

5.11。The Weight Route Predicate Factory
group和weight(int)。权重是按组计算的。

例子11. application.yml

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。

6. GatewayFilter Factories

  1. Global Filters
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值