微服务和nacos版本都在2.x及之后。
1、discovery用于服务注册,将想要注册的服务注册到nacos中,被naocs发现。
pom引入的依赖是:

yml配置文件中:

2、config用于获取nacos配置管理->配置列表下配置文件中的内容
pom引入的依赖是:

获取nacos的配置有三种方式
1>、通过nacos的内部规则
yml配置如下:

在 Nacos Spring Cloud 中,dataId 的完整格式如下:
${prefix}-${spring.profile.active}.${file-extension}
prefix:默认是spring.application.name的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
spring.profile.active对应的值。
file-extension是文件后缀名。
然后我们就在nacos新建了个cloudalibaba-provider-payment-9001-dev.yaml配置文件

配置变更需要刷新的话可以在类上加上@RefreshScope注解,不需要再重启服务,可读取到最新的。
配置文件中第二个参数,spring.profile.active在springboot中原本就是启用指定的环境,这里整合在一起了,不需要指定namespace。
2>、多Data-Id的使用
yml配置如下:

spring.cloud.nacos.config.extension-configs是个list,可以配置多个,dataId时配置文件名称,refresh=true变更支持刷新。
3>、配置共享
yml配置如下:

spring.cloud.nacos.config.shared-configs是个list,可以配置多个,dataId时配置文件名称,refresh=true变更支持刷新。