Spring Cloud 2023 新特性支持同步网关

网关不支持传统 Servlet 容器
Spring Cloud Gateway 需要运行在提供的 Netty 运行时。它不能在传统的 Servlet 容器中工作,也不能在构建为 WAR 时工作。WebFlux 使用了异步非阻塞的编程模型,相较于传统的 MVC Servlet 需要理解和适应新的编程范式和响应式编程概念,因此学习曲线可能较陡峭。
如果在 spring-cloud-gateway 引入了 tomcat 等传统容器会抛出如下异常:
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>配置转发规则
所有客户端请求 127.0.0.1:8080 转发至 pig4cloud.com
spring:
cloud:
gateway:
mvc:
routes:
- id: pig
uri: https://pig4cloud.com
predicates:
- Path=/注意:如果使用 gateway-mvc ,如有配置规则需要增加 mvc 这个层级的配置不然无效

如何自定义网关 filter
打开 Spring Cloud Gateway 的官网,发现针对 MVC 支持的部分没有提供过多的参考资料。不过按笔者看来完全不需要参考其他资料,只要按照 Spring MVC 项目中如何编写 Filter 的方法,就可以实现 Gateway 的开发。
注意指定自定义 Filter 的顺序,Spring Cloud Gateway Filter 执行顺序与 WebFlux Filter 保持一致。

总结
Spring Cloud Gateway 对于同步 MVC 的支持,早在社区中就已提出(毕竟最早使用的都是 Zuul 1.x 切换过去,原本就是 MVC 的实现)。随着 Java 21 正式发布,虚拟线程的出现无形中对同步 MVC 提供了性能加持。官方在适配 Java 21 后,会正式发布相关的版本,Spring Cloud Gateway 的 MVC 版本将简化原有的网关逻辑开发难度,极大地丰富了网关应用。
PIG 微服务已支持 SpringBoot3.2 、SpringCloud 2023
以上就是Spring Cloud 2023 新特性支持同步网关的详细内容,更多关于Spring Cloud同步网关的资料请关注脚本之家其它相关文章!
相关文章
SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)
本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链路,通过使用MDC和配置日志格式,可以在日志中包含traceId,便于问题排查,同时,还在返回的包装类中注入traceId,以便用户反馈问题,感兴趣的朋友一起看看吧2025-02-02
SpringBoot中整合JodConverter实现文件在线预览功能
Spring Boot JodConverter是一个基于Spring Boot框架的文档转换工具,它使用JodConverter库来实现文档格式之间的转换,本文主要介绍了SpringBoot中整合JodConverter实现文件在线预览功能,需要的朋友可以参考下2024-04-04
使用jackson实现对象json之间的相互转换(spring boot)
这篇文章主要介绍了使用jackson实现对象json之间的相互转换(spring boot),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-09-09
Spring中使用LocalDateTime、LocalDate等参数作为入参
这篇文章主要介绍了Spring中使用LocalDateTime、LocalDate等参数作为入参,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-12-12


最新评论