spring-cloud-gateway啟動踩坑及解決
spring-cloud-gateway啟動踩坑
本人使用的版本是2.1.2,以下只記錄幾個小問題,但確實(shí)實(shí)實(shí)在在的把個人惡心的要死要活的找不到辦法,幾經(jīng)掙扎,最終解決。
更可恨的是開發(fā)的過程中,沒有出現(xiàn)異常,后來由于項(xiàng)目組其它人加了依賴,不知不覺對項(xiàng)目的兼容造成了英雄,真的是被撞的頭破血流,才找到原因
1、webflux與mvc不兼容
如類路徑中引用了webmvc會導(dǎo)致項(xiàng)目啟動不起來
異常1
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
異常2
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.convert.ConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=webFluxConversionService)}
解決辦法,找到依賴webmvc的jar包,將webmvc排除即可,如
<dependency> <groupId>${project.groupId}</groupId> <artifactId>core</artifactId> <version>${project.version}</version> <exclusions> <!-- 1. webflux與webmvc不兼容,否則會項(xiàng)目啟動不起來 --> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </exclusion> </dependency>
2、webflux使用netty作為容器
不能使用tomcat,表現(xiàn)形式為網(wǎng)關(guān)工作轉(zhuǎn)發(fā)正常,目標(biāo)服務(wù)返回?cái)?shù)據(jù)也正常,但是網(wǎng)關(guān)會無法解析返回的數(shù)據(jù)并最終由網(wǎng)關(guān)將數(shù)據(jù)返回給客戶端
java.lang.ClassCastException: org.springframework.core.io.buffer.DefaultDataBufferFactory cannot be cast to org.springframework.core.io.buffer.NettyDataBufferFactory
解決辦法
https://github.com/spring-cloud/spring-cloud-gateway/issues/145
找到將tomcat依賴進(jìn)來的jar包,然后排除即可。需要注意的是,要看清楚自己項(xiàng)目依賴的tomcat具體的maven坐標(biāo)。
然后排除即可
<dependency> <groupId>${project.groupId}</groupId> <artifactId>core</artifactId> <version>${project.version}</version> <exclusions> <!-- 1. webflux與webmvc不兼容,否則會項(xiàng)目啟動不起來 2. webflux使用Netty作為容器,如果使用tomcat,接口轉(zhuǎn)發(fā)正常,但是會導(dǎo)致服務(wù)間的數(shù)據(jù)無法解析 java.lang.ClassCastException: org.springframework.core.io.buffer.DefaultDataBufferFactory cannot be cast to org.springframework.core.io.buffer.NettyDataBufferFactory --> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </exclusion> <exclusion> <groupId>org.springframework.bootk</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> <exclusion> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> </exclusion> <exclusion> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-el</artifactId> </exclusion> <exclusion> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-websocket</artifactId> </exclusion> </exclusions> </dependency>
3、后來實(shí)驗(yàn)了下
關(guān)于1webflux和mvc不兼容項(xiàng)目啟動不起來的異常,如果項(xiàng)目中存在了tomcat的用來,則拋出的異常是
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
而如果沒有依賴tomcat則拋出的異常是
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.convert.ConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=webFluxConversionService)}
很坑得spring cloud gateway 異常
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.convert.ConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=webFluxConversionService)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1655) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1214) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1168) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
... 101 common frames omitted
這個異常是因?yàn)閟pring cloud gateway 是webflux 項(xiàng)目,引了含有web-starter得項(xiàng)目就會出現(xiàn)沖突。因?yàn)镠ystrix-dashboard中含有web-starter,所以出現(xiàn)沖突。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java 中使用匿名類直接new接口詳解及實(shí)例代碼
這篇文章主要介紹了java 中使用匿名類直接new接口詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03微信游戲打飛機(jī)游戲制作(java模擬微信打飛機(jī)游戲)
java模擬微信打飛機(jī)游戲,大家參考使用吧2013-12-12springboot整合過濾器實(shí)戰(zhàn)步驟
在項(xiàng)目開發(fā)過程中,過濾器或者攔截器幾乎是必用的,他可以很方便的完成類似日志處理、token驗(yàn)證等一系列操作,區(qū)別于業(yè)務(wù)接口,獨(dú)立進(jìn)行處理,感覺就是一種Aop思想。下面模擬請求接口前的token驗(yàn)證,進(jìn)行過濾器的實(shí)戰(zhàn)2022-04-04java兩個數(shù)組合并為一個數(shù)組的幾種方法
這篇文章主要給大家介紹了關(guān)于java兩個數(shù)組合并為一個數(shù)組的幾種方法,最近在寫代碼時遇到了需要合并兩個數(shù)組的需求,文中將每種方法都介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07舉例分析Python中設(shè)計(jì)模式之外觀模式的運(yùn)用
這篇文章主要介紹了Python中設(shè)計(jì)模式之外觀模式的運(yùn)用,外觀模式主張以分多模塊進(jìn)行代碼管理而減少耦合,需要的朋友可以參考下2016-03-03Java使用集合實(shí)現(xiàn)斗地主分牌完整代碼
在斗地主游戲中,通常是將一副牌平均分成3份,每份17張牌,并留3張底牌,我們可以使用集合來實(shí)現(xiàn)這一功能,這篇文章主要給大家介紹了關(guān)于Java使用集合實(shí)現(xiàn)斗地主分牌的相關(guān)資料,需要的朋友可以參考下2024-05-05android中判斷服務(wù)或者進(jìn)程是否存在實(shí)例
本篇文章主要介紹了android中判斷服務(wù)或者進(jìn)程是否存在實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05淺談Java中隨機(jī)數(shù)的幾種實(shí)現(xiàn)方式
這篇文章主要介紹了Java中隨機(jī)數(shù)的幾種實(shí)現(xiàn)方式,從最簡單的Math.random到多線程的并發(fā)實(shí)現(xiàn)都在本文所列之中,需要的朋友可以參考下2015-07-07