gateway與spring-boot-starter-web沖突問題的解決
gateway與spring-boot-starter-web 沖突
環(huán)境:
SpringCloud 版本 ---- Finchley.SR2
SpringBoot 版本 ---- 2.0.6.RELEASE
問題描述:
將 zuul 網(wǎng)關升級為 gateway 時,引入gateway 依賴啟動網(wǎng)關子項目報錯
引入的依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
啟動網(wǎng)關報錯
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
問題分析:
查看控制臺打印日志:

可以看到是 web 依賴下的 tomcat 容器啟動失敗,且打印出 nio 異常。
回顧一下 zuul 和 gateway 的區(qū)別
Zuul: 構建于 Servlet 2.5,兼容3.x,使用的是阻塞式的API,不支持長連接,比如 websockets。
Gateway構建于 Spring 5+,基于 Spring Boot 2.x 響應式的、非阻塞式的 API。同時,它支持 websockets,和 Spring 框架緊密集成
報錯原因:啟動時默認使用了 spring-boot-starter-web 的內(nèi)置容器,不支持非阻塞
問題解決:
有兩種解決方式:
1、 排除 web 內(nèi)置容器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Maven整個生命周期內(nèi)排除內(nèi)置容器,排除內(nèi)置容器導出成war包可以讓外部容器運行spring-boot項目-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
2、使用 spring-webflux 模塊
webflux 有一個全新的非堵塞的函數(shù)式 Reactive Web 框架,可以用來構建異步的、非堵塞的、事件驅動的服務
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
成功啟動項目

gateway 網(wǎng)關版本沖突問題
1、spring-cloud版本
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
2、sprring-boot版本
<version>2.0.3.RELEASE</version>
3、錯誤描述
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-21 16:53:50.138 ERROR 15308 --- [ 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.
4、原因
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
與
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
版本沖突
5、解決
可以刪除:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
quartz定時執(zhí)行任務,并配置web.xml的操作方法
下面小編就為大家?guī)硪黄猶uartz定時執(zhí)行任務,并配置web.xml的操作方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
Redis?command?timed?out兩種異常情況的解決方式
Redis是我們開發(fā)中常用的數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關于Redis?command?timed?out兩種異常情況的解決方式,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04
基于tomcat8 編寫字符編碼Filter過濾器無效問題的解決方法
下面小編就為大家分享一篇基于tomcat8 編寫字符編碼Filter過濾器無效問題的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Java ThreadPoolExecutor的參數(shù)深入理解
這篇文章主要介紹了Java ThreadPoolExecutor的參數(shù)深入理解的相關資料,需要的朋友可以參考下2017-03-03

