1.配置获取流程图

image.png

2.单机版客户端搭建

引入依赖

  1. <!--Spring Cloud Alibaba Config 依赖-->
  2. <dependency>
  3. <groupId>com.alibaba.cloud</groupId>
  4. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  5. </dependency>
  6. <!--SpringCloud ailibaba nacos 服务注册与发现模块 -->
  7. <dependency>
  8. <groupId>com.alibaba.cloud</groupId>
  9. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  10. </dependency>

编写bootstrap.yml配置文件

bootstrap.yml 是系统级别的,加载优先级高于 application.yml ,负责从外部加载配置并解析

  1. management:
  2. endpoints:
  3. web:
  4. exposure:
  5. include: '*' #springboot监控开启所有端点
  6. server:
  7. port: 8083
  8. spring:
  9. application:
  10. name: config-client
  11. cloud:
  12. nacos:
  13. discovery:
  14. server-addr: 127.0.0.1:8848 #服务注册到nacos注册中心的地址
  15. config:
  16. server-addr: 127.0.0.1:8848 #Nacos作为配置中心地址
  17. file-extension: yaml #指定yaml格式的配置

编写application.yml配置文件

可以通过指定不同的激活文件配合Data Id从nacos获取不同环境下的配置

  1. spring:
  2. profiles:
  3. active: dev #激活 dev 的配置

nacos控制台添加配置

image.pngimage.png
image.png
Nacos Server 配置的Data ID的完整格式如下:${prefix}-${spring.profiles.active}.${file-extension}
dataId 格式中各参数说明如下:

  1. ${prefix}:默认取值为微服务的服务名,即配置文件中 spring.application.name 的值,我们可以在配置文件中通过配置 spring.cloud.nacos.config.prefix 来指定。
  2. ${spring.profiles.active}:表示当前环境对应的 Profile,例如 dev、test、prod 等。当没有指定环境的 Profile 时,其对应的连接符也将不存在, dataId 的格式变成 ${prefix}.${file-extension}。
  3. ${file-extension}:表示配置内容的数据格式,我们可以在配置文件中通过配置项 spring.cloud.nacos.config.file-extension 来配置,例如 properties 和 yaml。

    启动类添加注解

    ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication @EnableDiscoveryClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }

  1. <a name="F2tWZ"></a>
  2. ### 编写配置类获取配置
  3. **注意注解@RefreshScope**
  4. ```java
  5. import org.springframework.boot.context.properties.ConfigurationProperties;
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;
  7. import org.springframework.stereotype.Component;
  8. /**
  9. * @author 冯铁城 [17615007230@163.com]
  10. * @date 2022-05-31 16:20:25
  11. * @describe:
  12. */
  13. @Component
  14. @ConfigurationProperties(prefix = "config")
  15. @RefreshScope
  16. public class Config {
  17. private String version;
  18. private String name;
  19. public String getVersion() {
  20. return version;
  21. }
  22. public void setVersion(String version) {
  23. this.version = version;
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. }

配置使用

注意注解@RefreshScope

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.cloud.context.config.annotation.RefreshScope;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. /**
  7. * @author 冯铁城 [17615007230@163.com]
  8. * @date 2022-05-31 16:20:19
  9. * @describe:
  10. */
  11. @RefreshScope
  12. @RestController
  13. @RequestMapping("api/vi/config-client")
  14. public class Controller {
  15. @Autowired
  16. private Config config;
  17. @GetMapping
  18. public String get() {
  19. return "name:" + config.getName() + " | version:" + config.getVersion();
  20. }
  21. }