春天云网关;在类路径上发现 Spring MVC,与 Spring Cloud Gateway 不兼容问题
Posted
技术标签:
【中文标题】春天云网关;在类路径上发现 Spring MVC,与 Spring Cloud Gateway 不兼容问题【英文标题】:Spring Cloud Gateway; Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway Issue 【发布时间】:2021-10-05 19:32:31 【问题描述】:我在运行 API-GATEWAY 时遇到以下错误,我尝试了很多方法,但我无法解决这个问题。
说明:
在类路径中发现 Spring MVC,与 Spring Cloud Gateway 不兼容。
行动:
请设置 spring.main.web-application-type=reactive 或移除 spring-boot-starter-web 依赖。
主类
package com.sample.apigateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ApiGatewayApplication
public static void main(String[] args)
SpringApplication.run(ApiGatewayApplication.class, args);
application.yml
spring:
application:
name: GATEWAY-SERVICE
cloud:
gateway:
routes:
- id: USER-SERVICE
uri: lb://USER-SERVICE
predicates:
- Path=/users/**
- id: DEPARTMENT-SERVICE
uri: lb://DEPARTMENT-SERVICE
predicates:
- Path=/departments/**
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
hostname: localhost
server:
port: 9191
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sample.apigateway</groupId>
<artifactId>apigateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apigateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>$spring-cloud.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
完整的错误信息
2021-07-30 23:12:02.420 WARN 19032 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration$SpringMvcFoundOnClasspathConfiguration': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration$SpringMvcFoundOnClasspathConfiguration]: Constructor threw exception; nested exception is org.springframework.cloud.gateway.support.MvcFoundOnClasspathException
2021-07-30 23:12:02.423 INFO 19032 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2021-07-30 23:12:02.441 INFO 19032 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-07-30 23:12:02.454 ERROR 19032 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway.
Action:
Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency.
Process finished with exit code 1
【问题讨论】:
你能提供确切的错误信息或堆栈跟踪吗? 更新了问题兄弟。 Spring Cloud Gateway 是 Spring Cloud 团队在 Spring 响应式生态系统之上实现的 API Gateway。因此添加建议的属性 [spring.main.web-application-type=reactive]。 #Arun 我做了所有建议的事情,但错误没有解决.. 【参考方案1】:我遇到了类似的问题。 正如错误部分中所建议的那样,对 application.properties 文件进行以下更改对我来说效果很好。
spring.main.web-application-type=reactive
【讨论】:
这是正确的做法吗?这意味着我必须使我的应用程序适应 spring webflux【参考方案2】:请注意Spring Cloud Gateway 与 Spring MVC (spring-boot-starter-web
) 不兼容。这在section "How to include Spring Cloud Gateway in the official reference documentation" 中有概述:
Spring Cloud Gateway 基于 Spring Boot 2.x、Spring WebFlux 和 Project Reactor 构建。因此,当您使用 Spring Cloud Gateway 时,许多您熟悉的同步库(例如 Spring Data 和 Spring Security)和模式可能并不适用。
另外说明:
Spring Cloud Gateway 需要 Spring Boot 和 Spring Webflux 提供的 Netty 运行时。它不适用于传统的 Servlet 容器或构建为 WAR。
正如错误消息已经建议的那样,您需要删除对spring-boot-starter-web
的依赖。您可以使用以下命令列出所有直接和传递依赖项:
mvn dependency:tree
这将表明spring-cloud-starter-netflix-eureka-server
依赖于spring-boot-starter-web
。从 pom.xml
中删除以下行后,您的应用程序应该会启动:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
【讨论】:
Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or when built as a WAR.
。这意味着我们无法为网关构建 WAR 并部署在 tomcat 中?
不,这个不支持,另见docs.spring.io/spring-boot/docs/current/reference/html/…:“由于Spring WebFlux不严格依赖Servlet API,应用默认部署在嵌入式Reactor Netty服务器上,War部署是不支持 WebFlux 应用程序。”
好,那我们如何在生产环境部署spring cloud gateway(Spring boot webflux)呢?
创建 JAR 存档而不是 WAR 存档并在目标环境中运行:java -jar archive.jar
但是在生产环境中推荐吗?我的意思是可扩展且稳定吗?我也无法弄清楚我们如何为网关设置上下文路径。尝试设置spring.webflux.base-path
,但没有成功。以上是关于春天云网关;在类路径上发现 Spring MVC,与 Spring Cloud Gateway 不兼容问题的主要内容,如果未能解决你的问题,请参考以下文章
在类路径资源中定义名称为“entityManagerFactory”的 bean 创建错误
Jboss 在类路径上没有检测到 Spring WebApplicationInitializer 类型
在类路径上未检测到 Spring WebApplicationInitializer 类型