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)}

解决办法,找到依赖webmvcjar包,将webmvc排除即可,如


   ${project.groupId}
   core
   ${project.version}
   
        
        
            org.springframework
            spring-webmvc
        

  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坐标。
spring-cloud-gateway启动踩坑之路_第1张图片
然后排除即可


  ${project.groupId}
  core
  ${project.version}
  
      
      
          org.springframework
          spring-webmvc
      
      
          org.springframework.bootk
          spring-boot-starter-tomcat
      
      
          org.apache.tomcat.embed
          tomcat-embed-core
      
      
          org.apache.tomcat.embed
          tomcat-embed-el
      
      
          org.apache.tomcat.embed
          tomcat-embed-websocket
      
  

  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)}

你可能感兴趣的:(微服务)