文章目录
一、SpringCloud-GateWay介绍
GateWay介绍
网关作为流量的入口,常用的功能包括路由转发、权限校验、限流等。
Spring Cloud Gateway 是Spring Cloud官方推出的第二代网关框架,定位于取代Netfilx Zuul 1.0。相比于Zuul来说,Spring Cloud Gateway提供更优秀的性能,更强大的功能。
Spring Cloud Gateway 是由WebFiux+Netty+Reactor实现的响应式的API网关。它不能在传统的servlet容器中工作,也不能构建war包。
Spring Cloud GateWay旨在为微服务架构提供一种简单且有效的API路由的管理方式,并基于Filter的方式提供网关的基本功能,例如说安全认证、监控、限流等。
其他的网关组件
在Spring Cloud微服务体系中,有个很重要的组件就是网关,在1.x版本中都是采用的Zuul网关;但在2.x版本中,Zuul的升级一直跳票,SpringCloud最后自己研发了一个网关替代Zuul,那就是SpringCloud Gateway。
网上很多地方都说Zuul是阻塞的,Gateway是非阻塞的,这么说是不严谨的,准确的讲Zuul1.x是阻塞的,而在2.x的版本中,Zuul也是基于Netty,也是非阻塞的,如果说一定要说性能,其实这个真没多大差距。
二、Spring Cloud Gateway功能特征
- 基于Spring Framework5,Project Reactor和SpringBoot 2.0进行构建;
- 动态路由:能够匹配任何请求属性;
- 支持路径重写;
- 集成Spring Cloud服务发现功能(Nacos、Eruka);
- 可集成流控降级功能(Sentinel、Hystrix);
- 可以对路由指定易于编写的Predicate(断言)和Filter(过滤器);
2.1 核心概念
- 路由(route)
路由是网关中最基础的部分,路由信息包括一个ID、一个目的的URL、一组断言工厂、一组Filter组成。如果断言为真,则说明请求的URL和配置的路由匹配。
- 断言(predicates)
Java8的断言函数,SpringCloud Gateway中的断言函数类型是Spring5.0框架中的ServerWebExchange,断言函数允许开发者去定义匹配Http request中的任何信息,比如请求头和参数等。
- 过滤器(Filter)
SpringCloud Gateway中的filter分为Gateway Filter和Global Filter,Filter可以对请求和响应进行处理。
2.2 工作原理
Spring Cloud Gateway的工作原理跟Zuul的差不多,最大的区别就是Gateway的Filter只有pre和post两种。
客户端向Spring Cloud Gateway发出请求,如果请求与网关程序定义的路由匹配,则该请求就会被发送到网关Web处理程序,此时处理器程序运行特定的请求过滤器链。过滤器之间用虚线分开的原因是过滤器发送代理请求的前后执行逻辑。所有pre过滤器逻辑限制性。然后执行代理请求;代理请求完成后,执行post过滤器逻辑。
三、Spring Cloud Gateway快速开始
3.1 环境搭建
<!-- gateway依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
3.2 编写yml配置文件
server:
port: 8089
spring:
application:
name: api-gateway
cloud:
# gateway配置
gateway:
# 路由规则
routes:
- id: order_route # 路由的唯一标识,可以设置多个 - id:
uri: http://localhost:8020 # 需要转发的地址(订单服务)
# 断言规则 用于路由规则的匹配
predicates:
- Path=/order-serv/**
# http://localhost:8089/order-serv/order/create 路由到
# http://localhost:8020/order-serv/order/create
# 过滤器,转发之前去掉第一层路径/order-serv
filters:
- StripPrefix=1
# http://localhost:8020/order/create
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xx.xx.xx.xx:3306/seata_order?characterEncoding=utf8&useSSL=true&serverTimezone=GMT&serverTimezone=Asia/Shanghai
username: xx
password: xx
3.3 访问网关路由url,成功实现订单生成。
本地启动了库存和订单服务,分别都注册到nacos。网关服务调用订单服务url,进行路由转发。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IUnEJl4j-1667870947104)(F0978E5AEBD5487A962235790AD874F5)]
四、Gateway整合Nacos
4.1 添加nacos依赖
<!--nacos依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
4.2 修改配置文件
增加nacos配置信息,修改uri配置,lb://nacos注册服务名
server:
port: 8089
spring:
application:
name: api-gateway
cloud:
# gateway配置
gateway:
# 路由规则
routes:
- id: order_route # 路由的唯一标识,可以设置多个 - id:
uri: lb://service-order-ribbon # 需要转发的地址(订单服务) lb:使用nacos中得本地负载均衡策略 service-order-ribbon(nacos上得服务名)
# 断言规则 用于路由规则的匹配
predicates:
- Path=/order-serv/**
# http://localhost:8089/order-serv/order/create 路由到
# http://localhost:8020/order-serv/order/create
# 过滤器,转发之前去掉第一层路径/order-serv
filters:
- StripPrefix=1
# http://localhost:8020/order/create
# 配置nacos
nacos:
discovery:
server-addr: xx.xx.xx.xx:port
username: xxxx
password: xxxx
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xx.xx.xx.xx:3306/seata_order?characterEncoding=utf8&useSSL=true&serverTimezone=GMT&serverTimezone=Asia/Shanghai
username: xx
password: xx
4.3 访问网关路由url,成功实现订单生成。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bUXRv5ev-1667870947108)(F0978E5AEBD5487A962235790AD874F5)]
4.4 启动自动发现识别nacos服务,去除自定义路由与断言规则,配置如下(不推荐使用,更推荐使用自定义断言、过滤器规则)
server:
port: 8089
spring:
application:
name: api-gateway
cloud:
# gateway配置
gateway:
discovery:
locator:
enabled: true # 是否启动自动识别nacos服务
# 配置nacos
nacos:
discovery:
server-addr: xx.xx.xx.xx:port
username: xxxx
password: xxxx
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xx.xx.xx.xx:3306/seata_order?characterEncoding=utf8&useSSL=true&serverTimezone=GMT&serverTimezone=Asia/Shanghai
username: xx
password: xx
4.4.1 访问网关路由url,替换原有断言规则字符为nacos注册服务名。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VtV5PO12-1667870947110)(8235E8EC16454079AA4CF1F31D4D30BD)]
五、Gateway–内置路由断言工厂(Route Oredicate Factories)配置
作用
当请求gateway得时候,使用断言对请求进行匹配,如果匹配成功就由路由转发,如果匹配失败久返回404
5.1 断言工厂详细介绍
- After
说明:匹配某个时间点后的请求
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
# 时间格式忘记,可以使用这个函数输出当前时间。
ZonedDateTime.now();
- Before
说明:匹配某个时间点之前的请求
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
- Between
说明:匹配某两个时间点之间的请求
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
- Cookie
说明:匹配请求必须包含某些cookie,需包含一个key(chocolate),然后等于一个值(ch.p),值可以是正则表达式
- Cookie=chocolate, ch.p
- Header
说明:匹配请求必须包含某些header,请求头key(X-Request-Id),对应的值(\d+)支持正则表达式
- Header=X-Request-Id, \d+
- Host
说明:匹配请求必须是访问某个host(域名),支持通配符
- Host=**.somehost.org,**.anotherhost.org
- Method
说明:匹配请求方式必须是指定方式,多个逗号分割
- Method=GET,POST
- Path
说明:匹配请求路径必须符合指定规则,支持通配符也支持占位符
- Path=/red/{segment},/blue/{segment}
- Query
说明:匹配请求参数必须包含指定参数,比如green参数,或者包含red参数且值为gree.
- Query=green 或者 Query=red, gree.
- RemoteAddr
说明:匹配请求者的ip必须是指定范围
- RemoteAddr=192.168.1.1/24
- Weight
说明:匹配权重处理,配置权重一般为多个路由,配置方式为分组,权重(group1, 8)
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2