今天在配置运行Spring Cloud Gateway的时候出现了这个错误
Description:
Parameter 0 of method websocketRoutingFilter in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.web.reactive.socket.client.WebSocketClient' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'reactorNettyWebSocketClient' in 'GatewayAutoConfiguration.NettyConfiguration' not loaded because @ConditionalOnClass did not find required class 'reactor.ipc.netty.http.client.HttpClient'
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.web.reactive.socket.client.WebSocketClient' in your configuration.
这是maven所使用到的依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<name>gateway</name>
<description>Spring Cloud Gateway网关</description>
<properties>
<java.version>1.8</java.version>
<fastjson.version>1.2.8</fastjson.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
解决办法:排除报错的 WebSocketClient 相关的类
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.0.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</exclusion>
</exclusions>
</dependency>
排除后就能够正常运行了
总结:
这个错误的原因是在 Spring Cloud Gateway 的自动配置中,需要一个类型为 org.springframework.web.reactive.socket.client.WebSocketClient 的 Bean,但是系统没有找到符合条件的 Bean。
通过添加排除项 < exclusion >,排除了 spring-boot-starter-webflux 的依赖,这个依赖包含了 Spring WebFlux 框架,其中也包含了 WebSocketClient 相关的类。因此,排除了这个依赖后,系统就不再需要 WebSocketClient,相应的错误也消失了。
但是这么做会出现两个问题:
翻译:
类路径中缺少 Spring Webflux,而此时 Spring Cloud Gateway 需要该类路径。请添加 spring-boot-starter-webflux 依赖项。
在类路径上发现 Spring MVC,目前与 Spring Cloud Gateway 不兼容。请删除 spring-boot-starter-web 依赖项。
通过 mvn dependency:tree 指令查看依赖树结构
所以问题在于 spring-cloud-starter-netflix-eureka-server 包含了 spring-boot-starter-web 依赖,spring-cloud-starter-gateway 包含了 spring-boot-starter-webflux 依赖,spring-boot-starter-web 和 spring-boot-starter-webflux 存在冲突。
解决方案:将 spring-cloud-starter-netflix-eureka-server 依赖改为 spring-cloud-starter-netflix-eureka-client 依赖,因为 spring-cloud-starter-netflix-eureka-client 依赖中不包含 spring-boot-starter-web,修改 org.springframework.boot 依赖和 org.springframework.cloud 依赖的版本,确保其兼容。
修改后的 pom.xml 文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
网关 gateway 正常启动