码迷,mamicode.com
首页 > 编程语言 > 详细

003-spring cloud gateway-概述、基本原理、Route Predicate Factory

时间:2018-09-04 21:40:30      阅读:2612      评论:0      收藏:0      [点我收藏+]

标签:远程   ebe   发送请求   red   eric   netty   ipv6   过滤器   enc   

一、概述

  前提条件:Spring 5, Spring Boot 2 and Project Reactor.

  Spring Cloud Gateway旨在提供一种简单而有效的方式来路由到API,并为他们提供横切关注点,例如:安全性,监控/指标和弹性。

  Route:路由网关的基本构建块。它由ID,目标URI,谓词集合和过滤器集合定义。如果聚合谓词为真,则匹配路由。

  Predicate:Java 8 的函数, Spring Framework ServerWebExchange允许开发人员匹配HTTP请求中的任何内容,例如标头或参数。

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

二、使用

2.1、pom引用

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

关闭:spring.cloud.gateway.enabled=false

注意:Spring Cloud Gateway需要Spring Boot和Spring Webflux提供的Netty运行时。它不能在传统的Servlet容器中工作或构建为WAR。

三、工作原理

技术分享图片

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

   在没有端口的路由中定义的URI将分别为HTTP和HTTPS URI获取默认端口设置为80和443。

三、Route predicate Factories【路由谓词工厂】 

  Spring Cloud Gateway将路由作为Spring WebFlux HandlerMapping基础结构的一部分进行匹配。 Spring Cloud Gateway包含许多内置的Route Predicate Factories。所有这些谓词都匹配HTTP请求的不同属性。多路线谓词工厂可以组合,并通过逻辑and。  

3.1、After Route Predicate Factory

  此谓词匹配当前日期时间之后发生的请求。

  application.yml

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

3.2、Before Route Predicate Factory

  此谓词匹配在当前日期时间之前发生的请求。

  application.yml

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

3.3、Between Route Predicate Factory

  此谓词匹配datetime1之后和datetime2之前发生的请求。 datetime2参数必须在datetime1之后。

  application.yml

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: http://example.org
        predicates:
        - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

3.4、Cookie Route Predicate Factory

  Cookie Route Predicate Factory有两个参数,cookie名称和正则表达式。此谓词匹配具有给定名称且值与正则表达式匹配的cookie。

  application.yml

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

  此路由匹配请求有一个名为chocolate的cookie,其值与ch.p正则表达式匹配。

3.5、Header Route Predicate Factory

  Header Route Predicate Factory有两个参数,标题名称和正则表达式。此谓词与具有给定名称且值与正则表达式匹配的标头匹配。

  application.yml

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

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

3.6、Host Route Predicate Factory

  Host Route Predicate Factory采用一个参数:主机名模式。该模式是一种Ant样式模式“.”作为分隔符。此谓词匹配与模式匹配的Host标头。

  application.yml

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

  如果请求的主机头具有值www.somehost.org或beta.somehost.org,则此路由将匹配。

3.7、Method Route Predicate Factory

   Method Route Predicate Factory采用一个参数:要匹配的HTTP方法。

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

3.8、Path Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://example.org
        predicates:
        - Path=/foo/{segment}

/foo/1 or /foo/bar.

3.9、Query Route Predicate Factory

Query Route Predicate Factory有两个参数:一个必需的参数和一个可选的正则表达式。

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

如果请求包含baz查询参数,则此路由将匹配。

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://example.org
        predicates:
        - Query=foo, ba.

如果请求包含其值与ba匹配的foo查询参数,则此路由将匹配。 regexp,所以bar和baz匹配。

3.10、RemoteAddr Route Predicate Factory

RemoteAddr Route Predicate Factory采用CIDR符号(IPv4或IPv6)字符串的列表(最小值为1),例如, 192.168.0.1/16(其中192.168.0.1是IP地址,16是子网掩码)。

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

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

 

003-spring cloud gateway-概述、基本原理、Route Predicate Factory

标签:远程   ebe   发送请求   red   eric   netty   ipv6   过滤器   enc   

原文地址:https://www.cnblogs.com/bjlhx/p/9581655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!