服务器之家

服务器之家 > 正文

spring-cloud-gateway启动踩坑及解决

时间:2021-11-23 13:18     来源/作者:DDF_YiChen

spring-cloud-gateway启动踩坑

本人使用的版本是2.1.2,以下只记录几个小问题,但确实实实在在的把个人恶心的要死要活的找不到办法,几经挣扎,最终解决。

更可恨的是开发的过程中,没有出现异常,后来由于项目组其它人加了依赖,不知不觉对项目的兼容造成了英雄,真的是被撞的头破血流,才找到原因

1、webflux与mvc不兼容

如类路径中引用了webmvc会导致项目启动不起来

异常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排除即可,如

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<dependency>
   <groupId>${project.groupId}</groupId>
   <artifactId>core</artifactId>
   <version>${project.version}</version>
   <exclusions>
        <!--
            1. webflux与webmvc不兼容,否则会项目启动不起来
        -->
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </exclusion>
</dependency>

2、webflux使用netty作为容器

不能使用tomcat,表现形式为网关工作转发正常,目标服务返回数据也正常,但是网关会无法解析返回的数据并最终由网关将数据返回给客户端

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依赖进来的jar包,然后排除即可。需要注意的是,要看清楚自己项目依赖的tomcat具体的maven坐标。

spring-cloud-gateway启动踩坑及解决

然后排除即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<dependency>
  <groupId>${project.groupId}</groupId>
  <artifactId>core</artifactId>
  <version>${project.version}</version>
  <exclusions>
      <!--
          1. webflux与webmvc不兼容,否则会项目启动不起来
          2. webflux使用Netty作为容器,如果使用tomcat,接口转发正常,但是会导致服务间的数据无法解析
              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、后来实验了下

关于1webflux和mvc不兼容项目启动不起来的异常,如果项目中存在了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

这个异常是因为spring cloud gateway 是webflux 项目,引了含有web-starter得项目就会出现冲突。因为Hystrix-dashboard中含有web-starter,所以出现冲突。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yichen0429/article/details/98203775

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部