今天在家玩玩阿里巴巴的nacos项目,我是将nacos整合到spring cloud 和 dubbo项目里面。
在nacos里面配置了dataId为项目spring.application.name并添加properties后缀之后,进行项目启动,发现启动失败。
我的nacos配置
启动后,会报错:
com.alibaba.nacos.api.exception.NacosException: nullat com.alibaba.nacos.api.config.ConfigFactory.createConfigService(ConfigFactory.java:45) ~[nacos-api-1.0.0.jar:na]at com.alibaba.nacos.api.NacosFactory.createConfigService(NacosFactory.java:41) ~[nacos-api-1.0.0.jar:na]at org.springframework.cloud.alibaba.nacos.NacosConfigProperties.configServiceInstance(NacosConfigProperties.java:343) ~[spring-cloud-alibaba-nacos-config-0.2.2.RELEASE.jar:0.2.2.RELEASE]at org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator.locate(NacosPropertySourceLocator.java:63) [spring-cloud-alibaba-nacos-config-0.2.2.RELEASE.jar:0.2.2.RELEASE]at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:94) [spring-cloud-context-2.0.1.RELEASE.jar:2.0.1.RELEASE]at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:654) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:390) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:331) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'hello.value' in value "${hello.value}"at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
出现这个问题,我一开先去nacos官网查了下,官网的最佳实践是怎么玩的,结果没有发现有什么问题。该配置的数据,我也配置上了。然后去各种博客里面查询,发现是因为springcloud和nacos config都是通过spring boot加载bootstrap.properties的时候进行上下文的初始化的。但是这时候还没有加载我们的application.properties,这就导致,config没加载完就去nacos server里面读取数据了。
解法:
将nacos的配置文件转移到bootstrap.properties或者boostrap.yml文件中。