环境版本:
2.6.13 2021.0.8 2.2.9.RELEASE
bootstrap.yaml配置:
spring: application: name: order cloud: nacos: config: name: order.yml group: DEV_GROUP server-addr: 127.0.0.1:8848 # file-extension: yaml namespace: c822f776-306c-4dd2-9612-68b697e3b240 username: nacos password: nacos
报错信息:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'test' in value "${test}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties问题解决
(PropertySourcesPlaceholderConfigurer.java:191) ~[spring-context-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1332) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:656) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.23.jar:5.3.23] ... 32 common frames omitted
在nacos页面配置正确的情况下,无论如何都无法获取到配置的参数。
通过查看spring cloud alibaba的官网,发现2021.0.8版本的spring cloud不适合使用2.2.9.RELEASE版本的spring-cloud-alibaba,需要改成如下配置
Spring Cloud Version | Spring Boot Version | |
---|---|---|
2021.0.5.0* | Spring Cloud 2021.0.5 | 2.6.13 |
2021.0.4.0 | Spring Cloud 2021.0.4 | 2.6.11 |
2021.0.1.0 | Spring Cloud 2021.0.1 | 2.6.3 |
2021.1 | Spring Cloud 2020.0.1 | 2.4.2 |
所以将依赖版本改为如下配置
2.6.13 2021.0.5 2021.0.5.0
重启后发现依然有问题,报错如下
spring: application: name: order cloud: nacos: config: group: DEV_GROUP server-addr: 127.0.0.1:8848 namespace: c822f776-306c-4dd2-9612-68b697e3b240 username: nacos password: nacos discovery: server-addr: 127.0.0.1:8848 config: import: - optional:nacos:order.yml - optional:nacos:comm.yml?group=DEFAULT_GROUP&refreshEnabled=false
查看SpringCloud Alibaba 2021版 nacos 配置中心教程得知,在该版本下原有的nacos配置文件获取规则不再生效需要改为spring.config.import的方式来配置,此种配置方式也支持多个配置文件的获取,配置参考如下
重启后程序正常运行。
# bootstrap.yml spring: cloud: nacos: config: name: test.yml group: DEFAULT_GROUP server-addr: 127.0.0.1:8848 extension-configs: - dataId: test01.yml group: group_01 - dataId: test02.yml group: group_02 refresh: false
原有的bootstrap.yml配置文件不再生效,需要修改为application.yml的配置
# application.yml spring: cloud: nacos: config: group: DEFAULT_GROUP server-addr: 127.0.0.1:8848 config: import: - optional:nacos:test.yml # 监听 DEFAULT_GROUP:test.yml - optional:nacos:test01.yml?group=group_01 # 覆盖默认 group,监听 group_01:test01.yml - optional:nacos:test02.yml?group=group_02&refreshEnabled=false # 不开启动态刷新 - nacos:test03.yml # 在拉取nacos配置异常时会快速失败,会导致 spring 容器启动失败
需要改为
注意: