nacos中的配置使用@Value注解获取不到值

可能导致问题的原因:
1.nacos中的配置文件名不规范,官网有命名规则:“前缀”-“激活的环境配置”."文件后缀”
例如:nacosdemo-dev.yaml
在这里插入图片描述
2.配置文件的后缀名写错了,后缀名有两种,yamlproperties,不能写成yml,项目中的配置文件中也要检查后缀名是否一致,如果用yaml类型的配置,需要在bootstrap.properties文件中添加这行配置来指定类型**“spring.cloud.nacos.config.file-extension=yaml**”
官网地址https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-config
3.如果要使用nacos做为外部配置,就需要将连接nacos的配置写在bootstrap.properties或者bootstrap.yaml文件中,application.properties文件可以删除掉,或者application.properties中只配置server.port,注意:spring.application.name要和nacos中的配置文件名一致,不需要加后缀,如果是nacos中是yaml格式的配置加上spring.cloud.nacos.config.file-extension=yaml,不加默认是properties格式。
在这里插入图片描述
在这里插入图片描述
4.可能识别不到bootstrap.properties配置文件,加入以下依赖:

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

完整依赖如下:

 <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>2021.0.3</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.2.9.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  
   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值