当使用gateway的时候,我们可能会在gateway项目中引入一些额外的包,比如webmvc,starter-web等,当然默认是不需要这些的,而且这些放到里面也是会报错的,一般使用的场景,比如你项目中引入elk,然后加入traceId的时候,就需要使用拦截器等,就会有问题,不过拦截器一般使用gateway的filter就可以了,这个组件会在后面做讲解,引用了会有依赖冲突的,会报如下的错误
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这个pom文件的,然后将这个进行排除,解决jar冲突的依赖
因为gateway在其内部导入了webflux包,但是webmvc和webflux是不能同时出现的,是有冲突的,所以一般做法就是先忽略webmv 包,但是如果必须要使用的话,我们可以进行合理的拆分,将一些功能拆分出来,使用gateway里面中一些GatewayFilterFactory,GatewayPredicate来处理我们需要处理的逻辑