这里写目录标题
- 一、场景
- 二、关键依赖
- 三、报错信息
- 四、排查
- 1、`bootstrap.yml`配置
- 2、查看Nacos配置中心
- 3、重启后程序依旧报错,查看启动日志,发现Nacos加载到了错误的配置文件
- 4、Debug查看源码,企图弄清楚使用错误应用名的原因
- 5、找不到使用依赖程序应用名的原因,发现`ConfigurationProperties`的属性可以手动进行配置,进行尝试
- 五、原因
- 六、解决
- 七、Nacos配置文件DataId配置规则
- 八、备注
一、场景
1、将SpringBoot项目升级为SpringCloud
2、SpringBoot版本从2.1.3.RELEASE
升级为2.7.8
3、不使用Nacos配置中心,而是加载本地配置文件时,程序启动正常
4、使用Nacos配置中心,加载到了错误的配置文件,导致程序启动失败
二、关键依赖
<!--nacos-config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.0.5.0</version>
</dependency>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.0.5.0</version>
</dependency>
<!-- spring-cloud-starter-bootstrap -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>2.7.8</version>
</dependency>
三、报错信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shardingDataSourceConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'sharding.jdbc.datasource.driver' in value "${sharding.jdbc.datasource.driver}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
at com.xxx.OnlineApp.main(OnlineApp.java:33)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'sharding.jdbc.datasource.driver' in value "${sharding.jdbc.datasource.driver}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:191)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1332)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
... 17 common frames omitted
四、排查
1、bootstrap.yml
配置
server:
port: 8877 #服务端口号
spring:
## 服务名称,默认项目名称
application:
name: focallmedia-online-admin-cloud
profiles:
# 这里已将Nacos管理的配置文件加进来了
include: config,rocketmq,scheduler,shardingDataSource
main:
allow-circular-references: true
mvc:
pathmatch:
matching-strategy: ant_path_matcher
cloud:
nacos:
discovery:
server-addr: 192.168.1.231:8848 #服务注册中心地址
namespace: focallmedia
group: dev
config:
server-addr: 192.168.1.231:8848 #配置中心地址
file-extension: properties #指定properties格式的配置
namespace: focallmedia
group: dev
2、查看Nacos配置中心
2.1、group
和namespace
都没有问题,但配置文件的DataId没有添加文件后缀
2.2、调整配置文件DataId
3、重启后程序依旧报错,查看启动日志,发现Nacos加载到了错误的配置文件
日志如下
2023-11-16 10:11:42.892 - [WARN ] - [main] - [c.a.cloud.nacos.client.NacosPropertySourceBuilder - line:87]: Ignore the empty nacos configuration and get it based on dataId[focallmedia-common] & group[dev]
2023-11-16 10:11:42.908 - [WARN ] - [main] - [c.a.cloud.nacos.client.NacosPropertySourceBuilder - line:87]: Ignore the empty nacos configuration and get it based on dataId[focallmedia-common.properties] & group[dev]
2023-11-16 10:11:42.908 - [WARN ] - [main] - [c.a.cloud.nacos.client.NacosPropertySourceBuilder - line:87]: Ignore the empty nacos configuration and get it based on dataId[focallmedia-common-config.properties] & group[dev]
2023-11-16 10:11:42.908 - [WARN ] - [main] - [c.a.cloud.nacos.client.NacosPropertySourceBuilder - line:87]: Ignore the empty nacos configuration and get it based on dataId[focallmedia-common-rocketmq.properties] & group[dev]
2023-11-16 10:11:42.908 - [WARN ] - [main] - [c.a.cloud.nacos.client.NacosPropertySourceBuilder - line:87]: Ignore the empty nacos configuration and get it based on dataId[focallmedia-common-scheduler.properties] & group[dev]
2023-11-16 10:11:42.924 - [WARN ] - [main] - [c.a.cloud.nacos.client.NacosPropertySourceBuilder - line:87]: Ignore the empty nacos configuration and get it based on dataId[focallmedia-common-shardingDataSource.properties] & group[dev]
2023-11-16 10:13:16.856 - [INFO ] - [main] - [o.s.c.b.c.PropertySourceBootstrapConfiguration - line:109]: Located property source: [BootstrapPropertySource {name='bootstrapProperties-focallmedia-common-shardingDataSource.properties,dev'}, BootstrapPropertySource {name='bootstrapProperties-focallmedia-common-scheduler.properties,dev'}, BootstrapPropertySource {name='bootstrapProperties-focallmedia-common-rocketmq.properties,dev'}, BootstrapPropertySource {name='bootstrapProperties-focallmedia-common-config.properties,dev'}, BootstrapPropertySource {name='bootstrapProperties-focallmedia-common.properties,dev'}, BootstrapPropertySource {name='bootstrapProperties-focallmedia-common,dev'}]
– 从日志上看是加载配置文件时,文件的前缀没有使用当前程序配置的spring.application.name
属性
– 加载配置文件使用的前缀focallmedia-common
是当前程序pom里边依赖的某个Jar包
– 为什么会使用到Jar包配置的应用名?为什么不使用bootstrap.yml
所配置的应用名?Debug查看源码试图找到答案,由于水平太菜,没有找到原因(欢迎大佬留言)
4、Debug查看源码,企图弄清楚使用错误应用名的原因
– 日志是PropertySourceBootstrapConfiguration
打印出来的,直接找到这个class进行查看
– 发现加载了错误的配置文件
– 往前查看配置来源
依照程序的执行,最先需要注入配置的确实是focallmedia-common
这个依赖,但其他项目也依赖了这个Jar包,为什么启动是正常的?
5、找不到使用依赖程序应用名的原因,发现ConfigurationProperties
的属性可以手动进行配置,进行尝试
在bootstrap.yml
指定配置文件前缀
至此,程序启动正常
五、原因
Nacos加载的配置文件没有使用spring.application.name
所配置的应用名,而是加载到了依赖Jar包的应用名(具体原因不明,欢迎大佬留言)
六、解决
在bootstrap.yml
指定配置文件前缀
七、Nacos配置文件DataId配置规则
DataId的规则: $ {prefix}-spring.profile.active.{file-extension}
$ {prefix}: 前缀,默认为spring.application.name
的值
$ {spring.profile.active} 当前环境对应的profile(多环境配置)
$ {file-extension} 配置内容的数据格式(文件扩展名)
配置时需要和bootstrap.yml中配置的file-extension属性保持一致,否则不能正常读取
八、备注
该问题在其他springcloud项目上没有出现,就算没有在bootstrap.yml
配置的spring.cloud.nacos.config.prefix
属性,依旧可以找到nacos配置中心正确的配置文件(该程序也依赖了focallmedia-common)
具体原因不确定