栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实战项目笔记(四)——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'. 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'. Check configuration for 'predicates'

Action:

Update your application's configuration

原因分析:

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

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


踩坑二:

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


    org.springframework.cloud
    spring-cloud-starter-gateway

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


    org.springframework.cloud
    spring-cloud-starter-gateway
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        
    

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

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

解决方案:

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

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


    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework
        spring-webmvc
    
    
        org.hibernate
        hibernate-validator
    

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1014956.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号