You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Cloud Gateway整合OAuth2时Tomcat依赖冲突问题及解决尝试

Spring Cloud Gateway + OAuth2: 解决Tomcat与Netty冲突导致网关失效的问题

我之前也碰到过一模一样的坑!当Spring Cloud Gateway整合OAuth2时,如果项目里直接或者间接引入了spring-boot-starter-tomcat依赖,就会触发嵌入式Tomcat启动,而不是网关默认依赖的Netty服务器——这直接导致网关没法正常工作,毕竟Netty才是Spring Cloud Gateway原生设计的高性能异步服务器呀。

问题根源

Spring Cloud Gateway的核心依赖spring-cloud-starter-gateway默认引入Netty作为底层网络容器,但如果你的项目(比如OAuth2相关的starter依赖)间接带入了spring-boot-starter-tomcat,Spring Boot会优先选择Tomcat作为嵌入式服务器,这就和网关的运行要求完全冲突了。

解决方案:排除Tomcat依赖

你找的方向完全正确!需要在引入的相关依赖中排除spring-boot-starter-tomcat,具体的Maven配置如下:

<exclusions>
    <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
</exclusions>

⚠️ 注意:要把这段排除配置加到所有间接引入Tomcat的依赖上,比如如果是OAuth2的客户端依赖(比如spring-boot-starter-oauth2-client)带来的Tomcat,就需要在该依赖的<dependency>标签内添加这段<exclusions>配置。

验证是否生效

配置完成后启动项目,查看控制台日志,如果看到类似Netty started on port(s): 8080的输出,就说明已经成功切换到Netty服务器,网关应该能正常处理请求了。

内容的提问来源于stack exchange,提问作者ashraf revo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:59:04