本篇文章记录个人排查项目问题的思路以及后续的思考。
项目背景:
spring-boot-starter 3.x版本
spring-cloud-starter-alibaba-nacos-config 2021版本
项目启动出现服务bean初始化找不到nacos的配置,通过debug排查到是没有PropertySourceBootstrapConfiguration初始化流程导致nacos没有加载到配置中。
因此个人仔细阅读了springboot启动流程
createApplicationContext->prepareContext ->refreshContext ->afterContext
其中prepareContext中applyInitializer中 初始化PropertySourceBootstrapConfiguration
其中的propertySourceLocator目前的实现的是是通过NacosConfigBootstrapConfiguration初始化
NacosPropertySourceLocator.locate进行加载流程。
其中PropertySourceBootstrapConfiguration加载的依赖条件是需要BootstrapApplicationListener加载,而BootstrapApplicationListener目前需要两种的加载方式
1.spring.cloud.bootstrap.enabled=true
2.org.springframework.cloud.bootstrap.marker.class存在
因此需要加载nacos配置可以通过两个方案解决:
1.启动项:spring.cloud.bootstrap.enabled=true
2.引入spring-cloud-starter-bootstrap依赖
目前通过方案2平稳解决