创建配置

image.png
image.png
其中:

  • Data ID:填入alibaba-nacos-config-client.properties
  • Group:不修改,使用默认值DEFAULT_GROUP
  • 配置格式:选择Properties
  • 配置内容:应用要加载的配置内容,这里仅作为示例,做简单配置

    创建应用

    第一步:创建SpringBoot应用alibaba-nacos-config-client
    第二步:编辑pom.xml ```java <?xml version=”1.0” encoding=”UTF-8”?>

    1. <artifactId>spring-cloud-learn</artifactId>
    2. <groupId>com.example</groupId>
    3. <version>0.0.1-SNAPSHOT</version>

    4.0.0 alibaba-nacos-config-client 11 UTF-8 UTF-8 2.3.7.RELEASE 2.2.2.RELEASE Hoxton.SR9 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine

  1. **第三步:**创建应用主类
  2. ```java
  3. package com.demo;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.boot.SpringApplication;
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;
  8. import org.springframework.cloud.context.config.annotation.RefreshScope;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. /**
  12. * @author wujiawei
  13. * @see
  14. * @since 2021/4/12 下午9:21
  15. */
  16. @SpringBootApplication
  17. public class AlibabaNacosConfigClientApplication {
  18. public static void main(String[] args) {
  19. SpringApplication.run(AlibabaNacosConfigClientApplication.class, args);
  20. }
  21. @Slf4j
  22. @RefreshScope
  23. @RestController
  24. static class EchoController {
  25. @Value("${title_en}")
  26. private String titleEn;
  27. @Value("${title_cn}")
  28. private String titleCn;
  29. @GetMapping("echo")
  30. public String echo() {
  31. return titleEn + ", " + titleCn;
  32. }
  33. }
  34. }

其中:

  • 通过@RefreshScope注解使这个类下的配置内容支持动态刷新
  • 通过@Value注解获取配置内容

第四步:创建配置文件bootstrap.properties,配置服务名称和Nacos地址

  1. spring.application.name=alibaba-nacos-config-client
  2. server.port=8002
  3. spring.cloud.nacos.config.server-addr=127.0.0.1:8848

注意:这里必须使用bootstrap.properties。同时,spring.application.name值必须与上一阶段Nacos中创建的配置Data Id匹配(除了.properties或者.yaml后缀)。

第五步:启动应用

  1. 2021-04-12 21:36:32.770 WARN 49987 --- [-127.0.0.1_8848] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[alibaba-nacos-config-client] & group[DEFAULT_GROUP]
  2. 2021-04-12 21:36:32.890 INFO 49987 --- [-127.0.0.1_8848] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-alibaba-nacos-config-client.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-alibaba-nacos-config-client,DEFAULT_GROUP'}]

看到类似的日志信息,说明Nacos配置读取成功。

第六步:验证配置内容和动态刷新
用curl访问接口,查看输出的内容。然后修改Nacos中的相关配置,再次访问接口查看内容。

  1. wujiawei@wujiaweideMacBook-Pro ~ % curl http://localhost:8002/echo
  2. spring cloud learning, 斯普林 克劳德 棱宁
  3. wujiawei@wujiaweideMacBook-Pro ~ % curl http://localhost:8002/echo
  4. spring_cloud_learning_test, 斯普林 克劳德 棱宁 泰斯特

代码示例

  • Github:
  • Gitee: