Spring Cloud Config 核心原理

公众号:JavaArchJourney

Spring Cloud Config 是 Spring Cloud 提供的一个用于集中化管理应用程序各个环境下的配置属性的解决方案。它支持统一管理配置,并且可以在不重启应用的情况下动态地更新配置信息,提高开发和运维效率。

主要特点

  • 集中管理配置:可以将不同环境下(如开发、测试、生产)的应用配置集中存储在一个地方,比如Git仓库,便于管理和版本控制。
  • 动态刷新配置 :支持在不重启应用的情况下,动态刷新配置信息,只需要引入spring-boot-starter-actuator依赖,并暴露 refresh 端点即可实现对指定配置项的刷新。
  • 多种存储方式:虽然默认使用Git作为后端存储,但也支持其他存储方式,如Subversion、本地文件系统等,通过简单的配置就可以切换。
  • 加密与解密:提供对配置内容的加密与解密功能,增加了配置的安全性,特别是对于敏感信息的保护。

简单使用示例

创建配置文件

在一个 Git 仓库中创建配置文件 {application}-{profile}.yml 或者 {application}-{profile}.properties ,存放微服务的配置项。比如 myapp-dev.yml 或者 myapp-dev.properties ,表示的是微服务 myappdev 环境下的配置项。

创建 Config Server

pom.xml 添加依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

Spring Boot 应用的主类上添加 @EnableConfigServer 注解以启用 Config Server 功能:

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

application.propertiesapplication.yml 中指定 Git 仓库的位置,Config Server 将从这里读取配置文件。

yaml 复制代码
server:
  port: 8888 # 默认端口为8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-repo/config-repo # 替换为配置文件的Git仓库地址

创建 Config Client

pom.xml 文件中添加依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

bootstrap.propertiesbootstrap.yml 中配置客户端应用去连接到 Config Server 并获取配置信息。

yaml 复制代码
spring:
  application:
    name: myapp # 与 Config Server 拉取的 Git 的配置文件名匹配
  cloud:
    config:
      uri: http://localhost:8888 # Config Server的地址

这样就可以在应用程序代码中通过 @Value 注解或者直接注入 Environment 对象来访问从 Config Server 获取的配置值:

java 复制代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Value("${your.config.key:default_value}")
    private String configValue;

    @GetMapping("/test")
    public String test() {
        return this.configValue;
    }
}

核心原理

Spring Cloud Config 的核心组件:

  • 配置存储(比如Git)
    • 用户将配置文件 push 到 Git 仓库,配置文件按照 {application}-{profile}.yml 或者 {application}-{profile}.properties 格式命名。
  • Config Server
    • 配置服务器,负责从后端存储(如 Git )中读取配置,并提供给客户端访问。需要在项目中添加spring-cloud-config-server依赖,并使用@EnableConfigServer注解启用。
    • 每次 Config Client 请求获取配置信息的时候,Config Server 会从 Git 把最新配置信息拉到本地文件系统(本地 Git 仓库),然后从本地读取并返回。当远程仓库无法获取的时候,会直接从本地返回。
    • 连接 Git 仓库的配置:
      • spring.cloud.config.server.git.uri:Git仓库位置。
      • spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个。
      • spring.cloud.config.server.git.username:访问Git仓库的用户名。
      • spring.cloud.config.server.git.password:访问Git仓库的用户密码。
  • Config Client
    • 配置客户端,连接到配置服务器获取配置信息,并根据这些配置启动或运行。需要添加spring-cloud-starter-config依赖来接入配置中心服务。
    • Cloud Client 在启动的时候,默认会从工程的 classpath 中加载配置信息并启动应用。只有当配置了 spring.cloud.config.uri 的时候,客户端才会尝试连接 Config Server 服务端来获取远程配置信息并初始化 Spring 环境配置
      • 备注:必须将该参数配置在 bootstrap.propertites环境变量或其他优先级高于应用 Jar 包内的配置信息中,才能正确加载到远程配置( Spring boot 的配置加载解有优先级)。
    • Config Server 中存储了多个 Config Client 的配置,因此 Config Client 需要配置声明获取哪一个 Client 的配置文件:
      • spring.cloud.config.uri:配置服务端 Config Server 的地址。可以把 Config Server 作为一个普通的微服务应用,纳入 Eureka 的服务治理体系中,这样就可以通过配置中心的服务名(而不是具体 ip )来获取配置信息。
      • spring.application.name:应用名,对应配置文件规则中的 {application} 部分。
      • spring.cloud.config.profile:环境名,对应配置文件规则中的 {profile} 部分,比如 pregrayonline
      • spring.cloud.config.label:指定 Git 的 branchtagcommit ID,不填写则默认为获取 Git 的 master 分支配置。

Spring Cloud Config 动态更新配置原理:

  • 核心概念:
    • Environment 和 PropertySource :在 Spring 中,Environment 是一个关键接口,它代表了当前应用程序正在运行的环境,并且包含了多个 PropertySource 实例。每个 PropertySource 都是一个潜在的属性来源,如系统属性、环境变量或外部配置文件等。Spring Cloud Config Client 会将从 Config Server 获取到的配置作为 PropertySource 添加到 Environment 中。
    • @RefreshScope 注解 :这是 Spring Cloud 提供的一个特殊 Bean 作用域。标记为 @RefreshScope 的 Bean 在启动时会被代理,当触发刷新操作时,这些 Bean 会重新初始化它们的状态,以反映最新的配置值,特别适用于那些需要根据最新配置立即做出反应的组件。
  • 刷新机制:
    • /actuator/refresh 端点 :Spring Boot Actuator 提供了一个 /actuator/refresh 端点,允许用户手动触发配置的刷新操作。当发送 POST 请求到这个端点时,Spring Cloud 会扫描所有标注了 @RefreshScope 的 Bean,并重新加载它们的配置。
    • Spring Cloud Bus :为了简化在微服务架构中广播配置变更通知的过程,可以使用 Spring Cloud Bus。它本质上是一个消息总线,可以用来传播状态变化(如配置更新)。通过向任意服务实例发送 /actuator/bus-refresh 请求,可以触发整个集群的服务配置更新,而不需要逐个调用每个服务的刷新端点。
  • 动态刷新配置步骤:
    • 客户端请求刷新 :可以通过 HTTP POST 请求访问 /actuator/refresh 端点来手动触发刷新过程。
    • 获取最新配置:一旦收到刷新请求,Spring Cloud Config Client 会尝试从 Config Server 获取最新的配置信息。
    • 更新 Environment :获取到的新配置将被添加到现有的 Environment 中,覆盖旧的配置值。
    • 重刷 @RefreshScope Beans :对于那些被 @RefreshScope 注解标记的 Bean,Spring 将重新初始化它们,使它们能够感知到最新的配置变化。
      • @RefreshScope 是 Spring Cloud 中用于实现配置动态刷新的机制,其原理是通过创建一个代理对象来替代被标记为 @RefreshScope 的 Bean,并采用延迟初始化策略(即仅在首次访问时根据最新的配置信息创建并初始化该 Bean)。当配置发生变更并触发刷新操作时,Spring Cloud 会发布刷新事件,RefreshScope 捕获此事件后将相关 Bean 的缓存视为过期并清除;下次访问这些 Bean 时,系统基于更新后的配置重新创建和初始化它们,从而实现在不重启应用的情况下响应配置变化的能力。

参考

相关推荐
林林code9 小时前
从源码的角度解读 Nacos 是如何动态刷新配置的
spring cloud
太阳之神aboluo10 小时前
SpringCloud (4) 分布式事务
java·spring·spring cloud
林林code12 小时前
Springboot中的yml为单个的服务配置Feign不生效.md
spring cloud
麦兜*17 小时前
Spring Boot 与 Ollama 集成部署私有LLM服务 的完整避坑指南,涵盖 环境配置、模型管理、性能优化 和 安全加固
java·spring boot·后端·安全·spring cloud·性能优化
麦兜*18 小时前
国产大模型平替方案:Spring Boot通义千问API集成指南
java·spring boot·后端·python·spring cloud·系统架构·springboot
程序员陆通1 天前
Spring Cloud微服务中的内存泄漏问题定位与解决方案
java·spring cloud·微服务
麦兜*2 天前
Spring Integration 整合 Web3.0网关:智能合约事件监听与Spring Integration方案
java·spring boot·后端·spring·spring cloud·web3·智能合约
天机️灵韵2 天前
开源医院信息管理系统:基于若依框架的智慧医疗解决方案
java·开发语言·spring boot·spring cloud·github·开源项目
JavaArchJourney2 天前
Spring Cloud Gateway 核心原理(含源码分析)
spring cloud