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,不支持長(zhǎng)連接,比如 websockets。
Gateway構(gòu)建于 Spring 5+,基于 Spring Boot 2.x 響應(yīng)式的、非阻塞式的 API。同時(shí),它支持 websockets,和 Spring 框架緊密集成
報(bào)錯(cuò)原因:?jiǎn)?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)文章
Spring Cloud使用Feign實(shí)現(xiàn)Form表單提交的示例
本篇文章主要介紹了Spring Cloud使用Feign實(shí)現(xiàn)Form表單提交的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
解決java.lang.NullPointerException問題(空指針異常)
本文詳細(xì)介紹了Java中的NullPointerException異常及其常見原因,包括對(duì)象引用為null、數(shù)組元素為null和方法返回null等情況,文章還提供了幾種解決空指針異常的方法,如使用if語句、Optional類、三元運(yùn)算符和異常處理等,通過這些方法,可以有效地避免空指針異常2025-02-02
android中判斷服務(wù)或者進(jìn)程是否存在實(shí)例
本篇文章主要介紹了android中判斷服務(wù)或者進(jìn)程是否存在實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
springboot線程池監(jiān)控的簡(jiǎn)單實(shí)現(xiàn)
本文主要介紹了springboot線程池監(jiān)控的簡(jiǎn)單實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
詳解Java如何在CompletableFuture中實(shí)現(xiàn)日志記錄
這篇文章主要為大家詳細(xì)介紹了一種slf4j自帶的MDC類,來記錄完整的請(qǐng)求日志,和在CompletableFuture異步線程中如何保留鏈路id,需要的可以參考一下2023-04-04
詳細(xì)介紹Java關(guān)鍵字throw?throws?Throwable的用法與區(qū)別
這篇文章主要介紹了java中throws與throw及Throwable的用法和區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

