Spring Boot 3中多应用共享配置引发的组件循环依赖问题求助
升级Spring Boot v3后遇到的循环依赖问题及解决思路
我有多个共享相同配置类的应用,升级到Spring Boot v3后出现了大量循环依赖错误。大部分问题已经通过重构代码修复,但所有应用都卡在同一条循环依赖链上:
webHandler defined in class path resource [org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration$EnableWebFluxConfiguration.class] ↓ healthEndpointWebFluxHandlerMapping defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointReactiveWebExtensionConfiguration$WebFluxAdditionalHealthEndpointPathsConfiguration.class] ┌─────┐ | healthEndpoint defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class] ↑ ↓ | healthContributorRegistry defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class] ↑ ↓ | mongoHealthContributor defined in class path resource [org/springframework/boot/actuate/autoconfigure/data/mongo/MongoReactiveHealthContributorAutoConfiguration.class] ↑ ↓ | reactiveMongoTemplate defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.class] ↑ ↓ | mappingMongoConverter defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.class] ↑ ↓ | mongoMappingContext defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.class] ↑ ↓ | consulAutoServiceRegistrationListener defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class] ↑ ↓ | consulAutoServiceRegistration defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class] ↑ ↓ | consulServiceRegistry defined in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulServiceRegistryAutoConfiguration.class] ↑ ↓ | ttlScheduler defined in class path resource [org/springframework/cloud/consul/support/ConsulHeartbeatAutoConfiguration.class] ↑ ↓ | actuatorHealthStatusProvider defined in class path resource [org/springframework/cloud/consul/support/ConsulHeartbeatAutoConfiguration$ActuatorBasedApplicationStatusProviderConfig.class] └─────┘
根本原因分析
这条循环链的核心是Actuator健康检查、MongoDB Reactive组件和Consul服务注册/心跳三者形成的闭环:
- Actuator的
healthEndpoint依赖mongoHealthContributor,后者需要reactiveMongoTemplate,进而依赖mongoMappingContext mongoMappingContext初始化时触发了consulAutoServiceRegistrationListener,导致Consul服务注册组件被提前初始化- Consul心跳组件
ttlScheduler依赖actuatorHealthStatusProvider,而它又反向依赖healthEndpoint,最终形成循环
解决方法
切断Consul心跳与Actuator的依赖
在配置文件中关闭基于Actuator的健康状态提供者,改用默认的简单状态实现:spring.cloud.consul.heartbeat.actuator-health=false延迟MongoMappingContext初始化
自定义配置类继承MongoDataConfiguration,将mongoMappingContext设置为懒加载,避免启动阶段过早触发Consul注册逻辑:@Configuration public class LazyMongoMappingContextConfig extends MongoDataConfiguration { @Override @Lazy public MongoMappingContext mongoMappingContext() throws ClassNotFoundException { return super.mongoMappingContext(); } }排除相关自动配置类
如果不需要Consul心跳与Actuator的集成,可以直接排除对应的自动配置类:@SpringBootApplication(exclude = {ConsulHeartbeatAutoConfiguration.ActuatorBasedApplicationStatusProviderConfig.class}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
内容的提问来源于stack exchange,提问作者r.m.ghimis
相关产品推荐
相关产品推荐

