艰辛的学习之路——异常:Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this t

引言

在学习Spring Cloud 中的 Gateway 网关服务时,引入依赖, 编写配置文件,设置启动类后想着启动看看效果如何,结果不出意外的出现了异常报错:Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.

环境

这是我 gateway server 工程的 pom.xml 依赖包括 spring cloud gateway 和 eureka 的依赖

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>

这是我 gateway server 工程模块的 application.properties 配置文件,配置了 端口,IP地址,eureka 客户端,以及 spring cloud gateway 服务网关的相关转发和过滤配置:

server.port=8080
server.address=localhost
spring.application.name=api-gateway-server

# eureka 注册中心信息配置
eureka.client.service-url.defaultZone=http://localhost:9000/eureka
eureka.instance.prefer-ip-address=true


# 配置 Spring Cloud Gateway 路由
spring.cloud.gateway.routes[0].id=product-service
#spring.cloud.gateway.routes[0].uri=http://127.0.0.1:9001
# lb:// 表示根据微服务名称从注册中心拉取请求路劲
spring.cloud.gateway.routes=[0].uri=lb://service-product
spring.cloud.gateway.routes[0].predicates[0]=Path=/product-service/**      #/product/**
# 路由过滤器 : 路径重写过滤器
spring.cloud.gateway.routes[0].filters[0]=RewritePath=/product-service/(?<segment>.*),/\${segment}
# 配置自动根据微服务名称进行路由转发:
# 开启更具微服务名称自动转发 : http://localhost:8080/service-product/product/1
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

异常

下面是异常报错的信息:翻译过来就是spring mvc 的依赖冲突了,需要去除 spring-boot-starter-web 这个依赖

**********************************************************

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.

**********************************************************
![在这里插入图片描述](https://img-blog.csdnimg.cn/411610887af64e688030e30dbcfa1aa7.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0hLX0hI,size_16,color_FFFFFF,t_70)

原因

原来是springcloudgateway的内部是通过netty+webflux实现的,webflux实现和springmvc配置依赖冲突。

解决

根据以上思路想到了2中解决方法:

  • 第一种:去除父工程中的 spring-boot-starter-web 这个依赖, 然后在各个子工程需要的地方进行单独引入依赖,在我们的 gateway server 工程中不要引入, 刷新项目即可
  • 第二种:既然是2中 mvc 存在冲突那我就在加载配置的时候忽略,去除加载一种就好了,通过pom.xml 中进行修改即可: 改为以下依赖, 替换原来的 spring cloud gateway
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-webflux</artifactId>
                </exclusion>
            </exclusions>
</dependency>

总结

此时服务已经成功运行起来了,有什么问题都可以留言私信哦, 垃圾博主什么问题都遇到过
在这里插入图片描述

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值