Spring Cloud 2023 新特性支持同步網(wǎng)關

網(wǎng)關不支持傳統(tǒng) Servlet 容器
Spring Cloud Gateway 需要運行在提供的 Netty 運行時。它不能在傳統(tǒng)的 Servlet 容器中工作,也不能在構建為 WAR 時工作。WebFlux 使用了異步非阻塞的編程模型,相較于傳統(tǒng)的 MVC Servlet 需要理解和適應新的編程范式和響應式編程概念,因此學習曲線可能較陡峭。
如果在 spring-cloud-gateway 引入了 tomcat 等傳統(tǒng)容器會拋出如下異常:
14 Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
15 at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:124) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
16 at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
17 at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:414) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
18 at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:178) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
19 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
20 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
21 ... 8 common frames omitted
MVC Servlet 支持
在 SpringCloud 2023 版本(Spring Cloud Gateway 4.1)版本,官方提供了對 MVC Servlet 的支持,可以使用 tomcat 等容器替代 netty ,使用同步的 Servlet Filter 代替復雜的事件流的異步編程風格。

快速開始
- springboot 3.2
- spring clouod 2023
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway-mvc</artifactId>
</dependency>配置轉發(fā)規(guī)則
所有客戶端請求 127.0.0.1:8080 轉發(fā)至 pig4cloud.com
spring:
cloud:
gateway:
mvc:
routes:
- id: pig
uri: https://pig4cloud.com
predicates:
- Path=/注意:如果使用 gateway-mvc ,如有配置規(guī)則需要增加 mvc 這個層級的配置不然無效

如何自定義網(wǎng)關 filter
打開 Spring Cloud Gateway 的官網(wǎng),發(fā)現(xiàn)針對 MVC 支持的部分沒有提供過多的參考資料。不過按筆者看來完全不需要參考其他資料,只要按照 Spring MVC 項目中如何編寫 Filter 的方法,就可以實現(xiàn) Gateway 的開發(fā)。
注意指定自定義 Filter 的順序,Spring Cloud Gateway Filter 執(zhí)行順序與 WebFlux Filter 保持一致。

總結
Spring Cloud Gateway 對于同步 MVC 的支持,早在社區(qū)中就已提出(畢竟最早使用的都是 Zuul 1.x 切換過去,原本就是 MVC 的實現(xiàn))。隨著 Java 21 正式發(fā)布,虛擬線程的出現(xiàn)無形中對同步 MVC 提供了性能加持。官方在適配 Java 21 后,會正式發(fā)布相關的版本,Spring Cloud Gateway 的 MVC 版本將簡化原有的網(wǎng)關邏輯開發(fā)難度,極大地豐富了網(wǎng)關應用。
PIG 微服務已支持 SpringBoot3.2 、SpringCloud 2023
以上就是Spring Cloud 2023 新特性支持同步網(wǎng)關的詳細內(nèi)容,更多關于Spring Cloud同步網(wǎng)關的資料請關注腳本之家其它相關文章!
相關文章
Java找出兩個大數(shù)據(jù)量List集合中的不同元素的方法總結
本文將帶大家了解如何快速的找出兩個相似度非常高的List集合里的不同元素。主要通過Java API、List集合雙層遍歷比較不同、借助Map集合查找三種方式,需要的可以參考一下2022-10-10
Java 實戰(zhàn)項目之精美物流管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實現(xiàn)一個精美的物流管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
SpringBoot3整合Hutool-captcha實現(xiàn)圖形驗證碼
在整合技術框架的時候,想找一個圖形驗證碼相關的框架,看到很多驗證碼的maven庫不再更新了或中央倉庫下載不下來,還需要多引入依賴,后面看到了Hutool圖形驗證碼(Hutool-captcha)中對驗證碼的實現(xiàn),所以本文介紹了SpringBoot3整合Hutool-captcha實現(xiàn)圖形驗證碼2024-11-11

