SpringCloudAlibaba SpringCloudGateway
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主要的区别,还是在底层的通信框架上。
简单说明一下上文中的三个术语:
(1)Filter(过滤器):
和Zuul的过滤器在概念上类似,可以使用它拦截和修改请求,并且对上游的响应,进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的实例。
(2)Route(路由):
网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。
(3)Predicate(断言):
这是一个 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依赖:
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-gateway</artifactId>
-
</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资源路径,配置文件示例如下:
-
server:
-
port: 8010
-
-
spring:
-
application:
-
name: @artifactId@
-
cloud:
-
nacos:
-
discovery:
-
server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
-
config:
-
server-addr: ${spring.cloud.nacos.discovery.server-addr}
-
file-extension: yml
-
namespace: public
-
group: DEFAULT_GROUP
-
gateway:
-
routes:
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/albaba/ljxwtl/**
-
各字段含义如下:
- id:我们自定义的路由 ID,保持唯一
- uri:目标服务地址
- predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
- filters:过滤规则。
2.2 基于代码的路由配置方式
转发功能同样可以通过代码来实现,我们可以在启动类 GateWayApplication 中添加方法 customRouteLocator() 来定制转发规则。
-
package ljxwtl.gateway.config;
-
-
import org.springframework.cloud.gateway.route.RouteLocator;
-
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
-
/**
-
* @author: wtl
-
* @License: (C) Copyright 2021, wtl Corporation Limited.
-
* @Contact: 1050100468@qq.com
-
* @Date: 2021/11/6 7:51 上午
-
* @Version: 1.0
-
* @Description:
-
*/
-
-
public class GatewayStaticConfig {
-
-
/**
-
* 自定义路由规则
-
* @param routeLocatorBuilder 路由定位器
-
* @return RouteLocator
-
*/
-
-
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
-
return routeLocatorBuilder.routes()
-
.route("path_route", r -> r.path("/csdn")
-
.filters(gatewayFilterSpec -> {
-
return gatewayFilterSpec.stripPrefix(1);
-
})
-
.uri("https://blog.csdn.net"))
-
.build();
-
}
-
}
我们在yml配置文件中注销掉相关路由的配置,重启服务,访问链接:http://localhost:8080/ csdn, 可以看到和上面一样的页面,证明我们测试成功。
2.3 和注册中心相结合的路由配置方式
在uri的schema协议部分为自定义的lb:类型,表示从微服务注册中心(nacos)订阅服务,并且进行服务的路由。
一个典型的示例如下:
application.yml:
-
server:
-
port: 8010
-
-
spring:
-
application:
-
name: @artifactId@
-
cloud:
-
nacos:
-
discovery:
-
server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
-
config:
-
server-addr: ${spring.cloud.nacos.discovery.server-addr}
-
file-extension: yml
-
namespace: public
-
group: DEFAULT_GROUP
-
gateway:
-
routes:
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/albaba/ljxwtl/**
-
###路由id
-
- id: order
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-order-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/gateway/order/**
-
###路由id
-
- id: member
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-member-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- 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 的使用。
- 转发规则(predicates),假设转发uri都设定为http://localhost:9023
规则 | 实例 | 说明 |
---|---|---|
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 支持传入两个参数,一个是属性名一个为属性值,属性值可以是正则表达式。
-
server:
-
port: 8010
-
-
spring:
-
application:
-
name: @artifactId@
-
cloud:
-
nacos:
-
discovery:
-
server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
-
config:
-
server-addr: ${spring.cloud.nacos.discovery.server-addr}
-
file-extension: yml
-
namespace: public
-
group: DEFAULT_GROUP
-
gateway:
-
routes:
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/albaba/ljxwtl/**
-
###路由id
-
- id: order
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-order-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/gateway/order/**
-
###路由id
-
- id: member
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-member-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/member/**
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/ljxwtl/**
-
- Query=keyword
http://localhost:8010/ljxwtl/selectByMovieLikeTitle?keyword=周星驰
3.1.2 通过 Header 属性匹配
Header Route Predicate 和 Cookie Route Predicate 一样,也是接收 2 个参数,一个 header 中属性名称和一个正则表达式,这个属性值和正则表达式匹配则执行。
-
server:
-
port: 8010
-
-
spring:
-
application:
-
name: @artifactId@
-
cloud:
-
nacos:
-
discovery:
-
server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
-
config:
-
server-addr: ${spring.cloud.nacos.discovery.server-addr}
-
file-extension: yml
-
namespace: public
-
group: DEFAULT_GROUP
-
gateway:
-
routes:
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/alibaba/ljxwtl/**
-
###路由id
-
- id: order
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-order-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/gateway/order/**
-
###路由id
-
- id: member
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-member-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/member/**
-
###路由id
-
- id: header
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/header/**
-
- 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 值和正则表达式去匹配,如果匹配上就会执行路由,如果没有匹配上则不执行。
-
server:
-
port: 8010
-
-
spring:
-
application:
-
name: @artifactId@
-
cloud:
-
nacos:
-
discovery:
-
server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
-
config:
-
server-addr: ${spring.cloud.nacos.discovery.server-addr}
-
file-extension: yml
-
namespace: public
-
group: DEFAULT_GROUP
-
gateway:
-
routes:
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/alibaba/ljxwtl/**
-
###路由id
-
- id: order
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-order-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/gateway/order/**
-
###路由id
-
- id: member
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-member-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/member/**
-
###路由id
-
- id: header
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/header/**
-
- Header=auth,\d
-
###路由id
-
- id: cookie
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/cookie/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
则会返回页面代码,如果去掉--cookie "sessionId=test",后台汇报 404 错误。
3.1.4 通过 Host 匹配
Host Route Predicate 接收一组参数,一组匹配的域名列表,这个模板是一个 ant 分隔的模板,用.号作为分隔符。它通过参数中的主机地址作为匹配规则。
-
server:
-
port: 8010
-
-
spring:
-
application:
-
name: @artifactId@
-
cloud:
-
nacos:
-
discovery:
-
server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
-
config:
-
server-addr: ${spring.cloud.nacos.discovery.server-addr}
-
file-extension: yml
-
namespace: public
-
group: DEFAULT_GROUP
-
gateway:
-
routes:
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/alibaba/ljxwtl/**
-
###路由id
-
- id: order
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-order-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/gateway/order/**
-
###路由id
-
- id: member
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-member-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/member/**
-
###路由id
-
- id: header
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/header/**
-
- Header=auth,\d
-
###路由id
-
- id: cookie
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/cookie/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
###路由id
-
- id: host
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
经测试包含host头信息可匹配到 host_route 路由,去掉 host 参数则会报 404 错误。
3.1.5 通过请求方式匹配
可以通过是 POST、GET、PUT、DELETE 等不同的请求方式来进行路由。
-
server:
-
port: 8010
-
-
spring:
-
application:
-
name: @artifactId@
-
cloud:
-
nacos:
-
discovery:
-
server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
-
config:
-
server-addr: ${spring.cloud.nacos.discovery.server-addr}
-
file-extension: yml
-
namespace: public
-
group: DEFAULT_GROUP
-
gateway:
-
routes:
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/alibaba/ljxwtl/**
-
###路由id
-
- id: order
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-order-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/gateway/order/**
-
###路由id
-
- id: member
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-member-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/member/**
-
###路由id
-
- id: header
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/header/**
-
- Header=auth,\d
-
###路由id
-
- id: cookie
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/cookie/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
###路由id
-
- id: host
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
-
###路由id
-
- id: method
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
-
- 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 接收一个匹配路径的参数来判断是否走路由。
-
server:
-
port: 8010
-
-
spring:
-
application:
-
name: @artifactId@
-
cloud:
-
nacos:
-
discovery:
-
server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
-
config:
-
server-addr: ${spring.cloud.nacos.discovery.server-addr}
-
file-extension: yml
-
namespace: public
-
group: DEFAULT_GROUP
-
gateway:
-
routes:
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/alibaba/ljxwtl/**
-
###路由id
-
- id: order
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-order-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/gateway/order/**
-
###路由id
-
- id: member
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-member-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/member/**
-
###路由id
-
- id: header
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/header/**
-
- Header=auth,\d
-
###路由id
-
- id: cookie
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/cookie/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
###路由id
-
- id: host
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
-
###路由id
-
- id: method
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
-
- Method=GET
-
###路由id
-
- id: path
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/{test}
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
-
- 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 是子网掩码)。
-
server:
-
port: 8010
-
-
spring:
-
application:
-
name: @artifactId@
-
cloud:
-
nacos:
-
discovery:
-
server-addr: ${NACOS_HOST:ljxwtl.cn}:${NACOS_PORT:8848}
-
config:
-
server-addr: ${spring.cloud.nacos.discovery.server-addr}
-
file-extension: yml
-
namespace: public
-
group: DEFAULT_GROUP
-
gateway:
-
routes:
-
###路由id
-
- id: ljxwtl.cn
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/alibaba/ljxwtl/**
-
###路由id
-
- id: order
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-order-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=2
-
###匹配规则
-
predicates:
-
- Path=/gateway/order/**
-
###路由id
-
- id: member
-
####转发https://ljxwtl.cn/
-
uri: lb://ljxwtl-member-biz/
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/member/**
-
###路由id
-
- id: header
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/header/**
-
- Header=auth,\d
-
###路由id
-
- id: cookie
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/cookie/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
###路由id
-
- id: host
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
-
###路由id
-
- id: method
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/**
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
-
- Method=GET
-
###路由id
-
- id: path
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/{test}
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
-
- Method=GET
-
###路由id
-
- id: ip
-
####转发https://ljxwtl.cn/
-
uri: https://ljxwtl.cn
-
filters:
-
### Path的深度
-
- StripPrefix=1
-
###匹配规则
-
predicates:
-
- Path=/host/{test}
-
- Header=auth,\d
-
- Cookie=sessionId,test
-
- Host=**.百度.com
-
- Method=GET
-
- RemoteAddr=192.168.1.1/24
可以将此地址设置为本机的 ip 地址进行测试。
curl localhost:8010
如果请求的远程地址是 192.168.1.3,则此路由将匹配。
3.1.8 组合使用
各种 Predicates 同时存在于同一个路由时,请求必须同时满足所有的条件才被这个路由匹配。
一个请求满足多个路由的断言条件时,请求只会被首个成功匹配的路由转发
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhggbkfj
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13