栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

SpringCloud集成Nacos Config配置中心无法读取配置Could not resolve placeholder ‘config.info‘ in value

Java 更新时间:发布时间: 百科书网 趣学号

环境:

本地nacos服务已启动,可以正常登录web管理界面

pom引入也OK

配置文件

bootstrap.yml

application.yml 

 代码

@RestController
@Slf4j
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }
}

 nacos

添加dataip 命名规范为 

${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}的配置文件nacos-config-client-dev.yml

问题现象:

2021-10-05 14:23:24.425  WARN 20700 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.configClientController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'config.info' in value "${config.info}"

启动后报错,无法获取到controller中通过@value注入的配置config.info

解决问题:

考虑到可能是代码和配置都没什么问题,把思路放在nacos配置中,重复多次的修改配置文件中的格式问题,包括再三的检查dataid的命名规范,最终

spring.cloud.config.file-extension:yaml

问题出在这里。

刚开始的dataid不包含后缀,文件名为:nacos-config-client-dev

修改后为:nacos-config-client-dev.yaml

期间修改过为后缀yml,无效

所以最终总结:

如果file-extension配置为yaml,那么在nacos新建配置文件时格式选择各yaml,dataid后缀也要带上,一定要和项目配置文件中的保持一致。

至于为什么别人的nacos中配置后缀为yml 而项目配置中为yaml为什么就可以正常获取配置,我就不得而知了。

以上就是问题描述和解决过程,希望对正在尝试学习nacos的朋友有所帮助。算是一个小坑吧
 

更新:

尝试了修改file-extension的配置,改为yml,在nacos中添加配置文件,dataid的后缀为yml,nacos配置格式为yaml 也是可以读取到配置的,所以说file-extension和dataid的后缀要保持一致。

如果以上还不能解决你的问题,尝试检查一下项目配置文件里的group和namespace也要与nacos里的配置保持一致。这里就不再做拓展了。

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/294331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号