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 构建。该版本已停止官方维护EOLSpring 官方自 2021 年起已将 Config Server 的演进重心转向Spring Cloud Config 3.x Spring Boot 2.4支持 Config Data API并最终在 Spring Cloud 2022.xKilburn及之后完全弃用旧版spring-cloud-config-server的传统加载机制。主要特性针对 2.2.2支持 Git、SVN、本地文件系统等后端存储配置提供/actuator/env、/actuator/configprops等监控端点需启用 Actuator客户端通过spring.cloud.config.uri拉取配置支持 profile、label如分支名动态匹配支持对称/非对称加密JCE 或 Vault 集成与 Eureka、BusRabbitMQ/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.xJubilee Spring Boot 2.6.x过渡→ Spring Cloud 2022.0.xKilburn Spring Boot 3.0Java 17使用spring-config-data新模型# 示例Config Client2.2.2bootstrap.yml注意Boot 2.4 已废弃 bootstrapspring:application:name:demo-servicecloud:config:uri:http://config-server:8888profile:devlabel:mainSpring Cloud Config 2.2.2Spring 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.FeaturesSpring 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 EnableConfigServerConfig 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 StartedAs 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.ConfigurationEnableAutoConfigurationRestControllerpublic 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.nameconfigserver 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.RELEASESpring Cloud Hoxton.SR3含 Config 2.2.2、Bus 2.2.1消息中间件RabbitMQ推荐或 Kafka需对应版本兼容 一、服务端Config Server配置1. 添加依赖MavendependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-config-server/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-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. 添加依赖dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-config/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bus-amqp/artifactId/dependency!-- 注意无需 spring-cloud-config-clientstarter 已包含 --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:guest3. 启用 RefreshScope 并注入配置RestControllerRefreshScope// ← 关键使 Bean 支持运行时刷新publicclassTestController{Value(${my.property:default-value})privateStringmyProperty;GetMapping(/prop)publicStringgetProp(){returnmyProperty;}}✅RefreshScope作用于类如RestController,Service其代理会在收到 RefreshEvent 后重建 Bean 实例从而读取新配置。 三、触发自动刷新流程修改 Git 仓库中demo-service-dev.yml的my.property: old→my.property: newgit push提交变更手动触发刷新Config Server 端curl-XPOST http://localhost:8888/actuator/bus-refresh# 或按服务粒度刷新推荐生产环境curl-XPOSThttp://localhost:8888/actuator/bus-refresh?destinationdemo-service:**Config Server 通过 Bus 发送RefreshRemoteApplicationEvent所有监听 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.coloradmin.cn/o/2416030.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!