gateway與spring-boot-starter-web沖突問題的解決
gateway與spring-boot-starter-web 沖突
環(huán)境:
SpringCloud 版本 ---- Finchley.SR2
SpringBoot 版本 ---- 2.0.6.RELEASE
問題描述:
將 zuul 網(wǎng)關(guān)升級(jí)為 gateway 時(shí),引入gateway 依賴啟動(dòng)網(wǎng)關(guān)子項(xiàng)目報(bào)錯(cuò)
引入的依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
啟動(dòng)網(wǎng)關(guān)報(bào)錯(cuò)
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
問題分析:
查看控制臺(tái)打印日志:
可以看到是 web 依賴下的 tomcat 容器啟動(dòng)失敗,且打印出 nio 異常。
回顧一下 zuul 和 gateway 的區(qū)別
Zuul: 構(gòu)建于 Servlet 2.5,兼容3.x,使用的是阻塞式的API,不支持長連接,比如 websockets。
Gateway構(gòu)建于 Spring 5+,基于 Spring Boot 2.x 響應(yīng)式的、非阻塞式的 API。同時(shí),它支持 websockets,和 Spring 框架緊密集成
報(bào)錯(cuò)原因:啟動(dòng)時(shí)默認(rèn)使用了 spring-boot-starter-web 的內(nèi)置容器,不支持非阻塞
問題解決:
有兩種解決方式:
1、 排除 web 內(nèi)置容器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- Maven整個(gè)生命周期內(nèi)排除內(nèi)置容器,排除內(nèi)置容器導(dǎo)出成war包可以讓外部容器運(yùn)行spring-boot項(xiàng)目--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
2、使用 spring-webflux 模塊
webflux 有一個(gè)全新的非堵塞的函數(shù)式 Reactive Web 框架,可以用來構(gòu)建異步的、非堵塞的、事件驅(qū)動(dòng)的服務(wù)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
成功啟動(dòng)項(xiàng)目
gateway 網(wǎng)關(guān)版本沖突問題
1、spring-cloud版本
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
2、sprring-boot版本
<version>2.0.3.RELEASE</version>
3、錯(cuò)誤描述
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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
quartz定時(shí)執(zhí)行任務(wù),并配置web.xml的操作方法
下面小編就為大家?guī)硪黄猶uartz定時(shí)執(zhí)行任務(wù),并配置web.xml的操作方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07Redis?command?timed?out兩種異常情況的解決方式
Redis是我們開發(fā)中常用的數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于Redis?command?timed?out兩種異常情況的解決方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04基于tomcat8 編寫字符編碼Filter過濾器無效問題的解決方法
下面小編就為大家分享一篇基于tomcat8 編寫字符編碼Filter過濾器無效問題的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01深入了解JAVA數(shù)據(jù)類型與運(yùn)算符
這篇文章主要介紹了Java基本數(shù)據(jù)類型和運(yùn)算符,結(jié)合實(shí)例形式詳細(xì)分析了java基本數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換、算術(shù)運(yùn)算符、邏輯運(yùn)算符等相關(guān)原理與操作技巧,需要的朋友可以參考下2021-07-07Java ThreadPoolExecutor的參數(shù)深入理解
這篇文章主要介紹了Java ThreadPoolExecutor的參數(shù)深入理解的相關(guān)資料,需要的朋友可以參考下2017-03-03