一、場景
1、將SpringBoot項(xiàng)目升級(jí)為SpringCloud
2、SpringBoot版本從2.1.3.RELEASE
升級(jí)為2.7.8
3、不使用Nacos配置中心,而是加載本地配置文件時(shí),程序啟動(dòng)正常
4、使用Nacos配置中心,加載到了錯(cuò)誤的配置文件,導(dǎo)致程序啟動(dòng)失敗
二、關(guān)鍵依賴
<!--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>
三、報(bào)錯(cuò)信息
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 #服務(wù)端口號(hào)
spring:
## 服務(wù)名稱,默認(rèn)項(xiàng)目名稱
application:
name: focallmedia-online-admin-cloud
profiles:
# 這里已將Nacos管理的配置文件加進(jìn)來了
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 #服務(wù)注冊(cè)中心地址
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、調(diào)整配置文件DataId
3、重啟后程序依舊報(bào)錯(cuò),查看啟動(dòng)日志,發(fā)現(xiàn)Nacos加載到了錯(cuò)誤的配置文件
日志如下
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'}]
– 從日志上看是加載配置文件時(shí),文件的前綴沒有使用當(dāng)前程序配置的spring.application.name
屬性
– 加載配置文件使用的前綴focallmedia-common
是當(dāng)前程序pom里邊依賴的某個(gè)Jar包
– 為什么會(huì)使用到Jar包配置的應(yīng)用名?為什么不使用bootstrap.yml
所配置的應(yīng)用名?Debug查看源碼試圖找到答案,由于水平太菜,沒有找到原因(歡迎大佬留言)
4、Debug查看源碼,企圖弄清楚使用錯(cuò)誤應(yīng)用名的原因
– 日志是PropertySourceBootstrapConfiguration
打印出來的,直接找到這個(gè)class進(jìn)行查看
– 發(fā)現(xiàn)加載了錯(cuò)誤的配置文件
– 往前查看配置來源
依照程序的執(zhí)行,最先需要注入配置的確實(shí)是focallmedia-common
這個(gè)依賴,但其他項(xiàng)目也依賴了這個(gè)Jar包,為什么啟動(dòng)是正常的?
5、找不到使用依賴程序應(yīng)用名的原因,發(fā)現(xiàn)ConfigurationProperties的屬性可以手動(dòng)進(jìn)行配置,進(jìn)行嘗試
在bootstrap.yml
指定配置文件前綴
至此,程序啟動(dòng)正常
五、原因
Nacos加載的配置文件沒有使用spring.application.name
所配置的應(yīng)用名,而是加載到了依賴Jar包的應(yīng)用名(具體原因不明,歡迎大佬留言)
六、解決
在bootstrap.yml
指定配置文件前綴
七、Nacos配置文件DataId配置規(guī)則
DataId的規(guī)則: $ {prefix}-spring.profile.active.{file-extension}
$ {prefix}: 前綴,默認(rèn)為spring.application.name
的值
$ {spring.profile.active} 當(dāng)前環(huán)境對(duì)應(yīng)的profile(多環(huán)境配置)
$ {file-extension} 配置內(nèi)容的數(shù)據(jù)格式(文件擴(kuò)展名)
配置時(shí)需要和bootstrap.yml中配置的file-extension屬性保持一致,否則不能正常讀取
八、備注
該問題在其他springcloud項(xiàng)目上沒有出現(xiàn),就算沒有在bootstrap.yml
配置的spring.cloud.nacos.config.prefix
屬性,依舊可以找到nacos配置中心正確的配置文件(該程序也依賴了focallmedia-common)文章來源:http://www.zghlxwxcb.cn/news/detail-764037.html
具體原因不確定文章來源地址http://www.zghlxwxcb.cn/news/detail-764037.html
到了這里,關(guān)于使用nacos配置中心管理配置文件時(shí),springcloud程序啟動(dòng)報(bào)錯(cuò),無法找到對(duì)應(yīng)的配置文件(加載到了錯(cuò)誤的配置文件)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!