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

Spring Cloud Config 2.2.2 是 Spring Cloud 的一个**配置中心组件版本**

Spring Cloud Config 2.2.2 是 Spring Cloud 的一个配置中心组件版本,发布于 2020 年 3 月(属于 Spring Cloud Hoxton.SR3 版本栈),基于 Spring Boot 2.2.x 构建。该版本已停止官方维护(EOL),Spring 官方自 2021 年起已将 Config Server 的演进重心转向Spring Cloud Config 3.x + Spring Boot 2.4+(支持 Config Data API),并最终在 Spring Cloud 2022.x(Kilburn)及之后完全弃用旧版spring-cloud-config-server的传统加载机制。

主要特性(针对 2.2.2):

  • 支持 Git、SVN、本地文件系统等后端存储配置;
  • 提供/actuator/env/actuator/configprops等监控端点(需启用 Actuator);
  • 客户端通过spring.cloud.config.uri拉取配置,支持 profile、label(如分支名)动态匹配;
  • 支持对称/非对称加密(JCE 或 Vault 集成);
  • 与 Eureka、Bus(RabbitMQ/Kafka 实现配置刷新)可集成,但 Bus 在后续版本中也被标记为维护模式。

⚠️ 注意事项:

  • 不兼容 Spring Boot 2.4+ 的新配置加载机制(ConfigDataLocationResolver);
  • 存在已知安全漏洞(如 CVE-2020-5412,路径遍历风险),不建议用于新项目;
  • 官方文档归档地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/

推荐升级路径:
→ Spring Cloud 2021.0.x(Jubilee) + Spring Boot 2.6.x(过渡)
→ Spring Cloud 2022.0.x(Kilburn)+ Spring Boot 3.0+(Java 17+,使用spring-config-data新模型)

# 示例:Config Client(2.2.2)bootstrap.yml(注意:Boot 2.4+ 已废弃 bootstrap)spring:application:name:demo-servicecloud:config:uri:http://config-server:8888profile:devlabel:main

Spring Cloud Config 2.2.2
Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.
Features

Spring Cloud Config Server features:

HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content) Encrypt and decrypt property values (symmetric or asymmetric) Embeddable easily in a Spring Boot application using @EnableConfigServer

Config Client features (for Spring applications):

Bind to the Config Server and initialize Spring Environment with remote property sources Encrypt and decrypt property values (symmetric or asymmetric)

Getting Started

As long as Spring Boot Actuator and Spring Config Client are on the classpath any Spring Boot application will try to contact a config server on http://localhost:8888, the default value of spring.cloud.config.uri. If you would like to change this default, you can set spring.cloud.config.uri in bootstrap.[yml | properties] or via system properties or environment variables.

@Configuration
@EnableAutoConfiguration
@RestController
public class Application {

@Value(“${config.name}”)
String name = “World”;

@RequestMapping(“/”)
public String home() {
return "Hello " + name;
}

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

}

The value of config.name in the sample (or any other values you bind to in the normal Spring Boot way) can come from local configuration or from the remote Config Server. The Config Server will take precedence by default. To see this look at the /env endpoint in the application and see the configServer property sources.

To run your own server use the spring-cloud-config-server dependency and @EnableConfigServer. If you set spring.config.name=configserver the app will run on port 8888 and serve data from a sample repository. You need a spring.cloud.config.server.git.uri to locate the configuration data for your own needs (by default it is the location of a git repository, and can be a local file:… URL).
在 Spring Cloud Config 2.2.2(对应 Spring Cloud Hoxton.SR3)中,与Spring Cloud Bus(通常基于 RabbitMQ 或 Kafka)配合实现配置的自动刷新(即“配置变更→广播→客户端热更新”),需分服务端(Config Server + Bus)和客户端(Config Client + Bus)协同完成。该机制本质是:Config Server 接收/actuator/bus-refresh请求 → 通过 Bus 广播 RefreshRemoteApplicationEvent → 所有订阅 Bus 的客户端监听并触发@RefreshScopeBean 重载

✅ 前提条件:

  • Spring Boot 2.2.x(如 2.2.6.RELEASE)
  • Spring Cloud Hoxton.SR3(含 Config 2.2.2、Bus 2.2.1)
  • 消息中间件:RabbitMQ(推荐)或 Kafka(需对应版本兼容)

🔧 一、服务端(Config Server)配置

1. 添加依赖(Maven)

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId><!-- RabbitMQ --></dependency><!-- 若用 Kafka,替换为 spring-cloud-starter-bus-kafka -->

2. application.yml 配置

spring:application:name:config-serverrabbitmq:host:localhostport:5672username:guestpassword:guestcloud:bus:trace:enabled:true# 可选:开启事件追踪,便于调试config:server:git:uri:https://github.com/your/config-repodefault-label:mainmanagement:endpoints:web:exposure:include:"bus-refresh,health,info,env"# 必须暴露 bus-refresh 端点endpoint:bus-refresh:security:sensitive:false# Spring Boot 2.2 默认关闭敏感检查,但建议显式设 false

✅ 关键:/actuator/bus-refresh是 Bus 提供的全局刷新端点(POST 请求),调用后会广播刷新事件。


📱 二、客户端(Config Client)配置

1. 添加依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency><!-- 注意:无需 spring-cloud-config-client(starter 已包含) -->

2. bootstrap.yml(⚠️ Spring Boot 2.4+ 已废弃,但 2.2.x 必须使用)

spring:application:name:demo-servicecloud:config:uri:http://localhost:8888profile:devlabel:mainrabbitmq:host:localhostport:5672username:guestpassword:guest

3. 启用 @RefreshScope 并注入配置

@RestController@RefreshScope// ← 关键:使 Bean 支持运行时刷新publicclassTestController{@Value("${my.property:default-value}")privateStringmyProperty;@GetMapping("/prop")publicStringgetProp(){returnmyProperty;}}

@RefreshScope作用于类(如@RestController,@Service),其代理会在收到 RefreshEvent 后重建 Bean 实例,从而读取新配置。


🚀 三、触发自动刷新流程

  1. 修改 Git 仓库中demo-service-dev.ymlmy.property: oldmy.property: new
  2. git push提交变更
  3. 手动触发刷新(Config Server 端):
    curl-XPOST http://localhost:8888/actuator/bus-refresh# 或按服务粒度刷新(推荐生产环境):curl-XPOST"http://localhost:8888/actuator/bus-refresh?destination=demo-service:**"
  4. Config Server 通过 Bus 发送RefreshRemoteApplicationEvent
  5. 所有监听 Bus 的demo-service实例收到事件 → 清空@RefreshScope缓存 → 下次请求时重建 Bean → 加载新配置

✅ 验证:访问/prop返回"new"


⚠️ 注意事项(2.2.2 特定)

  • spring-cloud-bus在 Hoxton 中已进入维护模式,官方不再新增功能;
  • 不支持spring-boot-starter-actuator2.4+ 的新 Actuator 结构(如exposure.include写法在 2.2.x 有效);
  • 若使用 Kafka,需确保spring-cloud-starter-bus-kafka与 Kafka Broker 版本兼容(如 2.2.1 对应 Kafka 2.3.x);
  • 安全建议:为/actuator/bus-refresh添加 Spring Security 认证(Basic 或 OAuth2),避免未授权调用。

// 补充:自定义 Bus 刷新监听(可选调试)@ComponentpublicclassBusRefreshListener{@EventListenerpublicvoidhandleRefresh(RefreshRemoteApplicationEventevent){System.out.println("Received refresh event for: "+event.getDestinationService());}}

http://www.jsqmd.com/news/485179/

相关文章:

  • ‌智慧校园系统价格解析:如何看懂报价背后的逻辑与选择适合自己的方案?
  • Elasticsearch相关技术点
  • 2026逆变器OEM代加工供应企业口碑哪家好,这些企业值得关注 - 工业品牌热点
  • 扬州售后完善的月子餐企业有哪些,哪个好用? - mypinpai
  • 沃尔玛购物卡回收避坑指南,安全变现就看这篇! - 京顺回收
  • 聊聊扬州母婴护理服务,母婴护理服务升级亮点多,哪家靠谱有答案 - 工业推荐榜
  • 2026年华广东/福建/江苏/浙江/上海/四川/北京/天津/武汉/香港/澳门恒温储藏设备供应商选型评估报告:聚焦爱敦堡与区域头部企业的能力对比 - 2026年企业推荐榜
  • PCL: CorrespondenceEstimationNormalShooting的使用【2026最新版】
  • 瑞祥商联卡回收实用型攻略,回收避坑指南 - 京回收小程序
  • 【数据分析】数据驱动预测控制策略的比较分析附matlab代码复现
  • 如何快速上手Carefree Creator:AI智能创作的终极指南
  • 2026年定制吸管供应商推荐,专业定制服务靠谱源头厂家揭秘 - myqiye
  • Spring Cloud Circuit Breaker 2.0.0 M1(Milestone 1)是 Spring Cloud 官方在 2022 年初发布的
  • 陕西乐高机器人机构推荐|童程优创:创意启蒙筑基础,少儿编程育科技特长生 - 深度智识库
  • Spring Cloud Kubernetes 2.0.0 M1(Milestone 1)是 Spring Cloud 官方为适配 Kubernetes 原生能力而发布的**首个 2.x 版本里程碑版
  • Hyperswarm与Hypercore生态:构建去中心化应用的完美组合
  • 基于DE-Transformer单变量时序预测 (单输入单输出)Matlab代码
  • P8638 [蓝桥杯 2016 省 A] 密码脱落【LCS】
  • 如何快速集成 Vue Google Autocomplete:打造智能地址搜索体验
  • 光伏气象站:为光伏电站的发电效率评估提供数据支持
  • 如何使用Buster进行高效电子邮件侦察:从入门到精通
  • Spring Cloud Netflix 2.2.2 是 Spring Cloud 的一个**已停止维护的旧版本**,对应 Spring Boot 2.2.x(如 2.2.6.RELEASE)
  • 终极前端面试宝典:Web面试题库开源项目完全指南
  • asp毕业设计——基于asp+access的网上课件管理系统设计与实现(毕业论文+程序源码)——课件管理系统
  • Spring Integration 5.3 RC1(Release Candidate 1)、5.2.6 和 5.1.10 是 Spring Integration 项目在 2020 年初发布的多个
  • Siri Ultra开发路线图:未来将新增哪些令人期待的LLM功能?
  • asp毕业设计——基于asp+access的网上拍卖系统设计与实现(毕业论文+程序源码)——网上拍卖系统
  • 如何使用Goque:Go语言持久化数据结构的终极指南
  • Spring Tools 4.6.1 是 Spring Tools Suite(STS)的继任者——Spring Tools for Eclipse(基于 Eclipse IDE)的一个维护版本
  • Social-Engineer Toolkit (SET) 终极指南:10大社会工程攻击向量深度解析