可能导致问题的原因:
1.nacos中的配置文件名不规范,官网有命名规则:“前缀”-“激活的环境配置”."文件后缀”,
例如:nacosdemo-dev.yaml。
2.配置文件的后缀名写错了,后缀名有两种,yaml和properties,不能写成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>