一、Gateway网关
1、简介
上一代zuul 1.X zuul官网
Spring Cloud 自己研发了一个网关,替换zuul,就是Spring Cloud Gateway。
Gateway是在Spring生态系统之上构建的API网关服务,基于Spring 5,Spring Boot 2 和Project Reactor等技术。Gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能,例如:熔断、限流、重试等。
Spring Cloud Gateway 是Spring的一个全新项目,基于Spring 5.0+Spring Boot 2.0和Project Reactor等技术开发的网关,它旨在微服务架构提供一种简单有效的统一的API路由管理方式。
Spring Cloud Gateway作为 Spring Cloud生态系统中的网关,目标是代替Zuul,在Spring Boot 2.0以上版本中,没有对新版本的Zuul2.0以上最新高性能版本进行集成,仍然还是使用Zuul 1.x非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。
Spring Cloud Gateway的目标提供统一的路由方式且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。
总结:Spring Cloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架。
Spring Cloud Gateway在架构图中的位置
2、作用
反向代理
流量控制
熔断
日志监控
3、为什么选择Gatway?
1.zuul2.0迟迟未发布。Gatway是基于异步非阻塞模型上进行开发的,性能方面不需要担心。
2.SpringCloud Gateway具有如下特性:
基于Spring Framework 5,Project Reactor 和 Spring Boot 2.0进行构建。
动态路由:能够匹配任何请求属性。
可以对路由指定Predicate(断言)和Filter(过滤器)。
集成Hystrix的断路器功能。
集成Spring Cloud服务发现功能。
易于编写的Predicate(断言)和Filter(过滤器)。
请求限流功能。
支持路径重写
3.SpringCloud Gateway与Zuul的区别
1、Zuul 1.x是一个基于阻塞I/O的API Gateway
2、Zuul 1.x基于Servlet2.5使用阻塞架构它不支持任何长连接(如WebSocket)Zuul的设计模式和Nginx较像,每次I/O操作都是从工作线程中选择一个执行,请求线程被阻塞到工作线程完成,但是差别是Nginx用C++实现,Zuul用Java实现,而JVM本身会有第一次加载较慢的情况,使用Zuul的性能相对较差。
3、Zuul2.x理念更先进,想基于Netty非阻塞和支持长连接,但是SpringCloud目前没有整合。
4、SpringCloud Gateway建立Spring Framework 5,Project Reactor 和 Spring Boot 2.0之上,使用非阻塞API。
5、SpringCloud Gateway还支持WebSocket,并且与Spring紧密集成拥有更好的开发体验。
二、Spring WebFlux
1. Spring WebFlux
The original web framework included in the Spring Framework, Spring Web MVC, was purpose-
built for the Servlet API and Servlet containers. The reactive-stack web framework, Spring
WebFlux, was added later in version 5.0. It is fully non-blocking, supports Reactive
Streams back pressure, and runs on such servers as Netty, Undertow, and Servlet 3.1+
containers.
Both web frameworks mirror the names of their source modules (spring-webmvc and spring-
webflux) and co-exist side by side in the Spring Framework. Each module is optional.
Applications can use one or the other module or, in some cases, both — for example, Spring
MVC controllers with the reactive WebClient.
传统的Web框架,比如说:struts2,springmvc等都是基于Servlet API与Servlet容器基础之上运行的。
在Servlet3.1之后有了异步非阻塞的支持。而WebFlux是一个典型非阻塞异步的框架,它的核心是基于Reactor的相关API实现的。相对于传统的Web框架,它可以运行在诸如Netty,Undertow及支持Servlet3.1的容器上。非阻塞式+函数式编程(Spring5必须让你使用java8).