• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SpringCloudAlibaba SpringCloudGateway

武飞扬头像
SCBAiotAigc
帮助1

1.1 SpringCloud Gateway 简介

SpringCloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。

SpringCloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,仍然还是使用的Zuul 2.0之前的非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。

Spring Cloud Gateway 的目标,不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。

提前声明:Spring Cloud Gateway 底层使用了高性能的通信框架Netty

1.2 SpringCloud Gateway 特征

SpringCloud官方,对SpringCloud Gateway 特征介绍如下:

(1)基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0

(2)集成 Hystrix 断路器

(3)集成 Spring Cloud DiscoveryClient

(4)Predicates 和 Filters 作用于特定路由,易于编写的 Predicates 和 Filters

(5)具备一些网关的高级功能:动态路由、限流、路径重写

从以上的特征来说,和Zuul的特征差别不大。SpringCloud Gateway和Zuul主要的区别,还是在底层的通信框架上。

简单说明一下上文中的三个术语:

1Filter(过滤器)

和Zuul的过滤器在概念上类似,可以使用它拦截和修改请求,并且对上游的响应,进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的实例。

(2)Route(路由):

网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。

3Predicate(断言)

这是一个 Java 8 的 Predicate,可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。断言的输入类型是一个 ServerWebExchange。

1.3 SpringCloud Gateway和架构

Spring在2017年下半年迎来了Webflux,Webflux的出现填补了Spring在响应式编程上的空白,Webflux的响应式编程不仅仅是编程风格的改变,而且对于一系列的著名框架,都提供了响应式访问的开发包,比如Netty、Redis等等。

SpringCloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架。

maven依赖:

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.cloud</groupId>
  3.  
    <artifactId>spring-cloud-starter-gateway</artifactId>
  4.  
    </dependency>

学新通

1.3.1 SpringCloud Zuul的IO模型

Springcloud中所集成的Zuul版本,采用的是Tomcat容器,使用的是传统的Servlet IO处理模型。

大家知道,servlet由servlet container进行生命周期管理。container启动时构造servlet对象并调用servlet init()进行初始化;container关闭时调用servlet destory()销毁servlet;container运行时接受请求,并为每个请求分配一个线程(一般从线程池中获取空闲线程)然后调用service()。

弊端:servlet是一个简单的网络IO模型,当请求进入servlet container时,servlet container就会为其绑定一个线程,在并发不高的场景下这种模型是适用的,但是一旦并发上升,线程数量就会上涨,而线程资源代价是昂贵的(上线文切换,内存消耗大)严重影响请求的处理时间。在一些简单的业务场景下,不希望为每个request分配一个线程,只需要1个或几个线程就能应对极大并发的请求,这种业务场景下servlet模型没有优势。

学新通

 所以Springcloud Zuul 是基于servlet之上的一个阻塞式处理模型,即spring实现了处理所有request请求的一个servlet(DispatcherServlet),并由该servlet阻塞式处理处理。所以Springcloud Zuul无法摆脱servlet模型的弊端。虽然Zuul 2.0开始,使用了Netty,并且已经有了大规模Zuul 2.0集群部署的成熟案例,但是,Springcloud官方已经没有集成改版本的计划了。

1.3.2 Webflux 服务器

Webflux模式替换了旧的Servlet线程模型。用少量的线程处理request和response io操作,这些线程称为Loop线程,而业务交给响应式编程框架处理,响应式编程是非常灵活的,用户可以将业务中阻塞的操作提交到响应式框架的work线程中执行,而不阻塞的操作依然可以在Loop线程中进行处理,大大提高了Loop线程的利用率。官方结构图:

学新通

Webflux虽然可以兼容多个底层的通信框架,但是一般情况下,底层使用的还是Netty,毕竟,Netty是目前业界认可的最高性能的通信框架。而Webflux的Loop线程,正好就是著名的Reactor 模式IO处理模型的Reactor线程,如果使用的是高性能的通信框架Netty,这就是Netty的EventLoop线程。

关于Reactor线程模型,和Netty通信框架的知识,是Java程序员的重要、必备的内功,个中的原理,具体请参见尼恩编著的《Netty、Zookeeper、Redis高并发实战》一书,这里不做过多的赘述。

1.3.3 Spring Cloud Gateway的处理流程

客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。

学新通

2 路由配置方式

2.1 基础URI路由配置方式

如果请求的目标地址,是单个的URI资源路径,配置文件示例如下:

  1.  
    server:
  2.  
    port: 8010
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: @artifactId@
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
  11.  
    config:
  12.  
    server-addr: ${spring.cloud.nacos.discovery.server-addr}
  13.  
    file-extension: yml
  14.  
    namespace: public
  15.  
    group: DEFAULT_GROUP
  16.  
    gateway:
  17.  
    routes:
  18.  
    ###路由id
  19.  
    - id: ljxwtl.cn
  20.  
    ####转发https://ljxwtl.cn/
  21.  
    uri: https://ljxwtl.cn
  22.  
    filters:
  23.  
    ### Path的深度
  24.  
    - StripPrefix=2
  25.  
    ###匹配规则
  26.  
    predicates:
  27.  
    - Path=/albaba/ljxwtl/**
  28.  
     
学新通

各字段含义如下:

  • id:我们自定义的路由 ID,保持唯一
  • uri:目标服务地址
  • predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
  • filters:过滤规则。

2.2 基于代码的路由配置方式

转发功能同样可以通过代码来实现,我们可以在启动类 GateWayApplication 中添加方法 customRouteLocator() 来定制转发规则。

  1.  
    package ljxwtl.gateway.config;
  2.  
     
  3.  
    import org.springframework.cloud.gateway.route.RouteLocator;
  4.  
    import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
  5.  
    import org.springframework.context.annotation.Bean;
  6.  
    import org.springframework.context.annotation.Configuration;
  7.  
     
  8.  
    /**
  9.  
    * @author: wtl
  10.  
    * @License: (C) Copyright 2021, wtl Corporation Limited.
  11.  
    * @Contact: 1050100468@qq.com
  12.  
    * @Date: 2021/11/6 7:51 上午
  13.  
    * @Version: 1.0
  14.  
    * @Description:
  15.  
    */
  16.  
    @Configuration
  17.  
    public class GatewayStaticConfig {
  18.  
     
  19.  
    /**
  20.  
    * 自定义路由规则
  21.  
    * @param routeLocatorBuilder 路由定位器
  22.  
    * @return RouteLocator
  23.  
    */
  24.  
    @Bean
  25.  
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
  26.  
    return routeLocatorBuilder.routes()
  27.  
    .route("path_route", r -> r.path("/csdn")
  28.  
    .filters(gatewayFilterSpec -> {
  29.  
    return gatewayFilterSpec.stripPrefix(1);
  30.  
    })
  31.  
    .uri("https://blog.csdn.net"))
  32.  
    .build();
  33.  
    }
  34.  
    }
学新通

 我们在yml配置文件中注销掉相关路由的配置,重启服务,访问链接:http://localhost:8080/ csdn, 可以看到和上面一样的页面,证明我们测试成功。

2.3 和注册中心相结合的路由配置方式

在uri的schema协议部分为自定义的lb:类型,表示从微服务注册中心(nacos)订阅服务,并且进行服务的路由。

一个典型的示例如下:

application.yml:

  1.  
    server:
  2.  
    port: 8010
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: @artifactId@
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
  11.  
    config:
  12.  
    server-addr: ${spring.cloud.nacos.discovery.server-addr}
  13.  
    file-extension: yml
  14.  
    namespace: public
  15.  
    group: DEFAULT_GROUP
  16.  
    gateway:
  17.  
    routes:
  18.  
    ###路由id
  19.  
    - id: ljxwtl.cn
  20.  
    ####转发https://ljxwtl.cn/
  21.  
    uri: https://ljxwtl.cn
  22.  
    filters:
  23.  
    ### Path的深度
  24.  
    - StripPrefix=2
  25.  
    ###匹配规则
  26.  
    predicates:
  27.  
    - Path=/albaba/ljxwtl/**
  28.  
    ###路由id
  29.  
    - id: order
  30.  
    ####转发https://ljxwtl.cn/
  31.  
    uri: lb://ljxwtl-order-biz/
  32.  
    filters:
  33.  
    ### Path的深度
  34.  
    - StripPrefix=2
  35.  
    ###匹配规则
  36.  
    predicates:
  37.  
    - Path=/gateway/order/**
  38.  
    ###路由id
  39.  
    - id: member
  40.  
    ####转发https://ljxwtl.cn/
  41.  
    uri: lb://ljxwtl-member-biz/
  42.  
    filters:
  43.  
    ### Path的深度
  44.  
    - StripPrefix=1
  45.  
    ###匹配规则
  46.  
    predicates:
  47.  
    - Path=/member/**
学新通

学新通

3 路由 匹配规则

Spring Cloud Gateway 的功能很强大,我们仅仅通过 Predicates 的设计就可以看出来,前面我们只是使用了 predicates 进行了简单的条件匹配,其实 Spring Cloud Gataway 帮我们内置了很多 Predicates 功能。

Spring Cloud Gateway 是通过 Spring WebFlux 的 HandlerMapping 做为底层支持来匹配到转发路由,Spring Cloud Gateway 内置了很多 Predicates 工厂,这些 Predicates 工厂通过不同的 HTTP 请求参数来匹配,多个 Predicates 工厂可以组合使用。

学新通

gateWay的主要功能之一是转发请求,转发规则的定义主要包含三个部分

     
Route(路由) 路由是网关的基本单元,由ID、URI、一组Predicate、一组Filter组成,根据Predicate进行匹配转发。  
Predicate(谓语、断言) 路由转发的判断条件,目前SpringCloud Gateway支持多种方式,常见如:Path、Query、Method、Header等,写法必须遵循 key=vlue的形式  
Filter(过滤器) 过滤器是路由转发请求时所经过的过滤逻辑,可用于修改请求、响应内容  

其中Route和Predicate必须同时申明

3.1 Predicate 断言条件(转发规则)介绍

Predicate 来源于 Java 8,是 Java 8 中引入的一个函数,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。可以用于接口请求参数校验、判断新老数据是否有变化需要进行更新操作。

在 Spring Cloud Gateway 中 Spring 利用 Predicate 的特性实现了各种路由匹配规则,有通过 Header、请求参数等不同的条件来进行作为条件匹配到对应的路由。网上有一张图总结了 Spring Cloud 内置的几种 Predicate 的实现。

学新通

说白了 Predicate 就是为了实现一组匹配规则,方便让请求过来找到对应的 Route 进行处理,接下来我们接下 Spring Cloud GateWay 内置几种 Predicate 的使用。

规则 实例 说明
Path - Path=/gate/,/rule/ ## 当请求的路径为gate、rule开头的时,转发到http://localhost:9023服务器上
Before - Before=2022-01-20T17:42:47.789-07:00[America/Denver] 在某个时间之前的请求才会被转发到 http://localhost:9023服务器上
After - After=2022-01-20T17:42:47.789-07:00[America/Denver] 在某个时间之后的请求才会被转发
Between - Between=2022-01-20T17:42:47.789-07:00[America/Denver],2022-01-21T17:42:47.789-07:00[America/Denver] 在某个时间段之间的才会被转发
Cookie - Cookie=chocolate, ch.p 名为chocolate的表单或者满足正则ch.p的表单才会被匹配到进行请求转发
Header - Header=X-Request-Id, \d 携带参数X-Request-Id或者满足\d 的请求头才会匹配
Host - Host=www.hd123.com 当主机名为www.hd123.com的时候直接转发到http://localhost:9023服务器上
Method - Method=GET 只有GET方法才会匹配转发请求,还可以限定POST、PUT等请求方式

3.1.1 通过请求参数匹配

Query Route Predicate 支持传入两个参数,一个是属性名一个为属性值,属性值可以是正则表达式。

  1.  
    server:
  2.  
    port: 8010
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: @artifactId@
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
  11.  
    config:
  12.  
    server-addr: ${spring.cloud.nacos.discovery.server-addr}
  13.  
    file-extension: yml
  14.  
    namespace: public
  15.  
    group: DEFAULT_GROUP
  16.  
    gateway:
  17.  
    routes:
  18.  
    ###路由id
  19.  
    - id: ljxwtl.cn
  20.  
    ####转发https://ljxwtl.cn/
  21.  
    uri: https://ljxwtl.cn
  22.  
    filters:
  23.  
    ### Path的深度
  24.  
    - StripPrefix=2
  25.  
    ###匹配规则
  26.  
    predicates:
  27.  
    - Path=/albaba/ljxwtl/**
  28.  
    ###路由id
  29.  
    - id: order
  30.  
    ####转发https://ljxwtl.cn/
  31.  
    uri: lb://ljxwtl-order-biz/
  32.  
    filters:
  33.  
    ### Path的深度
  34.  
    - StripPrefix=2
  35.  
    ###匹配规则
  36.  
    predicates:
  37.  
    - Path=/gateway/order/**
  38.  
    ###路由id
  39.  
    - id: member
  40.  
    ####转发https://ljxwtl.cn/
  41.  
    uri: lb://ljxwtl-member-biz/
  42.  
    filters:
  43.  
    ### Path的深度
  44.  
    - StripPrefix=1
  45.  
    ###匹配规则
  46.  
    predicates:
  47.  
    - Path=/member/**
  48.  
    ###路由id
  49.  
    - id: ljxwtl.cn
  50.  
    ####转发https://ljxwtl.cn/
  51.  
    uri: https://ljxwtl.cn
  52.  
    filters:
  53.  
    ### Path的深度
  54.  
    - StripPrefix=1
  55.  
    ###匹配规则
  56.  
    predicates:
  57.  
    - Path=/ljxwtl/**
  58.  
    - Query=keyword
学新通

http://localhost:8010/ljxwtl/selectByMovieLikeTitle?keyword=周星驰

学新通 

3.1.2 通过 Header 属性匹配

Header Route Predicate 和 Cookie Route Predicate 一样,也是接收 2 个参数,一个 header 中属性名称和一个正则表达式,这个属性值和正则表达式匹配则执行。

  1.  
    server:
  2.  
    port: 8010
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: @artifactId@
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
  11.  
    config:
  12.  
    server-addr: ${spring.cloud.nacos.discovery.server-addr}
  13.  
    file-extension: yml
  14.  
    namespace: public
  15.  
    group: DEFAULT_GROUP
  16.  
    gateway:
  17.  
    routes:
  18.  
    ###路由id
  19.  
    - id: ljxwtl.cn
  20.  
    ####转发https://ljxwtl.cn/
  21.  
    uri: https://ljxwtl.cn
  22.  
    filters:
  23.  
    ### Path的深度
  24.  
    - StripPrefix=2
  25.  
    ###匹配规则
  26.  
    predicates:
  27.  
    - Path=/alibaba/ljxwtl/**
  28.  
    ###路由id
  29.  
    - id: order
  30.  
    ####转发https://ljxwtl.cn/
  31.  
    uri: lb://ljxwtl-order-biz/
  32.  
    filters:
  33.  
    ### Path的深度
  34.  
    - StripPrefix=2
  35.  
    ###匹配规则
  36.  
    predicates:
  37.  
    - Path=/gateway/order/**
  38.  
    ###路由id
  39.  
    - id: member
  40.  
    ####转发https://ljxwtl.cn/
  41.  
    uri: lb://ljxwtl-member-biz/
  42.  
    filters:
  43.  
    ### Path的深度
  44.  
    - StripPrefix=1
  45.  
    ###匹配规则
  46.  
    predicates:
  47.  
    - Path=/member/**
  48.  
    ###路由id
  49.  
    - id: header
  50.  
    ####转发https://ljxwtl.cn/
  51.  
    uri: https://ljxwtl.cn
  52.  
    filters:
  53.  
    ### Path的深度
  54.  
    - StripPrefix=1
  55.  
    ###匹配规则
  56.  
    predicates:
  57.  
    - Path=/header/**
  58.  
    - Header=auth,\d
学新通

使用 curl 测试,命令行输入:

curl http://localhost:8010/header/selectByMovieLikeTitle?keyword=周星驰 -H"auth:123"

则返回页面代码证明匹配成功。将参数-H"auth:123"改为-H "auth:spring"再次执行时返回404证明没有匹配。

学新通

3.1.3 通过 Cookie 匹配

Cookie Route Predicate 可以接收两个参数,一个是 Cookie name ,一个是正则表达式,路由规则会通过获取对应的 Cookie name 值和正则表达式去匹配,如果匹配上就会执行路由,如果没有匹配上则不执行。

  1.  
    server:
  2.  
    port: 8010
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: @artifactId@
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
  11.  
    config:
  12.  
    server-addr: ${spring.cloud.nacos.discovery.server-addr}
  13.  
    file-extension: yml
  14.  
    namespace: public
  15.  
    group: DEFAULT_GROUP
  16.  
    gateway:
  17.  
    routes:
  18.  
    ###路由id
  19.  
    - id: ljxwtl.cn
  20.  
    ####转发https://ljxwtl.cn/
  21.  
    uri: https://ljxwtl.cn
  22.  
    filters:
  23.  
    ### Path的深度
  24.  
    - StripPrefix=2
  25.  
    ###匹配规则
  26.  
    predicates:
  27.  
    - Path=/alibaba/ljxwtl/**
  28.  
    ###路由id
  29.  
    - id: order
  30.  
    ####转发https://ljxwtl.cn/
  31.  
    uri: lb://ljxwtl-order-biz/
  32.  
    filters:
  33.  
    ### Path的深度
  34.  
    - StripPrefix=2
  35.  
    ###匹配规则
  36.  
    predicates:
  37.  
    - Path=/gateway/order/**
  38.  
    ###路由id
  39.  
    - id: member
  40.  
    ####转发https://ljxwtl.cn/
  41.  
    uri: lb://ljxwtl-member-biz/
  42.  
    filters:
  43.  
    ### Path的深度
  44.  
    - StripPrefix=1
  45.  
    ###匹配规则
  46.  
    predicates:
  47.  
    - Path=/member/**
  48.  
    ###路由id
  49.  
    - id: header
  50.  
    ####转发https://ljxwtl.cn/
  51.  
    uri: https://ljxwtl.cn
  52.  
    filters:
  53.  
    ### Path的深度
  54.  
    - StripPrefix=1
  55.  
    ###匹配规则
  56.  
    predicates:
  57.  
    - Path=/header/**
  58.  
    - Header=auth,\d
  59.  
    ###路由id
  60.  
    - id: cookie
  61.  
    ####转发https://ljxwtl.cn/
  62.  
    uri: https://ljxwtl.cn
  63.  
    filters:
  64.  
    ### Path的深度
  65.  
    - StripPrefix=1
  66.  
    ###匹配规则
  67.  
    predicates:
  68.  
    - Path=/cookie/**
  69.  
    - Header=auth,\d
  70.  
    - Cookie=sessionId,test
学新通

学新通

 则会返回页面代码,如果去掉--cookie "sessionId=test",后台汇报 404 错误。

3.1.4 通过 Host 匹配

Host Route Predicate 接收一组参数,一组匹配的域名列表,这个模板是一个 ant 分隔的模板,用.号作为分隔符。它通过参数中的主机地址作为匹配规则。

  1.  
    server:
  2.  
    port: 8010
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: @artifactId@
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
  11.  
    config:
  12.  
    server-addr: ${spring.cloud.nacos.discovery.server-addr}
  13.  
    file-extension: yml
  14.  
    namespace: public
  15.  
    group: DEFAULT_GROUP
  16.  
    gateway:
  17.  
    routes:
  18.  
    ###路由id
  19.  
    - id: ljxwtl.cn
  20.  
    ####转发https://ljxwtl.cn/
  21.  
    uri: https://ljxwtl.cn
  22.  
    filters:
  23.  
    ### Path的深度
  24.  
    - StripPrefix=2
  25.  
    ###匹配规则
  26.  
    predicates:
  27.  
    - Path=/alibaba/ljxwtl/**
  28.  
    ###路由id
  29.  
    - id: order
  30.  
    ####转发https://ljxwtl.cn/
  31.  
    uri: lb://ljxwtl-order-biz/
  32.  
    filters:
  33.  
    ### Path的深度
  34.  
    - StripPrefix=2
  35.  
    ###匹配规则
  36.  
    predicates:
  37.  
    - Path=/gateway/order/**
  38.  
    ###路由id
  39.  
    - id: member
  40.  
    ####转发https://ljxwtl.cn/
  41.  
    uri: lb://ljxwtl-member-biz/
  42.  
    filters:
  43.  
    ### Path的深度
  44.  
    - StripPrefix=1
  45.  
    ###匹配规则
  46.  
    predicates:
  47.  
    - Path=/member/**
  48.  
    ###路由id
  49.  
    - id: header
  50.  
    ####转发https://ljxwtl.cn/
  51.  
    uri: https://ljxwtl.cn
  52.  
    filters:
  53.  
    ### Path的深度
  54.  
    - StripPrefix=1
  55.  
    ###匹配规则
  56.  
    predicates:
  57.  
    - Path=/header/**
  58.  
    - Header=auth,\d
  59.  
    ###路由id
  60.  
    - id: cookie
  61.  
    ####转发https://ljxwtl.cn/
  62.  
    uri: https://ljxwtl.cn
  63.  
    filters:
  64.  
    ### Path的深度
  65.  
    - StripPrefix=1
  66.  
    ###匹配规则
  67.  
    predicates:
  68.  
    - Path=/cookie/**
  69.  
    - Header=auth,\d
  70.  
    - Cookie=sessionId,test
  71.  
    ###路由id
  72.  
    - id: host
  73.  
    ####转发https://ljxwtl.cn/
  74.  
    uri: https://ljxwtl.cn
  75.  
    filters:
  76.  
    ### Path的深度
  77.  
    - StripPrefix=1
  78.  
    ###匹配规则
  79.  
    predicates:
  80.  
    - Path=/host/**
  81.  
    - Header=auth,\d
  82.  
    - Cookie=sessionId,test
  83.  
    - Host=**.百度.com
学新通

学新通

 经测试包含host头信息可匹配到 host_route 路由,去掉 host 参数则会报 404 错误。

3.1.5 通过请求方式匹配

可以通过是 POST、GET、PUT、DELETE 等不同的请求方式来进行路由。

  1.  
    server:
  2.  
    port: 8010
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: @artifactId@
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
  11.  
    config:
  12.  
    server-addr: ${spring.cloud.nacos.discovery.server-addr}
  13.  
    file-extension: yml
  14.  
    namespace: public
  15.  
    group: DEFAULT_GROUP
  16.  
    gateway:
  17.  
    routes:
  18.  
    ###路由id
  19.  
    - id: ljxwtl.cn
  20.  
    ####转发https://ljxwtl.cn/
  21.  
    uri: https://ljxwtl.cn
  22.  
    filters:
  23.  
    ### Path的深度
  24.  
    - StripPrefix=2
  25.  
    ###匹配规则
  26.  
    predicates:
  27.  
    - Path=/alibaba/ljxwtl/**
  28.  
    ###路由id
  29.  
    - id: order
  30.  
    ####转发https://ljxwtl.cn/
  31.  
    uri: lb://ljxwtl-order-biz/
  32.  
    filters:
  33.  
    ### Path的深度
  34.  
    - StripPrefix=2
  35.  
    ###匹配规则
  36.  
    predicates:
  37.  
    - Path=/gateway/order/**
  38.  
    ###路由id
  39.  
    - id: member
  40.  
    ####转发https://ljxwtl.cn/
  41.  
    uri: lb://ljxwtl-member-biz/
  42.  
    filters:
  43.  
    ### Path的深度
  44.  
    - StripPrefix=1
  45.  
    ###匹配规则
  46.  
    predicates:
  47.  
    - Path=/member/**
  48.  
    ###路由id
  49.  
    - id: header
  50.  
    ####转发https://ljxwtl.cn/
  51.  
    uri: https://ljxwtl.cn
  52.  
    filters:
  53.  
    ### Path的深度
  54.  
    - StripPrefix=1
  55.  
    ###匹配规则
  56.  
    predicates:
  57.  
    - Path=/header/**
  58.  
    - Header=auth,\d
  59.  
    ###路由id
  60.  
    - id: cookie
  61.  
    ####转发https://ljxwtl.cn/
  62.  
    uri: https://ljxwtl.cn
  63.  
    filters:
  64.  
    ### Path的深度
  65.  
    - StripPrefix=1
  66.  
    ###匹配规则
  67.  
    predicates:
  68.  
    - Path=/cookie/**
  69.  
    - Header=auth,\d
  70.  
    - Cookie=sessionId,test
  71.  
    ###路由id
  72.  
    - id: host
  73.  
    ####转发https://ljxwtl.cn/
  74.  
    uri: https://ljxwtl.cn
  75.  
    filters:
  76.  
    ### Path的深度
  77.  
    - StripPrefix=1
  78.  
    ###匹配规则
  79.  
    predicates:
  80.  
    - Path=/host/**
  81.  
    - Header=auth,\d
  82.  
    - Cookie=sessionId,test
  83.  
    - Host=**.百度.com
  84.  
    ###路由id
  85.  
    - id: method
  86.  
    ####转发https://ljxwtl.cn/
  87.  
    uri: https://ljxwtl.cn
  88.  
    filters:
  89.  
    ### Path的深度
  90.  
    - StripPrefix=1
  91.  
    ###匹配规则
  92.  
    predicates:
  93.  
    - Path=/host/**
  94.  
    - Header=auth,\d
  95.  
    - Cookie=sessionId,test
  96.  
    - Host=**.百度.com
  97.  
    - Method=GET
学新通

学新通

使用 curl 测试,命令行输入:

# curl 默认是以 GET 的方式去请求

curl http://localhost:8010/host/selectByMovieLikeTitle?keyword=周星驰 -H"auth:123" --cookie "sessionId=test" -H"host:www.百度.com" -X GET

测试返回页面代码,证明匹配到路由,我们再以 POST 的方式请求测试。

# curl 默认是以 GET 的方式去请求

curl http://localhost:8010/host/selectByMovieLikeTitle?keyword=周星驰 -H"auth:123" --cookie "sessionId=test" -H"host:www.百度.com" -X POST

返回 404 没有找到,证明没有匹配上路由

3.1.6 通过请求路径匹配

Path Route Predicate 接收一个匹配路径的参数来判断是否走路由。

  1.  
    server:
  2.  
    port: 8010
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: @artifactId@
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
  11.  
    config:
  12.  
    server-addr: ${spring.cloud.nacos.discovery.server-addr}
  13.  
    file-extension: yml
  14.  
    namespace: public
  15.  
    group: DEFAULT_GROUP
  16.  
    gateway:
  17.  
    routes:
  18.  
    ###路由id
  19.  
    - id: ljxwtl.cn
  20.  
    ####转发https://ljxwtl.cn/
  21.  
    uri: https://ljxwtl.cn
  22.  
    filters:
  23.  
    ### Path的深度
  24.  
    - StripPrefix=2
  25.  
    ###匹配规则
  26.  
    predicates:
  27.  
    - Path=/alibaba/ljxwtl/**
  28.  
    ###路由id
  29.  
    - id: order
  30.  
    ####转发https://ljxwtl.cn/
  31.  
    uri: lb://ljxwtl-order-biz/
  32.  
    filters:
  33.  
    ### Path的深度
  34.  
    - StripPrefix=2
  35.  
    ###匹配规则
  36.  
    predicates:
  37.  
    - Path=/gateway/order/**
  38.  
    ###路由id
  39.  
    - id: member
  40.  
    ####转发https://ljxwtl.cn/
  41.  
    uri: lb://ljxwtl-member-biz/
  42.  
    filters:
  43.  
    ### Path的深度
  44.  
    - StripPrefix=1
  45.  
    ###匹配规则
  46.  
    predicates:
  47.  
    - Path=/member/**
  48.  
    ###路由id
  49.  
    - id: header
  50.  
    ####转发https://ljxwtl.cn/
  51.  
    uri: https://ljxwtl.cn
  52.  
    filters:
  53.  
    ### Path的深度
  54.  
    - StripPrefix=1
  55.  
    ###匹配规则
  56.  
    predicates:
  57.  
    - Path=/header/**
  58.  
    - Header=auth,\d
  59.  
    ###路由id
  60.  
    - id: cookie
  61.  
    ####转发https://ljxwtl.cn/
  62.  
    uri: https://ljxwtl.cn
  63.  
    filters:
  64.  
    ### Path的深度
  65.  
    - StripPrefix=1
  66.  
    ###匹配规则
  67.  
    predicates:
  68.  
    - Path=/cookie/**
  69.  
    - Header=auth,\d
  70.  
    - Cookie=sessionId,test
  71.  
    ###路由id
  72.  
    - id: host
  73.  
    ####转发https://ljxwtl.cn/
  74.  
    uri: https://ljxwtl.cn
  75.  
    filters:
  76.  
    ### Path的深度
  77.  
    - StripPrefix=1
  78.  
    ###匹配规则
  79.  
    predicates:
  80.  
    - Path=/host/**
  81.  
    - Header=auth,\d
  82.  
    - Cookie=sessionId,test
  83.  
    - Host=**.百度.com
  84.  
    ###路由id
  85.  
    - id: method
  86.  
    ####转发https://ljxwtl.cn/
  87.  
    uri: https://ljxwtl.cn
  88.  
    filters:
  89.  
    ### Path的深度
  90.  
    - StripPrefix=1
  91.  
    ###匹配规则
  92.  
    predicates:
  93.  
    - Path=/host/**
  94.  
    - Header=auth,\d
  95.  
    - Cookie=sessionId,test
  96.  
    - Host=**.百度.com
  97.  
    - Method=GET
  98.  
    ###路由id
  99.  
    - id: path
  100.  
    ####转发https://ljxwtl.cn/
  101.  
    uri: https://ljxwtl.cn
  102.  
    filters:
  103.  
    ### Path的深度
  104.  
    - StripPrefix=1
  105.  
    ###匹配规则
  106.  
    predicates:
  107.  
    - Path=/host/{test}
  108.  
    - Header=auth,\d
  109.  
    - Cookie=sessionId,test
  110.  
    - Host=**.百度.com
  111.  
    - Method=GET
学新通

3.1.7 通过请求 ip 地址进行匹配

Predicate 也支持通过设置某个 ip 区间号段的请求才会路由,RemoteAddr Route Predicate 接受 cidr 符号(IPv4 或 IPv6 )字符串的列表(最小大小为1),例如 192.168.0.1/16 (其中 192.168.0.1 是 IP 地址,16 是子网掩码)。

  1.  
    server:
  2.  
    port: 8010
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: @artifactId@
  7.  
    cloud:
  8.  
    nacos:
  9.  
    discovery:
  10.  
    server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
  11.  
    config:
  12.  
    server-addr: ${spring.cloud.nacos.discovery.server-addr}
  13.  
    file-extension: yml
  14.  
    namespace: public
  15.  
    group: DEFAULT_GROUP
  16.  
    gateway:
  17.  
    routes:
  18.  
    ###路由id
  19.  
    - id: ljxwtl.cn
  20.  
    ####转发https://ljxwtl.cn/
  21.  
    uri: https://ljxwtl.cn
  22.  
    filters:
  23.  
    ### Path的深度
  24.  
    - StripPrefix=2
  25.  
    ###匹配规则
  26.  
    predicates:
  27.  
    - Path=/alibaba/ljxwtl/**
  28.  
    ###路由id
  29.  
    - id: order
  30.  
    ####转发https://ljxwtl.cn/
  31.  
    uri: lb://ljxwtl-order-biz/
  32.  
    filters:
  33.  
    ### Path的深度
  34.  
    - StripPrefix=2
  35.  
    ###匹配规则
  36.  
    predicates:
  37.  
    - Path=/gateway/order/**
  38.  
    ###路由id
  39.  
    - id: member
  40.  
    ####转发https://ljxwtl.cn/
  41.  
    uri: lb://ljxwtl-member-biz/
  42.  
    filters:
  43.  
    ### Path的深度
  44.  
    - StripPrefix=1
  45.  
    ###匹配规则
  46.  
    predicates:
  47.  
    - Path=/member/**
  48.  
    ###路由id
  49.  
    - id: header
  50.  
    ####转发https://ljxwtl.cn/
  51.  
    uri: https://ljxwtl.cn
  52.  
    filters:
  53.  
    ### Path的深度
  54.  
    - StripPrefix=1
  55.  
    ###匹配规则
  56.  
    predicates:
  57.  
    - Path=/header/**
  58.  
    - Header=auth,\d
  59.  
    ###路由id
  60.  
    - id: cookie
  61.  
    ####转发https://ljxwtl.cn/
  62.  
    uri: https://ljxwtl.cn
  63.  
    filters:
  64.  
    ### Path的深度
  65.  
    - StripPrefix=1
  66.  
    ###匹配规则
  67.  
    predicates:
  68.  
    - Path=/cookie/**
  69.  
    - Header=auth,\d
  70.  
    - Cookie=sessionId,test
  71.  
    ###路由id
  72.  
    - id: host
  73.  
    ####转发https://ljxwtl.cn/
  74.  
    uri: https://ljxwtl.cn
  75.  
    filters:
  76.  
    ### Path的深度
  77.  
    - StripPrefix=1
  78.  
    ###匹配规则
  79.  
    predicates:
  80.  
    - Path=/host/**
  81.  
    - Header=auth,\d
  82.  
    - Cookie=sessionId,test
  83.  
    - Host=**.百度.com
  84.  
    ###路由id
  85.  
    - id: method
  86.  
    ####转发https://ljxwtl.cn/
  87.  
    uri: https://ljxwtl.cn
  88.  
    filters:
  89.  
    ### Path的深度
  90.  
    - StripPrefix=1
  91.  
    ###匹配规则
  92.  
    predicates:
  93.  
    - Path=/host/**
  94.  
    - Header=auth,\d
  95.  
    - Cookie=sessionId,test
  96.  
    - Host=**.百度.com
  97.  
    - Method=GET
  98.  
    ###路由id
  99.  
    - id: path
  100.  
    ####转发https://ljxwtl.cn/
  101.  
    uri: https://ljxwtl.cn
  102.  
    filters:
  103.  
    ### Path的深度
  104.  
    - StripPrefix=1
  105.  
    ###匹配规则
  106.  
    predicates:
  107.  
    - Path=/host/{test}
  108.  
    - Header=auth,\d
  109.  
    - Cookie=sessionId,test
  110.  
    - Host=**.百度.com
  111.  
    - Method=GET
  112.  
    ###路由id
  113.  
    - id: ip
  114.  
    ####转发https://ljxwtl.cn/
  115.  
    uri: https://ljxwtl.cn
  116.  
    filters:
  117.  
    ### Path的深度
  118.  
    - StripPrefix=1
  119.  
    ###匹配规则
  120.  
    predicates:
  121.  
    - Path=/host/{test}
  122.  
    - Header=auth,\d
  123.  
    - Cookie=sessionId,test
  124.  
    - Host=**.百度.com
  125.  
    - Method=GET
  126.  
    - RemoteAddr=192.168.1.1/24
学新通

可以将此地址设置为本机的 ip 地址进行测试。

curl localhost:8010

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

3.1.8 组合使用

各种 Predicates 同时存在于同一个路由时,请求必须同时满足所有的条件才被这个路由匹配。

一个请求满足多个路由的断言条件时,请求只会被首个成功匹配的路由转发

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggbkfj
系列文章
更多 icon
同类精品
更多 icon
继续加载