当前位置: 首页 > news >正文

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.gitsearch-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-servicecloud:config:uri: http://localhost:8888profile: devlabel: 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: trueservice-id: config-server
eureka:client:service-url:defaultZone: http://localhost:8761/eureka/

✅ 总结

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

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

📚 参考资料

  • Spring Cloud Config 官方文档
  • Spring Boot Actuator 文档
http://www.dtcms.com/a/269200.html

相关文章:

  • 七牛云Java开发面试题及参考答案(60道面试题汇总)
  • 华为OD机试 2025B卷 - 最小循环子数组(C++PythonJAVAJSC语言)
  • 【论文笔记】World Models for Autonomous Driving: An Initial Survey
  • 【C++读取输入空格到CHAR数组】2022-7-19
  • 在vue3+ts项目中引入element-plus及其图标
  • 【读代码】深度解析TEN VAD:实时语音活动检测的高性能开源解决方案
  • 从被动救火到主动预测!碧桂园服务以图谱技术重塑IT运维底座
  • 开放端口,开通数据库连接权限,无法连接远程数据库 解决方案
  • Debian 11 Bullseye 在线安装docker
  • Java 命令行参数详解:系统属性、JVM 选项与应用配置
  • axios无感刷新token
  • 万物智联时代启航:鸿蒙OS重塑全场景开发新生态
  • Android kotlin中 Channel 和 Flow 的区别和选择
  • 《Effective Python》第十二章 数据结构与算法——当精度至关重要时使用 decimal
  • 【R语言】Can‘t subset elements that don‘t exist.
  • 学习日记-spring-day42-7.7
  • Java --接口--内部类分析
  • [学习] C语言数学库函数背后的故事:`double erf(double x)`
  • qiankun 微前端框架子应用间通信方法详解
  • 一份多光谱数据分析
  • Spring MVC HandlerInterceptor 拦截请求及响应体
  • [netty5: LifecycleTracer ResourceSupport]-源码分析
  • idea启动后闪一下,自动转为后台运行
  • 全国产化行业自主无人机智能处理单元-AI飞控+通信一体化模块SkyCore-I
  • VmWare 安装 mac 虚拟机
  • 量子计算+AI芯片:光子计算如何重构神经网络硬件生态
  • C++ 定位 New 表达式深度解析与实战教程
  • 如果让计算机理解人类语言- Word2Vec(Word to Vector,2013)
  • 系统学习Python——并发模型和异步编程:基础知识
  • 无需公网IP的文件交互:FileCodeBox容器化部署技术解析