Spring Cloud Alibaba学习笔记:6、Nacos - 配置中心

谁借莪1个温暖的怀抱¢ 2021-12-13 14:37 357阅读 0赞

文章目录

      • 分布式配置中心
      • Spring Cloud Alibaba Nacos Config
      • 创建配置文件
      • 创建Nacos Config 客户端
        • 1、 创建一个maven module 项目 `sca-nacos-config-client`
        • 2、 pom
        • 3、创建应用主类 `ScaNacosConfigApplication`,并实现一个HTTP接口
        • 4、 `bootstrap.yml` 配置端口,配置服务名称和Nacos地址
        • 5、启动应用程序
        • 6、测试验证配置获取和验证动态刷新

Nacos除了实现了服务的注册发现之外,还将配置中心功能整合在了一起,通过Nacos的配置管理功能,我们可以将整个架构体系内的所有配置都集中在Nacos中存储

分布式配置中心

在分布式系统中,由于服务数量巨多,为了实现更灵活的管理权限、安全性,实时更新以及一次打包,多处运行,所以需要分布式配置中心组件。

Spring Cloud Alibaba Nacos Config

Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持。使用 Spring Cloud Alibaba Nacos Config,您可以在 Nacos Server 集中管理你 Spring Cloud 应用的外部属性配置。

Spring Cloud Alibaba Nacos Config 是 Config Server 和 Client 的替代方案,客户端和服务器上的概念与 Spring Environment 和 PropertySource 有着一致的抽象,在特殊的 bootstrap 阶段,配置被加载到 Spring 环境中。当应用程序通过部署管道从开发到测试再到生产时,您可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容。

创建配置文件

第一步: 进入Nacos的控制页面,在配置列表功能页面中,点击右上角的“+”按钮
在这里插入图片描述
第二步:进入“新建配置”页面,如下图填写内容:
在这里插入图片描述

注意:Data ID 的默认扩展名为 .properties ,希望使用 YAML 配置,此处必须指明是 .yaml

第三步: 发布,发布成功后在 “配置列表” 一栏即可看到刚才创建的配置项
在这里插入图片描述

创建Nacos Config 客户端

1、 创建一个maven module 项目 sca-nacos-config-client

2、 pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>cn.fxbin.learn</groupId>
  8. <artifactId>sca</artifactId>
  9. <version>1.0</version>
  10. </parent>
  11. <artifactId>sca-nacos-config-client</artifactId>
  12. <version>1.0</version>
  13. <name>sca-nacos-config-client</name>
  14. <description>Spring Cloud Alibaba Nacos Config Client Service Project</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  26. </dependency>
  27. </dependencies>
  28. <build>
  29. <plugins>
  30. <plugin>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-maven-plugin</artifactId>
  33. </plugin>
  34. </plugins>
  35. </build>
  36. </project>

3、创建应用主类 ScaNacosConfigApplication,并实现一个HTTP接口

  1. package cn.fxbin.learn.sca.nacos.provider;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RestController;
  10. @SpringBootApplication
  11. public class ScaNacosConfigApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(ScaNacosConfigApplication.class, args);
  14. }
  15. @RefreshScope
  16. @RestController
  17. public class ConfigController {
  18. @Value("${config.test}")
  19. private String configTest;
  20. @GetMapping("/config")
  21. public String configTest() {
  22. return configTest;
  23. }
  24. }
  25. }

这里要敲黑板,划重点 哈哈,@RefreshScope,主要用来让这个类下的配置内容支持动态刷新,也就是当我们的应用启动之后,修改了Nacos中的配置内容之后,这里也会马上生效。

注意:你可以使用 spring.cloud.nacos.config.refresh.enabled=false 来关闭动态刷新

4、 bootstrap.yml 配置端口,配置服务名称和Nacos地址

  1. server:
  2. port: 9034
  3. spring:
  4. application:
  5. # 这里的应用名对应 Nacos Config 中的 Data ID,实际应用名称以配置中心的配置为准
  6. name: sca-nacos-config-client
  7. cloud:
  8. nacos:
  9. config:
  10. # 指定查找名为 sca-nacos-config-client.yaml 的配置文件
  11. file-extension: yaml
  12. # Nacos Server 的地址
  13. server-addr: 127.0.0.1:8848

注意:Spring Boot 配置文件的加载顺序,依次为 bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml ,其中 bootstrap.properties 配置为最高优先级

5、启动应用程序

启动应用后我们可以通过日志看到,已经成功加载到了配置文件
在这里插入图片描述

6、测试验证配置获取和验证动态刷新

配置获取

通过postman 请求 http://localhost:9034/config API
在这里插入图片描述

配置动态刷新

在 Nacos 控制台 修改 config.test
在这里插入图片描述
配置修改发布后,应用程序打印如下日志
在这里插入图片描述
通过postman 请求 http://localhost:9034/config API, 如下:
在这里插入图片描述

参考资料:
https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-config


end
如有问题,请留言或发送邮件,感谢阅读

发表评论

表情:
评论列表 (有 0 条评论,357人围观)

还没有评论,来说两句吧...

相关阅读