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

本篇内容介绍了“如何解决gateway与spring-boot-starter-web冲突问题”的有关知识,在实际案例的操作过程中,今天遇到了这个问题。

gateway与spring-boot-starter-web 冲突

环境:

SpringCloud 版本 ----Hoxton.SR9

SpringBoot 版本 ---- 2.3.8.RELEASE 

问题描述:

使用 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

问题分析:

查看控制台打印日志:

82203447abf1519987435fe91cd97402.png

可以看到是 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 的内置容器,不支持非阻塞。也就是说用Zuul的话,没有这个问题,用gateway就不行。

问题解决:

思路分析:如果该模块不需要用到starter-web依赖的话,直接去掉该依赖就行。如果引用了其他模块,里面有starter-web依赖的话,则可以用下面两种方案去解决

方案一、 排除 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>

方案二、使用 starter-webflux 依赖替代 starter-web 依赖:

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

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

成功启动项目:

46984fb3b23259927885003a9f0e2918.png

 

二、另外:如果SpringBoot和SpringCloud的版本有冲突的话,

也会有gateway 和 starter-web 依赖冲突问题:

比如:

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.

原因:

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

版本冲突

 

解决:

可以删除:

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

 

 

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值