1.什么是Spring Cloud Config

Spring Cloud Config 是由 Spring Cloud 团队开发的项目,它可以为微服务架构中各个微服务提供集中化的外部配置支持。
简单点说就是,Spring Cloud Config 可以将各个微服务的配置文件集中存储在一个外部的存储仓库或系统(例如 Git 、SVN 等)中,对配置的统一管理,以支持各个微服务的运行。
Spring Cloud Config 包含以下两个部分:

  • Config Server:也被称为分布式配置中心,它是一个独立运行的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密信息和解密信息的访问接口。
  • Config Client:指的是微服务架构中的各个微服务,它们通过 Config Server 对配置进行管理,并从 Config Sever 中获取和加载配置信息。

Spring Cloud Config 默认使用 Git 存储配置信息,因此使用 Spirng Cloud Config 构建的配置服务器天然就支持对微服务配置的版本管理。我们可以使用 Git 客户端工具方便地对配置内容进行管理和访问。除了 Git 外,Spring Cloud Config 还提供了对其他存储方式的支持,例如 SVN、本地化文件系统等。

2.Spring Cloud Config的工作原理

Spring Cloud Config 工作流程如下:

  1. 开发或运维人员提交配置文件到远程的 Git 仓库。
  2. Config 服务端(分布式配置中心)负责连接配置仓库 Git,并对 Config 客户端暴露获取配置的接口。
  3. Config 客户端通过 Config 服务端暴露出来的接口,拉取配置仓库中的配置。
  4. Config 客户端获取到配置信息,以支持服务的运行

具体流程如图:
image.png

3.Spring Cloud Config的特点

Spring Cloud Config 具有以下特点:

  1. Spring Cloud Config 由 Spring Cloud 团队开发,可以说是 Spring 的亲儿子,能够与 Spring 的生态体系无缝集成。
  2. Spring Cloud Config 将所有微服务的配置文件集中存储在一个外部的存储仓库或系统(例如 Git)中,统一管理。
  3. Spring Cloud Config 配置中心将配置以 REST 接口的形式暴露给各个微服务,以方便各个微服务获取。
  4. 微服务可以通过 Spring Cloud Config 向配置中心统一拉取属于它们自己的配置信息。
  5. 当配置发生变化时,微服务不需要重启即可感知到配置的变化,并自动获取和应用最新配置。
  6. 一个应用可能有多个环境,例如开发(dev)环境、测试(test)环境、生产(prod)环境等等,开发人员可以通过 Spring Cloud Config 对不同环境的各配置进行管理,且能够确保应用在环境迁移后仍然有完整的配置支持其正常运行。

    4.搭建config服务端

    1.创建配置文件文件夹并提交远程仓库

    创建配置文件文件夹

    image.png

    文件夹添加yml配置文件

    1. config:
    2. info: ftc
    3. version: 1.0

    配置文件文件夹提交到远程仓库对应分支

    image.png

    2.创建Config服务端

    引入依赖

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-config-server</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-web</artifactId>
    8. </dependency>
    9. <dependency>
    10. <groupId>org.springframework.cloud</groupId>
    11. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    12. </dependency>

    配置文件

    ```yaml server: port: 3344 spring: application: name: spring-cloud-config-center cloud: config: server:

    1. git:
    2. #Git地址
    3. uri: https://gitee.com/fengtiecheng/test-config.git
    4. #配置文件仓库地址(对应配置文件存放文件夹即可)
    5. search-paths:
    6. - test-config
    7. #用户名密码
    8. username: fengtiecheng
    9. password: Feng24301314
    10. force-pull: true

    分支

    label: test

eureka: instance: instance-id: spring-cloud-config prefer-ip-address: true lease-expiration-duration-in-seconds: 15 #服务过期时间,如果超过这个时间没有检测到心跳,就将这个实例剔除 lease-renewal-interval-in-seconds: 5 #client发送心跳给server端的频率 client: service-url: defaultZone: http://eureka1001.com:1001/eureka/,http://eureka2001.com:2001/eureka/

  1. <a name="WPxgb"></a>
  2. #### 启动类添加注解
  3. ```java
  4. @EnableConfigServer
  5. @SpringBootApplication
  6. @EnableEurekaClient
  7. public class ConfigApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(ConfigApplication.class, args);
  10. }
  11. }

浏览器访问配置文件

Spring Cloud Config 规定了一套配置文件访问规则,如下:

访问规则 示例
/{application}/{profile}[/{label}] /config/dev/master
/{application}-{profile}.{suffix} /config-dev.yml
/{label}/{application}-{profile}.{suffix} /master/config-dev.yml

访问规则内各参数说明如下。

  1. {application}:应用名称,即配置文件的名称,例如 config。
  2. {profile}:环境名,一个项目通常都有开发(dev)版本、测试(test)环境版本、生产(prod)环境版本,配置文件则以 application-{profile}.yml 的形式进行区分,例如 application-dev.yml、application-test.yml、application-prod.yml 等。
  3. {label}:Git 分支名,默认是 master 分支,当访问默认分支下的配置文件时,该参数可以省略,即第二种访问方式。
  4. {suffix}:配置文件的后缀,例如 config-dev.yml 的后缀为 yml。

通过这套规则,我们在浏览器上就直接对配置文件进行访问。
示例:
浏览器输入:http://localhost:3344/test/service4-dev.yml
image.png

5.搭建config客户端

引入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-config</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  12. </dependency>

编写bootstrap.yml配置文件

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

  1. server:
  2. port: 3355
  3. spring:
  4. application:
  5. name: spring-cloud-config-client
  6. cloud:
  7. config:
  8. label: test #分支名称
  9. name: service2 #配置文件名称,config-dev.yml 中的 config
  10. profile: dev #环境名 config-dev.yml 中的 dev
  11. discovery: #集群形式从配置服务端拉取配置
  12. enabled: true
  13. service-id: spring-cloud-config-center #配置服务端application.name
  14. eureka:
  15. instance:
  16. instance-id: spring-cloud-config-client
  17. prefer-ip-address: true
  18. lease-expiration-duration-in-seconds: 15 #服务过期时间,如果超过这个时间没有检测到心跳,就将这个实例剔除
  19. lease-renewal-interval-in-seconds: 5 #client发送心跳给server端的频率
  20. client:
  21. service-url:
  22. defaultZone: http://eureka1001.com:1001/eureka/,http://eureka2001.com:2001/eureka/

启动类添加注解

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class ConfigClientApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigClientApplication.class, args);
  6. }
  7. }
  • 配置更新后,Spring Cloud Config 服务端(Server)可以直接从 Git 仓库中获取最新的配置。
  • 除非重启 Spring Cloud Config 客户端(Client),否则无法通过 Spring Cloud Config 服务端获取最新的配置信息。

    6.Config客户端实时更新最新配置

    请参考:点击这里