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

目录
  • spring-cloud-gateway启动踩坑
    • 1、webflux与mvc不兼容
    • 2、webflux使用netty作为容器
    • 3、后来实验了下
  • 很坑得spring cloud gateway 异常

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

<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坐标。

然后排除即可

<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,所以出现冲突。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 初探Spring Cloud Gateway实战

    目录 关于Spring Cloud Gateway 版本信息 经典配置中的核心概念 启动nacos-2.0.3 源码下载 <Spring Cloud Gateway实战>系列的父工程 创建名为common的子工程,存放共用的常量和数据结构 创建web应用,作为服务提供方 开发一个简单的demo,完成spring-cloud-gateway的初体验 总结 关于Spring Cloud Gateway 这是一个基于Spring技术栈构建的API网关,涉及到:Spring5.Spring Boot

  • 创建网关项目(Spring Cloud Gateway)过程详解

    创建网关项目 加入网关后微服务的架构图 创建项目 POM文件 <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springfram

  • Spring Cloud Gateway入门解读

    Spring Cloud Gateway介绍 前段时间刚刚发布了Spring Boot 2正式版,Spring Cloud Gateway基于Spring Boot 2,是Spring Cloud的全新项目,该项目提供了一个构建在Spring 生态之上的API网关,包括:Spring 5,Spring Boot 2和Project Reactor. Spring Cloud Gateway旨在提供一种简单而有效的途径来发送API,并为他们提供横切关注点,例如:安全性,监控/指标和弹性.当前最新的

  • Springcloud GateWay网关配置过程图解

    一般为了不暴露自己的端口信息等,会选择架构一个网关在前面进行阻挡,起到保护的作用.附上一张工作示列图. 1.配置网关9527 gateway作为网关需要和其他的应用一样需要注册进eureka中进行管理,先创建应用gateway9527 pom文件,关键是gateway依赖 <dependencies> <dependency> <groupId>com.bai</groupId> <artifactId>cloud-api-common</

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

    目录 spring-cloud-gateway启动踩坑 1.webflux与mvc不兼容 2.webflux使用netty作为容器 3.后来实验了下 很坑得spring cloud gateway 异常 spring-cloud-gateway启动踩坑 本人使用的版本是2.1.2,以下只记录几个小问题,但确实实实在在的把个人恶心的要死要活的找不到办法,几经挣扎,最终解决. 更可恨的是开发的过程中,没有出现异常,后来由于项目组其它人加了依赖,不知不觉对项目的兼容造成了英雄,真的是被撞的头破血流,才

  • Spring Cloud Ribbon的踩坑记录与原理详析

    简介 Spring Cloud Ribbon 是一个基于Http和TCP的客服端负载均衡工具,它是基于Netflix Ribbon实现的.它不像服务注册中心.配置中心.API网关那样独立部署,但是它几乎存在于每个微服务的基础设施中.包括前面的提供的声明式服务调用也是基于该Ribbon实现的.理解Ribbon对于我们使用Spring Cloud来讲非常的重要,因为负载均衡是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一.在上节的例子中,我们采用了声明式的方式来实现负载均衡.实际上,内部

  • 基于Nacos实现Spring Cloud Gateway实现动态路由的方法

    简介 该文档主要介绍以Nacos为配置中心,实现Spring Cloud GateWay 实现动态路由的功能.Spring Cloud Gateway启动时候,就将路由配置和规则加载到内存里,无法做到不重启网关就可以动态的对应路由的配置和规则进行增加,修改和删除.通过nacos的配置下发的功能可以实现在不重启网关的情况下,实现动态路由. 集成 Spring Cloud GateWay集成 spring-cloud-starter-gateway:路由转发.请求过滤(权限校验.限流以及监控等) s

  • 解决spring cloud gateway 获取body内容并修改的问题

    之前写过一篇文章,如何获取body的内容. Spring Cloud Gateway获取body内容,不影响GET请求 确实能够获取所有body的内容了,不过今天终端同学调试接口的时候和我说,遇到了400的问题,报错是这样的HTTP method names must be tokens,搜了一下,都是说https引起的.可我的项目还没用https,排除了. 想到是不是因为修改了body内容导致的问题,试着不修改body的内容,直接传给微服务,果然没有报错了. 问题找到,那就好办了,肯定是我新构

  • 解决spring cloud服务启动之后回到命令行会自动挂掉问题

    我们的spring cloud微服务一般是打成jar包发布的,Linux下启动jar包和windows下一样,都是java -jar 包名,实际操作过的小伙伴可能会遇到这种情况:用java -jar启动之后,再切回到命令行服务会挂掉,怎么解决呢?使用nohup命令就不会了! 例: jar包: micro-service/micro-eureka-server-0.0.1-SNAPSHOT.jar 启动命令: ohup java -jar micro-service/micro-eureka-se

  • 解决Spring Cloud Gateway获取body内容,不影响GET请求的操作

    废话 这几天换了新工作,需要重新开发一套系统,技术选用Spring Cloud.在对接终端接口的时候要做验签,就涉及到在网关做拦截器,然后取出BODY里面的数据. 网上找了几个方法,有的拿不到数据,有的拿到数据之后不支持GET请求了.没有一个合理的解决办法,最后想到在动态路由构建的时候可以指定METHOD,于是有了如下解决办法 解决 @Bean public RouteLocator vmRouteLocator(RouteLocatorBuilder builder) { return bui

  • spring cloud gateway转发服务报错的解决

    目录 spring cloud gateway转发服务报错 错误如下 解决方案 使用gateWay做为网关遇到的404问题 GateWay有几个重要的配置,也是最重要的东西 我在项目中访问gateWay服务的时候 spring cloud gateway转发服务报错 错误如下 javax.net.ssl.SSLHandshakeException: error:1000009c:SSL routines:OPENSSL_internal:HTTP_REQUEST 在spring cloud ga

  • Nacos+Spring Cloud Gateway动态路由配置实现步骤

    目录 前言 一.Nacos环境准备 1.启动Nacos配置中心并创建路由配置 2.连接Nacos配置中心 二.项目构建 1.项目结构 2.编写测试代码 三.测试动态网关配置 1.启动服务,观察注册中心 2.访问网关,观察服务日志 四.总结 前言 Nacos最近项目一直在使用,其简单灵活,支持更细粒度的命令空间,分组等为麻烦复杂的环境切换提供了方便:同时也很好支持动态路由的配置,只需要简单的几步即可.在国产的注册中心.配置中心中比较突出,容易上手,本文通过gateway.nacos-consume

  • Spring Cloud Gateway 服务网关快速实现解析

    Spring Cloud Gateway 服务网关 API 主流网关有NGINX.ZUUL.Spring Cloud Gateway.Linkerd等:Spring Cloud Gateway构建于 Spring 5+,基于 Spring Boot 2.x 响应式的.非阻塞式的 API.同时,它支持 websockets,和 Spring 框架紧密集成,用来代替服务网关Zuul,开发体验相对来说十分不错. Spring Cloud Gateway 是 Spring Cloud 微服务平台的一个子

  • 深入学习spring cloud gateway 限流熔断

    目前,Spring Cloud Gateway是仅次于Spring Cloud Netflix的第二个最受欢迎的Spring Cloud项目(就GitHub上的星级而言).它是作为Spring Cloud系列中Zuul代理的继任者而创建的.该项目提供了用于微服务体系结构的API网关,并基于反应式Netty和Project Reactor构建.它旨在提供一种简单而有效的方法来路由到API并解决诸如安全性,监视/度量和弹性之类的普遍关注的问题. 基于Redis限流 Spring Cloud Gate

随机推荐