一,報(bào)錯(cuò)內(nèi)容
當(dāng)使用gateway的時(shí)候,我們可能會(huì)在gateway項(xiàng)目中引入一些額外的包,比如webmvc,starter-web等,當(dāng)然默認(rèn)是不需要這些的,而且這些放到里面也是會(huì)報(bào)錯(cuò)的,一般使用的場(chǎng)景,比如你項(xiàng)目中引入elk,然后加入traceId的時(shí)候,就需要使用攔截器等,就會(huì)有問(wèn)題,不過(guò)攔截器一般使用gateway的filter就可以了,這個(gè)組件會(huì)在后面做講解,引用了會(huì)有依賴沖突的,會(huì)報(bào)如下的錯(cuò)誤
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.
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

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.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.JAVA:163)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at com.hanvon.gateway.GatewayApplication.main(GatewayApplication.java:18)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:210)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160)
... 8 common frames omitted
二,解決方案
找到引用的包里面哪一些是有引用了spring-webmvc 或者 spring-boot-starter-web這個(gè)pom文件的,然后將這個(gè)進(jìn)行排除,解決jar沖突的依賴

三,原理
因?yàn)間ateway在其內(nèi)部導(dǎo)入了webflux包,但是webmvc和webflux是不能同時(shí)出現(xiàn)的,是有沖突的,所以一般做法就是先忽略webmv 包,但是如果必須要使用的話,我們可以進(jìn)行合理的拆分,將一些功能拆分出來(lái),使用gateway里面中一些GatewayFilterFactory,GatewayPredicate來(lái)處理我們需要處理的邏輯