Spring Cloud Gateway网关工作原理、网关谓词和GatewayFilter工厂

Spring Cloud Gateway:

官方文档地址:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/
  Spring Cloud Gateway项目提供了一个在Spring生态系统之上构建的API网关,它指在提供一种简单而高效的方法来将请求路由到API,并未它们提供跨领域的关注,例如:安全性,监视/度量和弹性等。
微服务使用Gateway调用

Spring Cloud Gateway核心概念:

  路由:网关的基本构建组成,它由ID,目标URL,谓词集合和过滤器定义,如果聚合谓词为true,则匹配路由;
  谓词:这是Java 8函数谓词,输入类型是Spring Framework ServerWebExchange,可以匹配HTTP请求中的所有内容,例如请求头或者参数;
  过滤器:这些事使用了特定工厂构造的Spring Framework GatewayFilter实例。在这里,可以在发送给下游请求之前或之后修改请求和响应。

Spring Cloud Gateway特征功能:

1、建立在Spring Framework 5,Project Reactor和Spring Boot 2.0之上;
2、能够匹配任何请求上的路由;
3、谓词和过滤器特定于路由;
4、Hystrix断路器集成;
5、Spring Cloud DiscoveryClient集成;
6、易于编写的谓词和过滤器(可扩展性很高);
7、路径改写;

Spring Cloud Gateway工作原理:

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

入门使用Spring Cloud Gateway:

1、添加gateway网关依赖:

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

        <!-- sentinel-spring-cloud-gateway-adapter -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
            <version>1.7.2</version>
        </dependency>

2、配置application.yaml文件(配置路由谓词工厂和网关过滤工厂)

#启动端口
server:
  port: 80

#spring boot actuator 监控端点
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    jmx:
      exposure:
        include: '*'
    web:
      exposure:
        include: '*'
  health:
    sentinel:
      enabled: false


spring:
  application:
    #服务名字
    name: nacos-discovery-spring-cloud-gateway

  cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现

      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index

          filters:
            - AddRequestHeader=X-Request-red, blue
            - AddRequestHeader=X-Request-Id, 12345
            - AddRequestParameter=color, black
            - RequestLog=prefix, gatewaytest #自定义的filter

Netty started
  由于Spring Cloud Gateway启动的时候是Netty而不是tomcat,所有此时项目里面就不需要spring-boot-starter-web依赖

路由谓词工厂:

  Spring Cloud Gateway将路由匹配作为 WebFlux HandlerMapping基础架构的一部分,Spring Cloud Gateway包括许多内置的路由谓词工厂,所有这些谓词都与HTTP请求的不同属性匹配,可以将多个路由谓词工厂结合使用;有以下11个路由谓词工厂:

After路由谓词工厂:

  After route谓词工厂采用一个参数,即datetime(这是一个Java ZonedDateTime),该谓词匹配在指定日期时间之后发生的请求,以下示例配置了路由后谓词:
时间获取方式:时间通过获取:System.out.println(ZonedDateTime.now());

cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现
      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index, /echo
            - After=2021-02-12T00:27:04.095+08:00[Asia/Shanghai]
#符合2021年02月12日00:27:04之后的请求

Before路由谓词工厂:

  Before路由谓词工厂采用一个参数,即datetime(这是一个Java ZonedDateTime),该谓词匹配在指定日期时间之前发生的请求,下面的示例配置路由之前谓词:

cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现
      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index, /echo
            - Before=2021-01-12T00:27:04.095+08:00[Asia/Shanghai]
#符合2021年02月12日00:27:04之前的请求

Between路由谓词工厂:

  路由谓词之间的工厂使用两个参数datetime1和datetime2,它们是java ZonedDateTime对象,该谓词匹配在datetime1之后和datetime2之前发生的请求,datetime2参数必须在datetime1之后,以下示例配置了路由之间的谓词:

 cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现

      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index, /echo
            - Between=2021-02-10T00:27:04.095+08:00[Asia/Shanghai], 2021-02-11T00:37:04.095+08:00[Asia/Shanghai]
#符合2021年02月12日00::27:04到2021年02月12日00:37:04之间的请求

Cookie 路由谓词工厂:

  Cookie路由谓词工厂采用两个参数,即cookie名称和一个regexp(这是Java正则表达式),该谓词匹配具有给定名称且其值与正则表达式匹配的cookie,以下示例配置Cookie路由谓词工厂:

  cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现

      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index, /echo
            - Cookie=token, ch.p
#此路由匹配具有名称为token与ch.p正则表达式匹配的cookie的请求。

Header 路由谓词工厂:

  header 路由谓词工厂使用两个参数,header 名称和一个regexp(这是Java正则表达式),该谓词与具有给定名称的header 匹配,该header 的值与正则表达式匹配,以下示例配置标头路由谓词:

  cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现

      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index, /echo
            - Header=X-Request-Id, \d+
#如果请求具有名为X-Request-Id的标头,且其值与\ d +正则表达式匹配(值为一个或多个数字),则此路由匹配;

Host 路由谓词工厂:

  Host路由谓词工厂使用一个参数:主机名模式列表,以下示例配置主机路由谓词:

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org
#如果请求的Host标头值为www.somehost.org或beta.somehost.org,则此路由匹配www.anotherhost.org。

Method 路由谓词工厂:

  方法路由谓词工厂使用方法参数,该参数是一个或多个参数,要匹配的HTTP方法,以下示例配置方法route谓词:

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST
#如果请求方法是GET或POST,则此路由匹配;

Path路由谓词工厂:

  路径路由谓词工厂使用两个参数:Spring PathMatcher模式列表和一个称为matchOptionalTrailingSeparator的可选标志,以下示例配置路径路由谓词:

  cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现

      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index
#如果请求路径为例如/test2或/notFound-feign或/index,则此路由匹配;

Query路由谓词工厂:

  查询路由谓词工厂采用两个参数:必需的参数和可选的regexp(这是Java正则表达式),以下示例配置查询路由谓词:

  cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现

      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index, /echo
            - Query=token
#如果请求包含token查询参数,则前面的路由匹配;
  cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现

      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index, /echo
            - Query=color,red
#如果请求包含值与red匹配的color查询参数,则上述路由匹配;

RemoteAddr 路由谓词工厂:

  RemoteAddr路由谓词工厂使用源列表(最小大小为1),这些源是标记(IPv4或IPv6)字符串,例如192.168.0.1/16(其中192.168.0.1是IP地址,而16是子网掩码)),下面的示例配置RemoteAddr路由谓词:

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24
#如果请求的远程地址为,则此路由匹配192.168.1.10;

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。

GatewayFilter 工厂:

  路由过滤器允许以某种方式修改传入的HTTP请求或传出的HTTP响应,Spring Cloud Gateway包括许多内置的GatewayFilter工厂(总共有31个 GatewayFilter 工厂);一下就举一个例子想了解更多去熟系下官方文档

AddRequestHeader GatewayFilter工厂:

 cloud:
    #配置网关
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成,可以实现服务的发现

      #配置网关路由转发规则
      routes:
        - id: route1
          uri: lb://29-nacos-discovery-customer
          predicates: #谓词:判断,是不是?对不对?是否匹配?
            - Path=/test2, /notFound-feign, /index, /echo
            - Token=123456

          filters:
            - AddRequestHeader=X-Request-red, blue
#此清单将X-Request-red:blue标头添加到所有匹配请求的下游请求头中。
#AddRequestHeader了解用于匹配路径或主机的URI变量。URI变量可以在值中使用,并在运行时扩展。
#以下示例配置了AddRequestHeader GatewayFilter使用变量的:
#System.out.println("X-Request-red = " + request.getHeader("X-Request-red"));
#X-Request-red = blue

Spring Cloud Gateway入门了解工作原理、11种网关谓词和GatewayFilter工厂

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值