Nacos笔记(六):Nacos应用 - 注册中心、配置中心
Nacos官网:https://nacos.io/zh-cn/index.html。
Nacos注册中心/配置中心搭建官方文档地址:https://nacos.io/zh-cn/docs/v2/ecology/use-nacos-with-spring-cloud.html。
1、注册中心
Nacos注册中心原理图:
1.1、项目搭建
创建新项目,项目结构如下:
父项目下有两个子项目nacos-9001、nacos-9002。
1、POM依赖
父项目POM文件
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.3.0.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent> 7 <dependencies> 8 <dependency> 9 <groupId>org.springframework.boot</groupId> 10 <artifactId>spring-boot-starter-web</artifactId> 11 </dependency> 12 <dependency> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-tomcat</artifactId> 15 <scope>provided</scope> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-starter-test</artifactId> 20 <scope>test</scope> 21 </dependency> 22 </dependencies> 23 <dependencyManagement> 24 <dependencies> 25 <dependency> 26 <groupId>com.alibaba.cloud</groupId> 27 <artifactId>spring-cloud-alibaba-dependencies</artifactId> 28 <version>2.2.5.RELEASE</version> 29 <type>pom</type> 30 <scope>import</scope> 31 </dependency> 32 </dependencies> 33 </dependencyManagement>
子项目POM文件
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>com.alibaba.cloud</groupId> 8 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> 9 </dependency> 10 </dependencies>
2、配置文件application.yml
nacos-9001、nacos-9002 配置文件基本一样,只不过端口与服务名称不一致。
2.1、nacos-9001
1 # 服务端口 2 server: 3 port: 9001 4 spring: 5 # 应用名称 6 application: 7 name: nacos-9001 8 # 注册中心地址 9 cloud: 10 nacos: 11 discovery: 12 server-addr: 192.168.33.55:8848
2.2、nacos-9002
1 # 服务端口 2 server: 3 port: 9001 4 spring: 5 # 应用名称 6 application: 7 name: nacos-9001 8 # 注册中心地址 9 cloud: 10 nacos: 11 discovery: 12 server-addr: 192.168.33.55:8848
3、启动类
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 4 5 @SpringBootApplication 6 // 开启注册功能 7 @EnableDiscoveryClient 8 public class Nacos9001Application { 9 public static void main(String[] args) { 10 SpringApplication.run(Nacos9001Application.class, args); 11 } 12 }
nacos-9002的启动类与nacos-9001的启动类似。
1.2、注册中心
启动nacos-9001、nacos-9002,访问注册中心的服务列表,详情如下:
nacos-9001、nacos-9002,已经注册到Nacos上了。
2、配置中心
2.1、POM依赖
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 <!-- 注冊中心依赖 --> 7 <dependency> 8 <groupId>com.alibaba.cloud</groupId> 9 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> 10 <version>2021.1</version> 11 </dependency> 12 <!-- 配置中心依赖 --> 13 <dependency> 14 <groupId>com.alibaba.cloud</groupId> 15 <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> 16 <version>2021.1</version> 17 </dependency> 18 </dependencies>
2.2、配置文件
配置文件需要两个,因为在项目初始化时,优先向nacos配置中心拉取配置,拉取到配置后,项目才能正常启动。
SpringBoot配置文件的加载存在优先级,bootstrap的优先级比application的高,配置中心、注册中心相关信息设置在bootstrap配置中。
bootstrap.yml:保证服务注册,并读取指定后缀的配置文件信息。
application.yml:设置环境属性等项目依赖信息。
1、bootstrap.yml
1 # nacos配置 2 server: 3 port: 9003 4 5 spring: 6 application: 7 name: nacos-config-9003 8 cloud: 9 nacos: 10 # Nacos服务注册中心 11 discovery: 12 server-addr: 192.168.33.55:8848 13 # Nacos作为配置中心 14 config: 15 server-addr: 192.168.33.55:8848 16 #指定yaml格式的配置 17 file-extension: yaml
2、application.yml
# 开发环境
spring:
profiles:
active: dev
2.3、添加配置
项目在启动之后,如何知道获取哪一个配置?答案救灾Data ID的设置,在Nacos中通过特定的规则生成Data ID,保证项目拉取正确的配置信息。
1、Data ID规则
Data ID 规则如下:
${prefix}-${spring.profiles.active}.${file-extension}
prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
spring.profiles.active 即为当前环境对应的 profile,当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。
2.4、测试代码
1 import org.springframework.beans.factory.annotation.Value; 2 import org.springframework.web.bind.annotation.RequestMapping; 3 import org.springframework.web.bind.annotation.RestController; 4 5 @RestController 6 public class ConfigController { 7 8 @Value("${config.switch}") 9 private String configSwitch; 10 11 @RequestMapping("/getConfigSwitch") 12 public String getConfigSwitch() { 13 return "配置的服务开关:" + configSwitch; 14 } 15 }
执行结果如下:
2.5、配置自动刷新
Nacos通过 Spring Cloud 原生注解 @RefreshScope 实现配置自动更新:
1 @RestController 2 @RefreshScope 3 public class ConfigController { 4 5 @Value("${config.switch}") 6 private String configSwitch; 7 8 @RequestMapping("/getConfigSwitch") 9 public String getConfigSwitch() { 10 return "配置的服务开关:" + configSwitch; 11 } 12 }
在不重启服务的前提下,修改配置将开关由 Y 变更成 N。
修改配置文件信息:
再次访问开关配置
![0](https://img2023.cnblogs.com/blog/1680081/202308/1680081-20230826165058934-2115471727.png)