可能导致问题的原因:
1.nacos中的配置文件名不规范,官网有命名规则:“前缀”-“激活的环境配置”."文件后缀”
例如:nacosdemo-dev.yaml

nacos中的配置使用@Value注解获取不到值的原因及解决方案

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格式。

nacos中的配置使用@Value注解获取不到值的原因及解决方案

nacos中的配置使用@Value注解获取不到值的原因及解决方案

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>

原文地址:https://blog.csdn.net/qq_42425515/article/details/129746707

相关文章:

  • 2021-07-13
  • 2021-06-26
  • 2021-04-29
  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
  • 2021-07-01
  • 2022-02-27
猜你喜欢
  • 2023-03-31
  • 2021-06-03
  • 2021-05-12
  • 2021-07-14
  • 2021-07-08
相关资源
相似解决方案