spring-cloud-gateway启动踩坑之路

  • Post author:
  • Post category:其他


本人使用的版本是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

排除即可,如

<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>
  1. 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

坐标。

在这里插入图片描述

然后排除即可

<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>
  1. 后来实验了下,关于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)}



版权声明:本文为yichen0429原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。