nacos 2.0 版本在spring cloud 2022.0.0.0-RC2读取配置文件失败

2023-12-13 05:18:08

报错

报错信息如下

Description:
No spring.config.import property has been defined

Spring 官方给出的解决方案如下

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.

出现原因

首先,自2021.0.5版本的 Spring Cloud 默认不再启用 bootstrap 包,因此应该将配置文件写在 application.yml 中,或手动在 maven 中导入 bootstrap 包。

解决方法

由于新版nacos要求我们在spring config import下配置新的nacos规则,我们仅需按照以下配置即可,按照nacos之前的的版本格式 p r e f i x ? {prefix}- prefix?{spring.profiles.active}.${file-extension}

spring:
  config:
    import:
      - optional:nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}-`` `
# 其他
如果进行修改后,还是注入为空,请检查nacos和本地的文件名称是否一致,是否在@Value注解是否采用正确格式`@Value("${pattern.dateformat}")`.

文章来源:https://blog.csdn.net/studycodeday/article/details/134700170
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。