Spring Cloud Config(微服务配置中心详解)

关键词:Spring Cloud Config、配置中心、远程仓库、动态刷新、加密解密


✅ 摘要

在微服务架构中,随着服务数量的增加,统一管理各服务的配置信息变得尤为重要。传统的本地配置文件方式难以满足多环境、多实例、集中化的需求。

Spring Cloud Config 是 Spring Cloud 提供的一个分布式配置中心解决方案,支持将配置信息集中存储在 Git、SVN 或本地文件系统中,并提供给各个微服务动态获取和更新。

本文将围绕 Spring Cloud Config 的核心功能与使用场景 展开讲解:

  • Spring Cloud Config 原理概述
  • 搭建 Config Server 服务端
  • 微服务集成 Config Client 客户端
  • 配置文件的命名规则与多环境支持
  • 动态刷新配置(@RefreshScope)
  • 加密与解密敏感信息
  • 实战案例:结合 Eureka、Git 实现配置中心集群部署

📌 一、Spring Cloud Config 简介

🔹 1. 什么是 Spring Cloud Config?

Spring Cloud Config 是一个用于为分布式系统中的多个微服务提供统一外部配置管理的服务组件。它支持从远程 Git/SVN 存储库或本地目录中读取配置文件。

🔹 2. 主要特性

特性描述
集中式管理所有服务的配置统一管理,避免分散维护
多环境支持支持 dev、test、prod 等不同 profile 配置
动态刷新结合 Spring Cloud Bus + RabbitMQ/Kafka 实现配置热更新
安全控制支持加密解密敏感信息(如数据库密码)

📌 二、搭建 Spring Cloud Config Server

🔹 1. 添加依赖(Maven)

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>

🔹 2. 启动类添加注解

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

🔹 3. 配置 application.yml

示例:连接 GitHub 仓库
server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/yourname/config-repo.git
          search-paths: config-data # 可选,指定子目录

🔹 4. 启动服务并访问配置

启动后可以通过以下 URL 获取配置:

http://localhost:8888/{application}/{profile}/{label}

例如:

http://localhost:8888/user-service/dev/master

📌 三、微服务作为 Config Client 接入

🔹 1. 添加依赖(Maven)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

🔹 2. 创建 bootstrap.yml(注意不是 application.yml)

spring:
  application:
    name: user-service
  cloud:
    config:
      uri: http://localhost:8888
      profile: dev
      label: master

🔹 3. 使用配置项

@Value("${user.config}")
private String userConfig;

@GetMapping("/config")
public String getConfig() {
    return "当前配置值:" + userConfig;
}

📌 四、配置文件命名规则详解

Spring Cloud Config 根据以下格式查找配置文件:

/{application}-{profile}.yml
/{application}-{profile}.properties

例如:

  • user-service-dev.yml
  • order-service-prod.yml

你还可以通过 /config-data 目录下的文件结构组织多服务、多环境配置。


📌 五、动态刷新配置(@RefreshScope)

🔹 1. 添加 Actuator 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

🔹 2. 在 Bean 上添加 @RefreshScope 注解

@Component
@RefreshScope
public class UserProperties {

    @Value("${user.config}")
    private String userConfig;

    // getter/setter
}

🔹 3. 触发刷新(POST 请求)

curl -X POST http://localhost:8080/actuator/refresh

📌 六、加密与解密敏感信息

🔹 1. 启用加密支持

确保已安装 Java Cryptography Extension (JCE),然后在配置中心项目中添加:

encrypt:
  key: my-secret-key

🔹 2. 加密明文内容

发送请求加密数据:

curl -X POST --data-urlencode "data=mydbpassword" http://localhost:8888/encrypt

返回类似:

{ "value": "ENC(AES/GCM/PBE加密后的字符串)" }

🔹 3. 在配置文件中使用加密值

spring:
  datasource:
    password: ENC(加密后的字符串)

📌 七、实战案例:结合 Eureka 和 Git 实现配置中心集群

场景说明:

  • 搭建两个 Config Server 实例,分别运行在 8888 和 8889
  • 使用同一个 Git 仓库
  • 微服务通过 Eureka 注册发现 Config Server 地址
  • 实现高可用与负载均衡
示例:Eureka + Config Client 配置
spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

✅ 总结

以下是微服务配置的主要点:

模块技能点
Spring Cloud Config 原理配置中心的作用与工作原理
Config Server 搭建Git/SVN 配置、YAML 文件命名规范
Config Client 接入微服务如何加载远程配置
动态刷新机制使用 @RefreshScope 实现热更新
加密解密配置对数据库密码等敏感信息进行安全处理
高可用部署结合 Eureka 实现集群部署与负载均衡

📚 参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值