首页 > 其他分享 >Spring Cloud 2023 新特性 同步网关

Spring Cloud 2023 新特性 同步网关

时间:2023-10-11 22:44:07浏览次数:36  
标签:网关 spring Spring boot springframework 2023 RELEASE org 2.1

网关不支持传统 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
源码下载: https://gitee.com/log4j/pig.git -b 2023

标签:网关,spring,Spring,boot,springframework,2023,RELEASE,org,2.1
From: https://www.cnblogs.com/leng-leng/p/17758429.html

相关文章

  • 每日总结20231011
    代码时间(包括上课)3h代码量(行):100行博客数量(篇):1篇相关事项:1、今天是周三,今天上午上的是软件构造,软件构造讲的是程序规范化。2、今天下午我们进行了献血的演讲的观看,明白了献血的意义。3、今天还打算看看软件设计师相关的题目,我要过,我要通过,我要高分通过!......
  • 20231010
    //grant,privilege,reduction,lower,bestdiscount,cometothepoint,discountedprice,gointhehole,quantitydiscountgrant-授予,给予Tograntmeanstogiveorbestowsomethingtosomeone.Itinvolvesgivingpermission,approval,oraprivilegetoso......
  • 20231011
    //eclectic,extreme,halfway,medium,request,tradeoff,giveground,meanmethod,middleway,midwaypoint,strikeahappymediumeclectic-折中的,多元的Eclecticreferstoastyleorapproachthatcombineselementsorideasfromvarioussourcesorstyles.......
  • “储”类拔萃 | 天合储能荣获“2023年度中国新型储能系统集成商创新力奖”
    9月10日-11日,由中国化学与物理电源行业协会主办、100余家机构联办的碳中和能源高峰论坛暨第三届中国国际新型储能技术及工程应用大会在深圳圆满完成。天合储能受邀参会,荣获“2023年度中国新型储能系统集成商创新力奖”,同期天合光能董事长高纪凡作为光伏行业的引领者,致力于推动......
  • 20230921 做题记录
    20230921做题记录目录20230921做题记录总结1P2863[USACO06JAN]TheCowPromS2P2746[USACO5.3]校园网NetworkofSchools3P1407[国家集训队]稳定婚姻4P1072[NOIP2009提高组]Hankson的趣味题总结总计完成\(3+4\)题上午校内练习赛,下午改了上午的题,晚上继续......
  • 2023NOIP A层联测9
    A.长春花简单题。打表发现情况并不多,记录下平方后模\(p\)对应的值,然后枚举\(a\),用链表维护即可。点击查看代码#include<bits/stdc++.h>usingll=longlong;usingull=unsignedlonglong;inta[100005],b[100005],p,ans,mx;boolvis[100005];std::list<int>l;std::st......
  • 2023.10.11 一些好题
    A你有\(m\)个相同的球,球有性能\(c\),你可以测试\(x\),若\(x\gec\),那么球会碎掉,若\(x<c\),那么球不碎。性能的范围\(n\le1e5\)。求最多要测试多少次。首先答案有一个上限是\(\logn\)。所以令\(m\to\min(m,\logn)\)所以我们记状态可以记\(dp_{l,r,k}\)表示当前确......
  • 20231010NOIP训练赛
    20231010NOIP训练赛时间安排7:50-8:10写T18:10-8:40写T29:40-10:40写T310:40-11:50写T4总结没时间写T5,T4和T3没写对题解T1简单题,用两个桶记录一下,然后再做两遍前缀和T2二分+哈希T3分组背包T4双指针+动态开点的值域线段树T5建图之后发现是内向基环树森林,对于......
  • 2023.10.11——每日总结
    学习所花时间(包括上课):9h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.java的一些特性;明日计划:学习......
  • Spring Boot 日期格式化
    我们先了解下,为什么需要配置日期格式化?通常情况下,发起一个Http请求,SpringBoot会根据请求路径映射到指定Controller上的某个方法的参数上,接着,Spring会自动进行类型转换。对于日期类型的参数,Spring默认是没有配置如何将字符串转换成日期类型的未配置日期格式化会如何?我......