问题描述
环境版本:
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$0(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页面配置正确的情况下,无论如何都无法获取到配置的参数。
问题解决
1.修改依赖版本
通过查看spring cloud alibaba的官网,发现2021.0.8版本的spring cloud不适合使用2.2.9.RELEASE版本的spring-cloud-alibaba,需要改成如下配置
Spring Cloud Alibaba Version | 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
重启后发现依然有问题,报错如下
org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set
at org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor.postProcessEnvironment(ConfigDataMissingEnvironmentPostProcessor.java:82)
at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:102)
at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:87)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
。
。
。
。
。
Description:
No spring.config.import property has been defined
Action:
Add a spring.config.import=nacos: property to your configuration.
If configuration is not required add spring.config.import=optional:nacos: instead.
To disable this check, set spring.cloud.nacos.config.import-check.enabled=false.
2.添加配置
查看SpringCloud Alibaba 2021版 nacos 配置中心教程得知,在该版本下原有的nacos配置文件获取规则不再生效需要改为spring.config.import的方式来配置,此种配置方式也支持多个配置文件的获取,配置参考如下
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
重启后程序正常运行。
spring cloud2021.0.5版本nacos配置中心的使用总结
原有的bootstrap.yml配置文件不再生效,需要修改为application.yml的配置
# 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
需要改为
# 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 容器启动失败
注意:
- 如果想保留以前的使用方式(bootstrap引入配置),需要添加依赖 pring-cloud-starter-bootstrap 依赖,不需要修改一行代码。
- refreshEnabled参数要搭配@RefreshScope使用,如果没有使用@RefreshScope注解,就算配置了refreshEnabled=true也不会自动刷新。
- refreshEnabled=false只是针对test02.yml这个配置文件在修改时不会自动刷新。如果test.yml中的参数和test02.yml所中的参数在同一个类中被使用且配置了@RefreshScope注解,那么在修改test02.yml后再修改test.yml会导致test02.yml被刷新。