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项目就可以启动了

二、问题分析

查看控制台打印日志:

可以看到是 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

java 复制代码
<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 框架,可以用来构建异步的、非堵塞的、事件驱动的服务

java 复制代码
<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的冲突,文章中有不对的地方,请多多指出,促使我们在学习中共同进步

相关推荐
奔跑吧邓邓子14 分钟前
npm包管理深度探索:从基础到进阶全面教程!
前端·npm·node.js
前端李易安34 分钟前
ajax的原理,使用场景以及如何实现
前端·ajax·okhttp
汪子熙1 小时前
Angular 服务器端应用 ng-state tag 的作用介绍
前端·javascript·angular.js
Envyᥫᩣ1 小时前
《ASP.NET Web Forms 实现视频点赞功能的完整示例》
前端·asp.net·音视频·视频点赞
Мартин.5 小时前
[Meachines] [Easy] Sea WonderCMS-XSS-RCE+System Monitor 命令注入
前端·xss
昨天;明天。今天。7 小时前
案例-表白墙简单实现
前端·javascript·css
数云界7 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
风清扬_jd7 小时前
Chromium 如何定义一个chrome.settingsPrivate接口给前端调用c++
前端·c++·chrome
安冬的码畜日常7 小时前
【玩转 JS 函数式编程_006】2.2 小试牛刀:用函数式编程(FP)实现事件只触发一次
开发语言·前端·javascript·函数式编程·tdd·fp·jasmine
ChinaDragonDreamer7 小时前
Vite:为什么选 Vite
前端