spring - Spring Cloud Gateway 服务器启动时出现问题?
问题描述
我有一个简单的 Spring Cloud 微服务系统。我在应用程序中有一个 Cloud Gateway 服务器。它向 Eureka 注册,获取 Config Server 的地址并为自己下载配置。
这个网关服务将是登录的地方,它也会找到正确的微服务,客户端真正想要调用的。它使用 Redis 进行会话处理和分发。
但是在启动过程中,我看到了这个失败:
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'sessionRepository', defined in class path resource [org/springframework/boot/autoconfigure/session/RedisReactiveSessionConfiguration$SpringBootRedisWebSessionConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
[WARNING]
java.lang.reflect.InvocationTargetException
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:566)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:558)
at java.lang.Thread.run (Thread.java:834)
Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'sessionRepository' defined in class path resource [org/springframework/boot/autoconfigure/session/RedisReactiveSessionConfiguration$SpringBootRedisWebSessionConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.session.RedisReactiveSessionConfiguration$SpringBootRedisWebSessionConfiguration; factoryMethodName=sessionRepository; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/session/RedisReactiveSessionConfiguration$SpringBootRedisWebSessionConfiguration.class]] for bean 'sessionRepository': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration; factoryMethodName=sessionRepository; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]] bound.
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition (DefaultListableBeanFactory.java:894)
这是网关应用程序的 pom.xml:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>elszamolas-gateway</artifactId>
<packaging>jar</packaging>
<name>elszamolas-gateway</name>
<description>TAO elszamolas GATEWAY service</description>
<parent>
<groupId>com.besztercekk.tao</groupId>
<artifactId>tao-elszamolas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<!-- Spring Cloud Gateway service -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Spring Cloud Eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Spring Cloud config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- Spring Boot reactive web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- Spring Boot share session via Redis -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</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>
</project>
这是它的父 pom.xml:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.besztercekk.tao</groupId>
<artifactId>tao-elszamolas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>elszamolas</name>
<description>TAO elszámolás szülő project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
<spring-session.version>1.3.4.RELEASE</spring-session.version>
<spring-session-core.version>2.1.2.RELEASE</spring-session-core.version>
</properties>
<modules>
<module>elszamolas-config</module>
<module>elszamolas-discovery</module>
<module>elszamolas-gateway</module>
</modules>
<dependencies>
<!-- Common Spring Boot Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Java 11 required dependencies -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
任何帮助将不胜感激。我真的不知道为什么这个“ sessionRepository ” bean 从多个地方多次加载。我是否混淆了必要的依赖项?
解决方案
更新:
我仔细研究了这个问题。异常很清楚 bean 被调用,它说 bean sessionRepository被定义了两次。如果您检查它们定义的类以及它们的来源,您会发现其中一个来自自动配置器,另一个来自 Redis 会话数据依赖项。
在这一点上,您似乎需要允许 bean 覆盖,您可以通过将其添加到bootstrap.yml轻松做到这一点:
spring:
main:
allow-bean-definition-overriding: true
但是,如果您执行其他几个例外情况,您将需要面对。这些家伙默认将此选项设置为false是有道理的。以下是您 cab 获得的示例异常:
2018-12-20 00:37:22.965 ERROR 3089 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisMessageListenerContainer' defined in class path resource org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.listener.RedisMessageListenerContainer]: Factory method 'redisMessageListenerContainer' threw exception; nested exception is java.lang.IllegalStateException: @Bean method RedisHttpSessionConfiguration.sessionRepository called as bean reference for type [org.springframework.session.data.redis.RedisOperationsSessionRepository] but overridden by non-compatible bean instance of type [org.springframework.session.data.redis.ReactiveRedisOperationsSessionRepository]. Overriding bean of same name declared in: class path resource [org/springframework/boot/autoconfigure/session/RedisReactiveSessionConfiguration$SpringBootRedisWebSessionConfiguration.class]
如果我错了,请纠正我。我真的很感激。但是,当您需要 Spring Boot 自动配置功能时,您就不能使用 Redis 进行会话存储和分发到微服务。它只是没有一起工作。有点难以置信,因为当您真的不想在每个微服务中验证您的用户时,您必须在 Spring Boot 2.1.1 应用程序中使用一些东西。
推荐阅读
- delphi - Delphi Sydney 调用FD CreateBlobStream 时在TStream 中报告内存泄漏
- reactjs - 带有关键字 this 的 JavaScript
- swift - 在视图中显示 API 数据
- php - Laravel 中的 Update() 似乎不起作用
- excel - 使用多个 MS 应用程序时如何更改 VBA 宏中的应用程序焦点
- deep-learning - 未找到运行 YOLO DARKNET 的 CUDNN
- kotlin - 为什么我不能将领域层中的类设置为具有清洁架构的抽象类?
- verilog - Verilog:三元运算符与算术右移一起导致意外行为
- javascript - 如果与普通 NodeJS 服务器中的给定模式不匹配,如何正确检查 HTTP 请求正文并拒绝它?
- dll - MFC dll 无模式对话框编辑控制键盘输入被主机应用程序阻止