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

網(wǎng)關(guān)不支持傳統(tǒng) Servlet 容器
Spring Cloud Gateway 需要運(yùn)行在提供的 Netty 運(yùn)行時(shí)。它不能在傳統(tǒng)的 Servlet 容器中工作,也不能在構(gòu)建為 WAR 時(shí)工作。WebFlux 使用了異步非阻塞的編程模型,相較于傳統(tǒng)的 MVC Servlet 需要理解和適應(yīng)新的編程范式和響應(yīng)式編程概念,因此學(xué)習(xí)曲線可能較陡峭。
如果在 spring-cloud-gateway 引入了 tomcat 等傳統(tǒng)容器會(huì)拋出如下異常:
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)版本,官方提供了對(duì) MVC Servlet 的支持,可以使用 tomcat 等容器替代 netty ,使用同步的 Servlet Filter 代替復(fù)雜的事件流的異步編程風(fēng)格。

快速開(kāi)始
- 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>配置轉(zhuǎn)發(fā)規(guī)則
所有客戶(hù)端請(qǐng)求 127.0.0.1:8080 轉(zhuǎn)發(fā)至 pig4cloud.com
spring:
cloud:
gateway:
mvc:
routes:
- id: pig
uri: https://pig4cloud.com
predicates:
- Path=/注意:如果使用 gateway-mvc ,如有配置規(guī)則需要增加 mvc 這個(gè)層級(jí)的配置不然無(wú)效

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

總結(jié)
Spring Cloud Gateway 對(duì)于同步 MVC 的支持,早在社區(qū)中就已提出(畢竟最早使用的都是 Zuul 1.x 切換過(guò)去,原本就是 MVC 的實(shí)現(xiàn))。隨著 Java 21 正式發(fā)布,虛擬線程的出現(xiàn)無(wú)形中對(duì)同步 MVC 提供了性能加持。官方在適配 Java 21 后,會(huì)正式發(fā)布相關(guān)的版本,Spring Cloud Gateway 的 MVC 版本將簡(jiǎn)化原有的網(wǎng)關(guān)邏輯開(kāi)發(fā)難度,極大地豐富了網(wǎng)關(guān)應(yīng)用。
PIG 微服務(wù)已支持 SpringBoot3.2 、SpringCloud 2023
以上就是Spring Cloud 2023 新特性支持同步網(wǎng)關(guān)的詳細(xì)內(nèi)容,更多關(guān)于Spring Cloud同步網(wǎng)關(guān)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringCloud之Admin服務(wù)監(jiān)控實(shí)現(xiàn)流程示例詳解
- SpringCloud使用AOP統(tǒng)一處理Web請(qǐng)求日志實(shí)現(xiàn)步驟
- SpringCloud配置服務(wù)端的ConfigServer設(shè)置安全認(rèn)證
- SpringCloud配置客戶(hù)端ConfigClient接入服務(wù)端
- SpringCloud聲明式Feign客戶(hù)端調(diào)用工具使用
- spring?cloud?gateway中netty線程池小優(yōu)化
- Spring?Cloud?Gateway中netty線程池優(yōu)化示例詳解
相關(guān)文章
SpringBoot項(xiàng)目注入?traceId?追蹤整個(gè)請(qǐng)求的日志鏈路(過(guò)程詳解)
本文介紹了如何在單體SpringBoot項(xiàng)目中通過(guò)手動(dòng)實(shí)現(xiàn)過(guò)濾器或攔截器來(lái)注入traceId,以追蹤整個(gè)請(qǐng)求的日志鏈路,通過(guò)使用MDC和配置日志格式,可以在日志中包含traceId,便于問(wèn)題排查,同時(shí),還在返回的包裝類(lèi)中注入traceId,以便用戶(hù)反饋問(wèn)題,感興趣的朋友一起看看吧2025-02-02
SpringBoot中整合JodConverter實(shí)現(xiàn)文件在線預(yù)覽功能
Spring Boot JodConverter是一個(gè)基于Spring Boot框架的文檔轉(zhuǎn)換工具,它使用JodConverter庫(kù)來(lái)實(shí)現(xiàn)文檔格式之間的轉(zhuǎn)換,本文主要介紹了SpringBoot中整合JodConverter實(shí)現(xiàn)文件在線預(yù)覽功能,需要的朋友可以參考下2024-04-04
使用jackson實(shí)現(xiàn)對(duì)象json之間的相互轉(zhuǎn)換(spring boot)
這篇文章主要介紹了使用jackson實(shí)現(xiàn)對(duì)象json之間的相互轉(zhuǎn)換(spring boot),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
IDEA搭建配置Java?Web項(xiàng)目的詳細(xì)步驟
解決表單post,get到springMVC后臺(tái)亂碼的問(wèn)題

