Spring Cloud Gateway
- 网关
- 背景简介
- 基本介绍
- 网关在微服务架构中的位置:
- Spring Cloud Gateway 特点:
- Gateway的三大核心概念
- Route(路由)
- Predicate(断言/谓词)
- Filter(过滤)
- GateWay 工作流程
- Gateway配置
网关
背景简介
Spring Cloud全家桶中一个重要的组件就是网关,一代网关Zuul迟迟不更新,Spring Cloud自己研发了一个网关(Spring Cloud GateWay)代替Zuul。
基本介绍
网关在微服务架构中的位置:
官网关于Gateway2.2.6版本具体文档:https://docs.spring.io/spring-cloud-gateway/docs/2.2.6.RELEASE/reference/html/Spring Cloud GateWay 是基于WebFlux框架 ,使用Reactor模式, 而WebFlux框架底层使用的Netty。
Spring Cloud Gateway 特点:
- Built on Spring Framework 5, Project Reactor and Spring Boot 2.0 基于 Spring Framework 5、Project Reactor 和 Spring Boot 2.0 构建
- Able to match routes on any request attribute. 能够匹配任何请求属性的路由
- Predicates and filters are specific to routes. 谓词和过滤器特定于路由
- Circuit Breaker integration. 断路器集成。
- Spring Cloud DiscoveryClient integration Spring Cloud Discovery客户端集成
- Easy to write Predicates and Filters 易于编写谓词和过滤器
- Request Rate Limiting 请求速率限制
- Path Rewriting 路径重写
Gateway的三大核心概念
Route(路由)
路由是构建网关的基本模块,它有id,目标url,以及一系列的谓词过滤器组成。
Predicate(断言/谓词)
Predicate(断言)又称谓词,用于条件判断,只有断言结果都为真,才会真正的执行路由。断言其本质就是定义路由转发的条件。
这个东西其实可以看成一个布尔表达式,如果为真就啥都不干,放你过去。如果为假,即断言不成立,就不让你过去。
Predicate 内置工厂
- -Path=/nacos/provider/echo/** 当返回值为true 运行过滤器 请求路径
- -After=2021-01-28T23:59:59.789+08:00[Asia/Shanghai] 请求时间
- -Header=X-Request-Id, \d+ 请求头
- -Method=get 请求类型
- -Query=pageSize,\d+ 请求参数
Filter(过滤)
过滤器(Filter)就是在请求传递过程中,对请求和响应做一个处理。Gateway的Filter从作用范围可分为两种:GatewayFilter与GlobalFilter。其中:
- GatewayFilter:应用到单个路由或者一个分组的路由上。
- GlobalFilter:应用到所有的路由上(例如负载均衡过滤器,请求转发过滤器等)。
全局过滤器:
局部过滤器:
任何请求进来,网关都会把它们拦住。根据请求的URI把它们分配到不同的路由上,路由上面会有断言(谓词),来判断请求能不能进来。进来之后会有一系列的过滤器对请求被转发前或转发后进行改动。
GateWay 工作流程
- 客户端向Gateway发出请求。然后Gateway Handler Mapping找到与请求相匹配的路由,将其发送给Gateway Web Handler。
- Handler再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前或之后执行业务逻辑。
- Filter在请求之前可以做参数校验,权限校验,流量监控,日志输出,协议转换等等,在请求之后可以做响应内容、响应头的修改,日志输出,流量监控等.
Gateway配置
- 在pom.xml中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
- yml配置文件添加配置
server:
port: 9000
spring:
application:
name: sca-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway: #http://localhost:9000/nacos/provider/echo/aa
discovery:
locator:
enabled: true #开启通过服务注册中心的serviceId创建路由
routes: #路由元素,此元素下可以定义多个路由
- id: router01 #路由id,每个路由都会有一个唯一标识
# uri: http://localhost:8081 #网关要基于路由转发的路径,http://localhost:8081/nacos/provider/echo/aa
uri: lb://sca-provider #lb是负载均衡的意思,底层负载均衡的实现还是Ribbon
predicates: #断言(谓词),这里定义请求转发规则
- Path=/nacos/provider/echo/** #当返回值为true 运行过滤器 请求路径
# - After=2021-01-28T23:59:59.789+08:00[Asia/Shanghai] #请求时间
# - Header=X-Request-Id, \d+ #请求头
# - Method=get #请求类型
# - Query=pageSize,\d+ #请求参数
filters: #过滤器(特殊的拦截器,写到这个位置的是局部过滤器 全局过滤器:GlobalFilter
- StripPrefix=1 #去除path中的第一层目录 http://localhost:9000/provider/echo/aa
#logging:
# level:
# org.springframework.cloud.gateway: debug
- 主启动类
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
- 测试
通过Gateway网关 访问 8081 端口的微服务成功。
---------------------未完待续