javascript
Spring Cloud入门+深入(十二)-Gateway网关(一)
一、Gateway網關
1、簡介
上一代zuul 1.X??zuul官網
Spring Cloud Gateway官網
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
reactive
?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).
?
Spring Cloud入門+深入(一)
Spring Cloud入門+深入(十三)-Gateway網關(二)
總結
以上是生活随笔為你收集整理的Spring Cloud入门+深入(十二)-Gateway网关(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言,好爽(第一二三季)
- 下一篇: ColorImpact v2.8.1.3