文章目录
一、基础介绍
- Spring Cloud Gateway
Spring Cloud Gateway
是基于Spring
生态系统之上构建的API
网关,包括:Spring 5.x
,Spring Boot 2.x
和Project Reactor
。Spring Cloud Gateway
旨在提供一种简单而有效的方法来路由到API
,并为它们提供跨领域的关注点,例如:安全性
,监视/指标
,限流
等。
- 什么是服务网关
API Gateway(APIGW / API 网关)
,顾名思义,是系统对外的唯一入口。API
网关封装了系统内部架构,为每个客户端提供定制的API。 近几年来移动应用与企业间互联需求的兴起。从以前单一的Web应用,扩展到多种使用场景,且每种使用场景对后台服务的要求都不尽相同。 这不仅增加了后台服务的响应量,还增加了后台服务的复杂性。随着微服务架构概念的提出,API网关成为了微服务架构的一个标配组件。
- 为什么要使用网关
微服务的应用可能部署在不同机房,不同地区,不同域名下。此时客户端(浏览器/手机/软件工具)想 要请求对应的服务,都需要知道机器的具体 IP 或者域名 URL,当微服务实例众多时,这是非常难以记忆的,对 于客户端来说也太复杂难以维护。此时就有了网关,客户端相关的请求直接发送到网关,由网关根据请求标识 解析判断出具体的微服务地址,再把请求转发到微服务实例。这其中的记忆功能就全部交由网关来操作了。
- 核心概念
路由(Route)
:路由是网关最基础的部分,
路由信息由 ID、目标 URI、一组断言和一组过滤器组成。如果断言 路由为真,则说明请求的 URI 和配置匹配。
断言(Predicate)
:Java8 中的断言函数。Spring Cloud Gateway 中的断言函数输入类型是 Spring 5.0 框架中 的 ServerWebExchange。
Spring Cloud Gateway 中的断言函数允许开发者去定义匹配来自于 HttpRequest 中的任何信息,比如请求头和参数等。
过滤器(Filter)
:一个标准的 Spring Web Filter。Spring Cloud Gateway 中的 Filter 分为两种类型,分别是 Gateway Filter 和 Global Filter。过滤器将会对请求和响应进行处理。
二、使用网关
1、添加依赖
<!-- spring cloud gateway 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2、resources/application.yml
配置文件
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
filters:
- StripPrefix=1
3、网关启动类
@SpringBootApplication
public class TestGatewayApplication
{
public static void main(String[] args)
{
SpringApplication.run(RuoYiGatewayApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 若依网关启动成功 ლ(´ڡ`ლ)゙ \n" +
" .-------. ____ __ \n" +
" | _ _ \\ \\ \\ / / \n" +
" | ( ' ) | \\ _. / ' \n" +
" |(_ o _) / _( )_ .' \n" +
" | (_,_).' __ ___(_ o _)' \n" +
" | |\\ \\ | || |(_,_)' \n" +
" | | \\ `' /| `-' / \n" +
" | | \\ / \\ / \n" +
" ''-' `'-' `-..-' ");
}
}
三、路由规则
Spring Cloud Gateway
创建Route
对象时, 使用RoutePredicateFactory
创建Predicate
对象,Predicate
对象可以赋值给Route
。
Spring Cloud Gateway
包含许多内置的RoutePredicateFactories
。- 所有这些断言都匹配 HTTP 请求的不同属性。
- 多个
Route Predicate Factories
可以通过逻辑与(and)
结合起来一起使用。
路由断言工厂RoutePredicateFactory
包含的主要实现类如图所示,包括Datetime
、请求的远端地址、路由权重、请求头、Host 地址、请求方法、请求路径和请求参数等类型的路由断言。
1、Datetime
匹配日期时间之后发生的请求
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
- After=2022-10-02T17:16:00.000+08:00[Asia/Shanghai]
filters:
- StripPrefix=1
匹配日期时间之前发生的请求
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
- Before=2022-10-02T17:16:00.000+08:00[Asia/Shanghai]
filters:
- StripPrefix=1
匹配日期之间发生的请求
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
- Between=2022-10-02T17:20:00.000+08:00[Asia/Shanghai],2022-10-02T17:23:00.000+08:00[Asia/Shanghai]
filters:
- StripPrefix=1
2、Cookie
匹配指定名称且其值与正则表达式匹配的cookie
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
- Cookie=loginname, ruoyi
filters:
- StripPrefix=1
测试:控制台输入:curl http://localhost:8080/system/user/list --cookie "loginname=ruoyi"
3、Header
匹配具有指定名称的请求头,\d+
值匹配正则表达式
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
- Header=X-Request-Id, \d+
filters:
- StripPrefix=1
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ONAtAWe8-1680511729270)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
4、Host
匹配主机名的列表
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
- Host=**.somehost.org,**.anotherhost.org
filters:
- StripPrefix=1
5、Method
匹配请求methods的参数,它是一个或多个参数
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
- Method=POST,PUT
filters:
- StripPrefix=1
6、Path
匹配请求路径
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/user/list,/system/role/list
filters:
- StripPrefix=1
7、Query
匹配查询参数
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
- Query=username, abc.
filters:
- StripPrefix=1
测试浏览器访问http://localhost:8080/system/user/list?username=abc1
8、RemoteAddr
匹配请求的远程IP地址和子网掩码
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system
uri: http://localhost:9201/
predicates:
- Path=/system/**
- RemoteAddr=192.168.10.1/0
filters:
- StripPrefix=1
9、Weight
匹配权重,权重是按组计算的
server:
port: 8080
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
# 系统模块
- id: ruoyi-system-a
uri: http://192.168.33.2:9201/
predicates:
- Path=