Gateway和spring-boot-starter-web的恩怨情仇

这篇具有很好参考价值的文章主要介绍了Gateway和spring-boot-starter-web的恩怨情仇。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

为什么取这个题目,其实与我踩到的坑有关,说起来这个坑非常神奇,这里面就涉及到Gateway和spring-boot-starter-web底层所依赖的技术不兼容的问题。

一、背景

SpringCloud 版本 ---- Finchley.SR2
SpringBoot 版本 ---- 2.0.6.RELEASE
如果同时在一个SpringBoot项目中引入了Gateway和spring-boot-starter-web,那么启动项目的时候会报错。

Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
2019-12-31 10:26:35.211 ERROR 13124 — [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:
Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type ‘org.springframework.http.codec.ServerCodecConfigurer’ that could not be found.
Action:
Consider defining a bean of type ‘org.springframework.http.codec.ServerCodecConfigurer’ in your configuration.
Process finished with exit code 1

但是移除了spring-boot-starter-web 那么Gateway项目就可以启动了

二、问题分析

查看控制台打印日志:starter-web starter-gateway,Java入门,Java提高,Java扩展,gateway,前端,springmvc,webflux,异步框架和同步框架,响应式编程
可以看到是 web 依赖下的 tomcat 容器启动失败,且打印出 nio 异常。

三、根源分析

3.1 原因分析

gateway应用的长链接,启动时基于netty-webflux ,gateway启动用的是webflux。然而Spring WebFlux 是 Spring Framework 5.0中引入的新的响应式web框架。与Spring MVC不同,它不需要Servlet API,是完全异步且非阻塞的,并且通过Reactor项目实现了Reactive Streams规范。

传统的spring-boot-starter-web,它的架构组合【spring-webmvc + Servlet + Tomcat】命令式的、同步阻塞的
响应式spring-webflux框架,它的架构组合【spring-webflux + Reactor + Netty】响应式的、异步非阻塞的

在一个web项目中存在基于两个不同的编程框架,互相冲突,从而导致项目启动报错。spring-boot-starter-web 和spring-boot-starter-webflux 相见分外眼红。不能配置在同一个pom.xml,或者不能同时用于同一个项目中。
如果一个项目中即引入Gateway又引入spring-boot-starter-web ,那么启动时默认使用了 spring-boot-starter-web 的内置容器,就会导致启动抛错,报错原因: spring-boot-starter-web 不支持gateway依赖非阻塞框架。

3.2 网关zuul和gateway区别

zuul基于同步webmvc(spring-boot-starter-web),而gateway是基于netty-webflux ,gateway启动用的是webflux,所以gateway和zuul不一样。

Zuul: 构建于 Servlet 2.5,兼容3.x,使用的是阻塞式的API,不支持长连接,比如 websockets。
Gateway构建于 Spring 5+,基于 Spring Boot 2.x 响应式的、非阻塞式的 API。同时,它支持 websockets,和 Spring 框架紧密集成

四、解决办法

4.1 排除web 内置容器

引入spring-boot-starter-web排除spring-boot-starter-tomcat

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- Maven整个生命周期内排除内置容器,排除内置容器导出成war包可以让外部容器运行spring-boot项目-->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

或者直接在gateway醒目中移除spring-boot-starter-web的全部引用

4.2 使用spring-webflux模块

webflux 有一个全新的非堵塞的函数式 Reactive Web 框架,可以用来构建异步的、非堵塞的、事件驱动的服务

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

五、总结

SpringCloud的生态非常的丰富,而且Spring的框架几经演变,出现了同步(springmvc)和异步(webflux),也出现了新技术webflux不兼容SpringMVC的情况,我们在从事java的spring开发应该要注意这些演变的区别,不断积累避免采坑。今天分享了gateway和spring-boot-starter-web的冲突,文章中有不对的地方,请多多指出,促使我们在学习中共同进步文章来源地址https://www.toymoban.com/news/detail-833111.html

到了这里,关于Gateway和spring-boot-starter-web的恩怨情仇的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • spring-cloud-starter-gateway-mvc的网关实现

    最近,我也一直在使用SpringCloudGateway开发我们自己的网关产品。根据我对官网文档:https://docs.spring.io/spring-cloud-gateway/reference/spring-cloud-gateway-server-mvc.html 的理解,内容如下: SpringCloudGateway的默认底层依赖项是SpringWebflux。我们知道Spring Webflux是异步和响应式编程,并且编程范

    2024年01月17日
    浏览(9)
  • Spring Boot - spring-boot-starter

    spring-boot-starter 当学习Spring Boot时,可以通过一个完整的案例来理解和实践其基本概念和功能。以下是一个简单的Spring Boot Starter完整案例,展示了如何创建一个基本的Web应用程序: 首先,创建一个名为pom.xml的Maven项目文件,添加以下内容:idea或其他直接创建直接跳过!

    2024年02月09日
    浏览(16)
  • Spring Cloud gateway 运行报错:Please set spring.main.web-application-type=reactive or remove spring-boot

    昨天在使用 Spring Cloud gateway 运行报错:“Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency”。经过一番分析与解决现在和大家分享一下解决办法。 首先,来了解一下这个报错的原因。该报错信息的意思是当前应用既引入了spring-cloud-starter-gateway包,又

    2024年01月24日
    浏览(11)
  • spring-cloud-starter-gateway 4.0.6负载均衡失败

    需要引入下面负载均衡依赖否则503找不到服务

    2024年02月16日
    浏览(11)
  • Spring Boot Starters

    Spring Boot Starters 概述 Spring Boot Starters是一系列为特定应用场景预设的依赖管理和自动配置方案。每个Starter都是为了简化特定类型的项目构建和配置。例如, spring-boot-starter-web 是为创建基于Spring MVC的Web应用程序而设计的。 Starter的结构 一个典型的Starter包含以下部分: pom.xml

    2024年01月25日
    浏览(28)
  • 6. Spring Boot的starters

    6. Spring Boot的starters

    6. Spring Boot的starters(重要) 一般认为,SpringBoot 微框架从两个主要层面影响 Spring 社区的开发者们: 基于 Spring 框架的“约定优先于配置(COC)”理念以及最佳实践之路。 提供了针对日常企业应用研发各种场景的 spring-boot-starter 自动配置依赖模块,如此多“开箱即用”的依赖模

    2024年01月24日
    浏览(13)
  • Spring Boot Starter设计实现

    Starter 是 Spring Boot 非常重要的一个硬核功能。 通过 Starter 我们可以快速的引入一个功能或模块,而无须关心模块依赖的其它组件。关于配置,Spring Boot 采用“约定大于配置”的设计理念,Starter 一般都会提供默认配置,只有当我们有特殊需求的时候,才需要在 application.yaml 里

    2024年01月18日
    浏览(12)
  • Spring Boot Starter Parent

    Spring Boot Starter Parent

    在这,您将学习了解 Spring Boot Starter Parent, 它是 Spring Boot 提供的父级 Pom 文件,旨在提供自动版本依赖管理,帮助我们轻松快速地进行 Spring Boot 开发。 通过 Spring Boot Starter Parent, 我们可以进行简单便捷地包依赖管理。在 Spring Boot 每一个发行版中, 均提供了该版本所兼容的依

    2024年02月08日
    浏览(8)
  • 自定义Spring Boot Starter

    自定义Spring Boot Starter

    Spring Boot starter 我们知道Spring Boot大大简化了项目初始搭建以及开发过程,而这些都是通过Spring Boot提供的starter来完成的。在实际项目中一些基础模块其本质就是starter,所以我们需要对Spring Boot的starter有一个全面深入的了解,这是我们的必备知识。 starter介绍 spring boot 在配置

    2024年02月10日
    浏览(10)
  • shiro-spring-boot-starter针对不同Spring Boot版本

    对于Spring Boot 2.4.10,无法找到shiro-spring-boot-starter的2.7.2版本,这是一个错误的版本号。 shiro-spring-boot-starter针对不同Spring Boot版本,推荐使用的版本如下: Spring Boot 1.x - 使用版本1.4.1 Spring Boot 2.0.x - 使用版本1.5.3 Spring Boot 2.1.x - 使用版本1.6.0 Spring Boot 2.2.x - 使用版本1.7.0 Spring Boot 2.3

    2024年02月13日
    浏览(10)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包