Spring Cloud Alibaba Nacos 分布式配置中心

╰半橙微兮° 2022-12-29 04:48 209阅读 0赞

文章目录

      • 1 摘要
      • 2 核心 Maven 依赖
      • 3 核心代码
        • 3.1 bootstrap 配置文件
        • 3.2 application 配置文件
        • 3.3 配置测试类 - Controller 层
        • 3.4 Nacos 配置中心
      • 4 测试配置
        • 4.1 测试配置中心是否生效
        • 4.2 测试配置修改实时刷新
        • 4.3 多环境配置测试
      • 5 推荐参考资料
      • 6 Github 源码


1 摘要

Nacos 作为阿里开源的微服务分布式组件,除了作为服务注册中心之外,自身也提供分布式配置功能,而且还是自动刷新的。这比基于 Netflix Eureka 实现的 Config 配置使用起来要方便的多。本文将介绍基于 Spring Cloud Alibaba 2.2 集成分布式配置服务。

Spring Cloud Alibaba Github: https://github.com/alibaba/spring-cloud-alibaba

准备工作:

Alibaba Nacos 安装教程

2 核心 Maven 依赖

  1. ./cloud-alibaba-config/pom.xml
  2. <dependencies>
  3. <!-- cloud alibaba -->
  4. <dependency>
  5. <groupId>com.alibaba.cloud</groupId>
  6. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  7. <version>${spring-cloud-alibaba.version}</version>
  8. </dependency>
  9. <dependency>
  10. <groupId>com.alibaba.cloud</groupId>
  11. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  12. <version>${spring-cloud-alibaba.version}</version>
  13. </dependency>
  14. <!-- 省略其他依赖 -->
  15. </dependencies>
  16. <dependencyManagement>
  17. <dependencies>
  18. <dependency>
  19. <groupId>com.alibaba.cloud</groupId>
  20. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  21. <version>${spring-cloud-alibaba.version}</version>
  22. <type>pom</type>
  23. <scope>import</scope>
  24. </dependency>
  25. </dependencies>
  26. </dependencyManagement>

其中 ${spring-cloud-alibaba.version} 的版本为 2.2.3.RELEASE

注意事项: Spring Cloud Alibaba 2.2.3.RELEASE 版本支持的 Spring Boot 版本为 2.3.1.RELEASE ,建议在搭建项目时要保持版本的一致性,Spring Boot 版本过高或过低都可能导致不兼容问题

3 核心代码

3.1 bootstrap 配置文件

Spring Boot 加载配置文件的顺序: bootstrap.properties > bootstrap.yml > application.properties > application.yml

bootstrap.properties/yml 的配置信息不会被 application.properties/yml 中的配置所覆盖

这里若要使 Spring Cloud Alibaba Nacos 的 Config 配置生效,必须使用 bootstrap.properties 配置文件

  1. ./cloud-alibaba-config/src/main/resources/bootstrap.properties
  2. ## bootstrap config
  3. ## spring
  4. spring.profiles.active=test
  5. spring.application.name=cloud-alibaba-config
  6. spring.cloud.nacos.server-addr=172.16.140.10:8688
  7. spring.cloud.nacos.config.file-extension=yaml

配置简要说明:

spring.profiles.active : 指定当前的环境,Nacos Config 是支持多环境的,和 SpringBoot 的配置类似

spring.application.name: 应用名称,Nacos 服务注册中心的服务名称,Nacos Config 配置文件的文件名(不包含后缀)

spring.cloud.nacos.server-addr:Nacos 服务注册中心地址,包括 ip 和端口

spring.cloud.nacos.config.file-extension: Nacos Config 配置文件的文件后缀(常用 propertiess/yaml)

3.2 application 配置文件

  1. ./cloud-alibaba-config/src/main/resources/application.yml
  2. ## config
  3. ## server
  4. server:
  5. port: 8614

这里既用了 bootstrap 配置,又用了 application 配置,是因为 Nacos Config 的配置必须放在 bootstrap 中才会生效,application 配置中用于服务端口以及其他Spring 相关的配置项。

3.3 配置测试类 - Controller 层

  1. ./cloud-alibaba-config/src/main/java/com/ljq/demo/springboot/alibaba/config/controller/NacosConfigController.java
  2. package com.ljq.demo.springboot.alibaba.config.controller;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.cloud.context.config.annotation.RefreshScope;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. /** * @Description: Spring Cloud Alibaba Nacos 分布式配置中心控制层 * @Author: junqiang.lu * @Date: 2020/12/9 */
  12. @Slf4j
  13. @RestController
  14. @RefreshScope
  15. @RequestMapping("/api/cloud/alibaba/config")
  16. public class NacosConfigController {
  17. /** * 用户名 */
  18. @Value("${userInfo.name: default}")
  19. private String userName;
  20. /** * 用户年龄 */
  21. @Value("${userInfo.age: 0}")
  22. private Integer userAge;
  23. /** * 用户手机号 */
  24. @Value("${userInfo.phone: default}")
  25. private String userPhone;
  26. /** * 用户信息 * * @return */
  27. @GetMapping(value = "/user", produces = { MediaType.APPLICATION_JSON_VALUE})
  28. public ResponseEntity<String> queryUserInfo() {
  29. StringBuilder userBuilder = new StringBuilder();
  30. userBuilder.append("用户名:").append(userName)
  31. .append(",年龄:").append(userAge)
  32. .append(",手机号:").append(userPhone);
  33. log.info("@Value 用户信息: {}", userBuilder.toString());
  34. return ResponseEntity.ok(userBuilder.toString());
  35. }
  36. }

简要说明:

@Value :Spring 注解,用于读取配置信息

@RefreshScope: Spring 注解,表示配置更新后能够同步刷新,保证当前使用的配置是最新的。只有添加这个注解,当配置中心的信息修改时才能够同步到程序中。

为了测试 Nacos Config 的多环境配置,这里单独写了一个测试类

  1. ./cloud-alibaba-config/src/main/java/com/ljq/demo/springboot/alibaba/config/controller/NacosConfigProfileController.java
  2. package com.ljq.demo.springboot.alibaba.config.controller;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.cloud.context.config.annotation.RefreshScope;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. /** * @Description: Spring Cloud Alibaba Config 多环境测试控制层 * @Author: junqiang.lu * @Date: 2020/12/13 */
  12. @Slf4j
  13. @RestController
  14. @RefreshScope
  15. @RequestMapping("/api/cloud/alibaba/config")
  16. public class NacosConfigProfileController {
  17. /** * 当前环境 */
  18. @Value("${currentProfile: default}")
  19. private String currentProfile;
  20. /** * 当前环境 * * @return */
  21. @GetMapping(value = "/profile", produces = { MediaType.APPLICATION_JSON_VALUE})
  22. public ResponseEntity<String> queryUserInfo() {
  23. log.info("当前环境: {}", this.currentProfile);
  24. return ResponseEntity.ok(this.currentProfile);
  25. }
  26. }

3.4 Nacos 配置中心

打开 Nacos 控制台,点击「配置管理」下的「配置列表」项,在右侧界面添加配置。

Nacos 控制台配置列表界面

image-20201220103759186

Nacos 控制台添加配置界面

Nacos 控制台添加配置界面

这里作为测试示例,添加了三个配置,分别是:

配置一:

  1. Data ID: cloud-alibaba-config
  2. Group: DEFAULT_GROUP
  3. 配置格式: YAML

配置内容:

  1. ## endpoint
  2. management:
  3. endpoints:
  4. web:
  5. exposure:
  6. include: "*"
  7. ## user
  8. userInfo:
  9. name: xxx
  10. age: 0
  11. phone: xxx

配置二:

  1. Data ID: cloud-alibaba-config-dev
  2. Group: DEFAULT_GROUP
  3. 配置格式: YAML

配置内容:

  1. ## endpoint
  2. management:
  3. endpoints:
  4. web:
  5. exposure:
  6. include: "*"
  7. ## user
  8. userInfo:
  9. name: 张三疯
  10. age: 36
  11. phone: 13123453333
  12. currentProfile: dev

配置三:

  1. Data ID: cloud-alibaba-config-test.yaml
  2. Group: DEFAULT_GROUP
  3. 配置格式: YAML

配置内容:

  1. ## endpoint
  2. management:
  3. endpoints:
  4. web:
  5. exposure:
  6. include: "*"
  7. ## user
  8. userInfo:
  9. name: 马保国
  10. age: 69
  11. phone: 13123456666
  12. currentProfile: test

4 测试配置

4.1 测试配置中心是否生效

在 Spring Boot 项目中的 bootstrap 配置中不设置激活环境,即:

  1. ## 注释掉当前行
  2. #spring.profiles.active=test

启动配置服务(cloud-alibaba-config)

部分启动日志:

  1. 2020-12-20 10:48:49.148 WARN 86608 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[cloud-alibaba-config.yaml] & group[DEFAULT_GROUP]
  2. 2020-12-20 10:48:49.150 INFO 86608 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-cloud-alibaba-config.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-cloud-alibaba-config,DEFAULT_GROUP'}]
  3. 2020-12-20 10:48:49.156 INFO 86608 --- [ main] .l.d.s.a.c.CloudAlibabaConfigApplication : No active profile set, falling back to default profiles: default

日志显示已经读取到 Nacos 配置中心的配置,并且表明没事设置当前环境

接口请求测试:

请求地址:

  1. http://127.0.0.1:8614/api/cloud/alibaba/config/user

请求方式: GET

返回结果:

  1. 用户名:xxx,年龄: 0,手机号:xxx

对比上边的配置 cloud-alibaba-config 发现,Nacos 配置已经生效

4.2 测试配置修改实时刷新

在 Nacos 控制台修改 cloud-alibaba-config 中的配置,将 userInfo.namexxx 改为 xxx666

Nacos 配置修改

在修改发布之后,后台已经接受到修改的信息,并在控制台打印出相关日志

  1. 2020-12-20 10:59:05.755 INFO 86608 --- [.16.140.10_8688] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [userInfo.name]

再一次测试 4.1 中的接口

返回结果:

  1. 用户名:xxx666,年龄: 0,手机号:xxx

此时,更新后的配置已经生效

至此,一个分布式配置中心已经搭建完成

4.3 多环境配置测试

在 Spring Boot 项目中的 bootstrap 配置中激活环境设置为 dev,即:

  1. spring.profiles.active=dev

重新启动配置服务(cloud-alibaba-config)

接口请求测试:

接口一:

使用 4.1 中的接口配置,返回结果为:

  1. 用户名:xxx666,年龄: 0,手机号:xxx

从返回结果可以看出, cloud-alibaba-config-dev 中的配置并没有生效,而生效的依然是 cloud-alibaba-config 中的配置。

那么是不是 Nacos Config 不支持环境配置?

且看下边的测试

将 Spring Boot 项目中的 bootstrap 配置中激活环境设置为 test,即:

  1. spring.profiles.active=test

重新启动配置服务(cloud-alibaba-config)

接口请求测试:

接口一:

使用 4.1 中的接口配置,返回结果为:

  1. 用户名:马保国,年龄: 69,手机号: 13123456666

接口二:

请求只有 cloud-alibaba-config-test.yaml 中才有的配置

接口请求测试:

请求地址:

  1. http://127.0.0.1:8614/api/cloud/alibaba/config/profile

请求方式: GET

返回结果:

  1. test

这一次的测试结果显示,多环境的配置时生效的,如果指定环境的配置中包含默认的配置,则会覆盖掉默认的配置

cloud-alibaba-config-test.yaml 中的配置会覆盖 cloud-alibaba-config 中的配置

测试到这里,基本可以下一个结论了:

在使用 Nacos Config 在配置多环境的时候,指定环境的配置文件必须包含后缀名,否则不会生效

(这真是个大坑,作者当时添加 cloud-alibaba-config 配置的时候没有添加后缀名,测试生效了,以为多环境的配置也可以不用后缀名,结果折腾了半天,一直找不到原因,最后再一次仔细看了官方文档,然后用对比测试的方法才发现这一特性)

至此,基于 Spring Cloud Alibaba 2.2 的分布式配置中心已经搭建完成了。

5 推荐参考资料

官方文档: Spring Cloud Alibaba Nacos Config

Spring Cloud Alibaba系列教程 - Spring Cloud Alibaba 服务配置

6 Github 源码

Gtihub 源码地址 : https://github.com/Flying9001/springBootDemo

个人公众号:404Code,分享半个互联网人的技术与思考,感兴趣的可以关注.
404Code

发表评论

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

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

相关阅读