实战项目笔记(四)——gateway部署的常见问题

项目场景:

        我在这个分布式项目中增加网关微服务的时候同样依赖了各个微服务的都依赖的公共服务,在网关启动的时候出现了


问题描述

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.
Reason: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.util.List<org.springframework.cloud.gateway.handler.predicate.PredicateDefinition>'. Check configuration for 'predicates'
刚进来的同学可以直接跳到解决方案,如果成功解决可以看一下原因!

踩坑一:

项目没有爆红,在网关项目启动的时候项目报错信息如下:

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

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

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


2022-07-12 17:52:05.656  WARN 36280 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'nettyWriteResponseFilter' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration$NettyConfiguration.class]: Unsatisfied dependency expressed through method 'nettyWriteResponseFilter' parameter 0; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'gatewayProperties': Could not bind properties to 'GatewayProperties' : prefix=spring.cloud.gateway, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.cloud.gateway.routes[0]' to org.springframework.cloud.gateway.route.RouteDefinition
2022-07-12 17:52:05.658  INFO 36280 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-07-12 17:52:05.669  INFO 36280 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-07-12 17:52:05.676 ERROR 36280 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Failed to bind properties under 'spring.cloud.gateway.routes[0]' to org.springframework.cloud.gateway.route.RouteDefinition:

    Reason: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.util.List<org.springframework.cloud.gateway.handler.predicate.PredicateDefinition>'. Check configuration for 'predicates'

Action:

Update your application's configuration

原因分析:

去网上找了好多,最后定位到spring-cloud gateway的内部是通过netty+webflux实现的,webflux实现和springmvc配置依赖冲突。

找到了原因于是乎我开始借鉴各个大神的方案,这个时候我就又悄悄的进了另外一个坑。。。


踩坑二:

根据网上找到的方法,想到了是因为依赖问题,于是乎我关注到了spring-cloud-gateway的依赖

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

 所以我加上了下面这段代码

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

         这样就解决了gateway依赖和spring boot依赖所用的web框架不同冲突的问题,并且项目也可以正常启动了,但是我发现我的gateway配置完后,在符合跳转的条件下没有进行任何的跳转,这时我忽然想到了,上边解决方案确实可以让项目正常启动,但是因为没有加载gateway相关依赖,所以也就导致了这个网关项目失去了网关的功能,变成了一个正常的spring boot项目。。。

        这个时候我想到了从依赖的公共组件方向出发,去把和gateway有冲突的其他依赖屏蔽掉

解决方案:

这里如果单纯是想让项目跑起来,可以选择上边的方法,如果希望保持gateway的功能,分享一下我的解决方案

在我们导入的公共依赖下加上下面的这段代码即可

<exclusions>
    <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </exclusion>
    <exclusion>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </exclusion>
    <exclusion>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </exclusion>
</exclusions>

        这是不是全部会造成冲突的依赖这个小编也不知道,大部分的问题是因为spring-boot-start-web这个jar包导致的,但是小编这个项目确确实实和这个没有关系,而是后边的两个jar包导致的,这个问题消耗了我将近半天的时间,分享出来希望你不会在这个问题上浪费太多时间,如果有帮助,希望给小编来个三连~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zi昂昂昂昂昂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值