gateway与spring-boot-starter-web冲突问题的解决

 更新时间:2021年07月16日 17:16:45   作者:Chen DingFeng  
这篇文章主要介绍了gateway与spring-boot-starter-web冲突问题的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

gateway与spring-boot-starter-web 冲突

环境:

SpringCloud 版本 ---- Finchley.SR2

SpringBoot 版本 ---- 2.0.6.RELEASE

问题描述:

将 zuul 网关升级为 gateway 时,引入gateway 依赖启动网关子项目报错

引入的依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

启动网关报错

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-12-31 10:26:35.211 ERROR 13124 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************
APPLICATION FAILED TO START
***************************

Description:
Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.

Action:
Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.
Process finished with exit code 1

问题分析:

查看控制台打印日志:

可以看到是 web 依赖下的 tomcat 容器启动失败,且打印出 nio 异常。

回顾一下 zuul 和 gateway 的区别

Zuul: 构建于 Servlet 2.5,兼容3.x,使用的是阻塞式的API,不支持长连接,比如 websockets。

Gateway构建于 Spring 5+,基于 Spring Boot 2.x 响应式的、非阻塞式的 API。同时,它支持 websockets,和 Spring 框架紧密集成

报错原因:启动时默认使用了 spring-boot-starter-web 的内置容器,不支持非阻塞

问题解决:

有两种解决方式:

1、 排除 web 内置容器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- Maven整个生命周期内排除内置容器,排除内置容器导出成war包可以让外部容器运行spring-boot项目-->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2、使用 spring-webflux 模块

webflux 有一个全新的非堵塞的函数式 Reactive Web 框架,可以用来构建异步的、非堵塞的、事件驱动的服务

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

成功启动项目

gateway 网关版本冲突问题

1、spring-cloud版本

<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>

2、sprring-boot版本

<version>2.0.3.RELEASE</version>

3、错误描述

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-21 16:53:50.138 ERROR 15308 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************
APPLICATION FAILED TO START
***************************

Description:
Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.

Action:
Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.

4、原因

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-gateway</artifactId>
 </dependency>

版本冲突

5、解决

可以删除:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

相关文章

  • idea一招搞定同步所有配置(导入或导出所有配置)

    idea一招搞定同步所有配置(导入或导出所有配置)

    使用intellij idea很长一段时间,软件相关的配置也都按照自己习惯的设置好,如果需要重装软件,还得需要重新设置,本文就详细的介绍了idea 同步所有配置,感兴趣的可以了解一下
    2021-07-07
  • Java 图表类库详解

    Java 图表类库详解

    本文主要介绍了Java图表类库的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • springboot使用IDEA远程Debug

    springboot使用IDEA远程Debug

    项目上线之后,如果日志打印的很模糊或者业务逻辑比较复杂,有时候无法定位具体的错误原因,因此可以通过IDEA远程代理进行Debug,本文就来介绍一下如何使用
    2021-06-06
  • Spring Native实现0.059s启动一个SpringBoot项目

    Spring Native实现0.059s启动一个SpringBoot项目

    Spring Native是Spring框架的一个子项目,旨在提供一种将Spring应用程序编译为本地可执行文件的方法,从而提高启动时间和资源效率,本文主要介绍了Spring Native实现0.059s启动一个SpringBoot项目,感兴趣的可以了解一下
    2024-02-02
  • Java中常用输出方式(print() println() printf())

    Java中常用输出方式(print() println() printf())

    这篇文章主要介绍了Java中常用输出方式(print() println() printf()),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • 分享几个提高Java性能的高效用法

    分享几个提高Java性能的高效用法

    这篇文章主要介绍了分享几个提高Java性能的高效用法 ,需要的朋友可以参考下
    2014-10-10
  • Java面试题冲刺第一天--基础篇1

    Java面试题冲刺第一天--基础篇1

    这篇文章主要为大家分享了最有价值的三道java面试题,涵盖内容全面,包括数据结构和算法相关的题目、经典面试编程题等,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • 由@NotNull注解引出的关于Java空指针的控制

    由@NotNull注解引出的关于Java空指针的控制

    这是一些很容易学会的简单技术,但是对于代码质量和健壮性来说确实很重要。以我的经验,仅是第一个小技巧就已经对改进代码质量具有很大的作用了
    2016-09-09
  • 浅谈JSON的数据交换、缓存问题和同步问题

    浅谈JSON的数据交换、缓存问题和同步问题

    这篇文章主要介绍了浅谈JSON的数据交换、缓存问题和同步问题,具有一定借鉴价值,需要的朋友可以参考下
    2017-12-12
  • SpringBoot jackson提供对LocalDate的支持方式

    SpringBoot jackson提供对LocalDate的支持方式

    这篇文章主要介绍了SpringBoot jackson提供对LocalDate的支持方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01

最新评论