版本信息:
<boot.version>2.6.3</boot.version>
<cloud.version>2021.0.5</cloud.version>
<cloud.alibaba.version>2021.0.5.0</cloud.alibaba.version>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
此版本移除了bootstrap.yml依赖。(本作者深感此版本配置方式简单明了)
移除了ext、share等配置
使用application.yml 及spring.config.import方式
spring:
application:
name: foo
cloud:
nacos:
discovery:
group: foo
namespace: dev
server-addr: yourIpAddress:8848
config:
namespace: dev
file-extension: yaml
config:
import:
# - optional:nacos:foo?group=foo&refresh=true
- nacos:foo?group=foo&refresh=true
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
logging:
level:
com.alibaba.cloud.nacos.configdata: debug
如此即可完成配置
在import中简单清晰的配置要用的dataID文件及所属项目分类、文件刷新属性。不必去猜是不是文件又配置名异常等麻烦小问题了。
但是!重要的是在nacos中输入配置文件时经常出现 明明写了属性却找不到或项目获取不到的问题。
两方面:
1、程序中是否有类似实现CommandLineRunner或ApplicationRunner等的实现。没错它会卡你的配置,自己研究自己代码如何转变实现吧。
2、DEBUG SpringCloudNacos的类NacosConfigDataLoader在这个方法中你可以清晰看到你所配置的文件属性。检查一下是否与你的预期一致。
完整的demo:
maven
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<boot.version>2.6.3</boot.version>
<cloud.version>2021.0.5</cloud.version>
<cloud.alibaba.version>2021.0.5.0</cloud.alibaba.version>
</properties>
<dependencies>
#springboot依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
# nacos依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
application.yml
spring:
application:
name: foo
cloud:
nacos:
discovery:
server-addr: yourAdderIP:8848
# namespace与命名空间的ID对应一致
namespace: changeToYourNamespaceId
config:
# namespace与命名空间的ID对应一致
namespace: changeToYourNamespaceId
server-addr: yourAdderIP:8848
config:
import:
# foo.yml与nacos中dataId一致
- nacos:foo.yml
#打开nacos日志 设置级别debug 建议初学者打开
logging:
level:
com.alibaba.cloud.nacos.configdata: debug
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
以上,可以帮助你解决99.99999%的NACOS问题。如有帮助 或意见欢迎反馈。