SpringCloud Config分布式配置中心

浅浅的花香味﹌ 2023-10-11 20:36 116阅读 0赞

目录

一、概述

二、Config服务端配置与测试

配置读取规则

三、Config客户端配置与测试

bootstrasp.yml

四、Config客户端之动态刷新


一、概述

官网:Spring Cloud Config

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

SpringCloud Config分为服务端客户端两部分。

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。

SpringCloud Config功能

  1. 可以集中管理配置文件;
  2. 不同环境不同配置,动态化的更新配置,分环境部署比如dev/test/prod/beta/release
  3. 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  4. 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  5. 将配置信息以REST接口的形式暴露,可以以Post方式通过curl命令行手动更新配置

由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式

二、Config服务端配置与测试

服务端的ConfigServer是直接向GitHub拉取配置文件的

在GitHub新建一个仓库springcloud-config用于测试,并创建三份不同环境下的配置文件

061cb424b40a445eb22055ebaa720702.png

新建Module模块cloud-config-center-3344,它即为Cloud的配置中心模块cloudConfig Center

pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-config-server</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-web</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-actuator</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-devtools</artifactId>
  21. <scope>runtime</scope>
  22. <optional>true</optional>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. <optional>true</optional>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. </dependencies>

yml

  1. server:
  2. port: 3344
  3. spring:
  4. application:
  5. name: cloud-config-center #注册进Eureka服务器的微服务名
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: git@github.com:zzyybs/springcloud-config.git #GitHub上面的git仓库名字
  11. ####搜索目录
  12. search-paths:
  13. - springcloud-config
  14. ####读取分支
  15. label: master
  16. #服务注册到eureka地址
  17. eureka:
  18. client:
  19. service-url:
  20. defaultZone: http://localhost:7001/eureka

主启动类 @EnableConfigServer

测试

0baea92a57ee4bff8248661994e64bf9.png

配置读取规则

be75dc00654a401897646065bc1fc6ae.png

其中,label为标签, profile为环境(dev/test/prod),application可以为服务名,所以我们在Github上的配置文件要采取上面的命名格式

三、Config客户端配置与测试

客户端的Config是向服务端ConfigServer拉取配置信息

新建cloud-config-client-3355

pom.xml(注意,客户端导入的依赖是spring-cloud-starter-config,服务端是spding-cloud-config-server

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-config</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-web</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-actuator</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-devtools</artifactId>
  21. <scope>runtime</scope>
  22. <optional>true</optional>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. <optional>true</optional>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. </dependencies>

bootstrasp.yml

applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高

即application.yml是当前微服务自己的配置文件,bootstrap是拉取中心配置的文件,但是如果有同名的配置优先采用bootstrap(中心配置)。

Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的`Application Context`的父上下文。初始化的时候,`Bootstrap Context`负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的`Environment`。

`Bootstrap`属性有高优先级,默认情况下,它们不会被本地配置覆盖。 `Bootstrap context`和`Application Context`有着不同的约定,所以新增了一个`bootstrap.yml`文件,保证`Bootstrap Context`和`Application Context`配置的分离。

要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,
因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml

  1. server:
  2. port: 3355
  3. spring:
  4. application:
  5. name: config-client
  6. cloud:
  7. #Config客户端配置
  8. config:
  9. label: master #分支名称
  10. name: config #配置文件名称
  11. profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
  12. uri: http://localhost:3344 #配置中心地址k
  13. #服务注册到eureka地址
  14. eureka:
  15. client:
  16. service-url:
  17. defaultZone: http://localhost:7001/eureka

主启动

  1. @EnableEurekaClient
  2. @SpringBootApplication
  3. public class ConfigClientMain3355
  4. {
  5. public static void main(String[] args)
  6. {
  7. SpringApplication.run(ConfigClientMain3355.class,args);
  8. }
  9. }
  10. a

业务类,configServer会将配置信息以REST接口的形式暴露

  1. @RestController
  2. public class 9
  3. {
  4. @Value("${config.info}")
  5. private String configInfo;
  6. @GetMapping("/configInfo")
  7. public String getConfigInfo()
  8. {
  9. return configInfo;
  10. }
  11. }

测试

be50cc720b0841b5b8080c842374ccb1.png

这里出现了一个问题,当我们修改github上的配置文件时,服务端直接拉取可以动态改变,但是客户端config并没有发生改变,所以我们需要做进一步配置开启动态刷新配置

四、Config客户端之动态刷新

避免每次更新配置都要重启客户端Config微服务

pom.xml中我们需要引入actuator

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>

修改YML

  1. # 暴露监控端点
  2. management:
  3. endpoints:
  4. web:
  5. exposure:
  6. include: "*"

业务类加上@RefreshScope

  1. @RestController
  2. @RefreshScope
  3. public class ConfigClientController
  4. {
  5. @Value("${config.info}")
  6. private String configInfo;
  7. @GetMapping("/configInfo")
  8. public String getConfigInfo() {
  9. return configInfo;
  10. }
  11. }

但还需要加上一步,需要运维人员发送Post请求刷新3355,可以通过curl命令的方式刷新

  1. curl -X POST "http://localhost:3355/actuator/refresh"

50647ea8d4054f919e73e6036b960adc.png

但如果有很多个客户端都需要手动刷新的话也很麻烦,可否广播,一次通知,处处生效?

这就开启了下一个篇章,SpringCloud Bus消息总线

发表评论

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

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

相关阅读