Spring Cloud Config 1 (分布式配置中心)

本文介绍如何使用Spring Cloud Config创建配置中心,并实现不同环境下的配置管理。通过构建配置中心和服务客户端,实现了配置的集中管理和动态刷新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring cloud config是spring cloud团队创建的一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端和客户端两部分。

服务端也被称为配置中心,其除了可以在spring构建的应用程序中使用也可以在其它语言运行的程序中使用。

构建配置中心:

新建项目命名为config-server并且添加config-server的依赖

主类中加入@EnableConfigServer注解,开启Spring Cloud Config的服务端功能

在application.properties中添加配置服务的基本信息以及Git仓库的相关信息

spring.application.name=config-server
server.port=7001
# 配置Git仓库位置
spring.cloud.config.server.git.uri=http://172.16.99.16/rxhan/test.git
# 配置仓库路径下的相对搜索位置
spring.cloud.config.server.git.searchPaths=config-repo
# Git仓库访问的用户名
spring.cloud.config.server.git.username=rxhan@travelsky.com
# Git仓库访问的密码
spring.cloud.config.server.git.password=12345678

到这里,使用一个通过Spring Cloud Config实现,并使用Git管理配置内容的分布式配置中心就完成了。启动项目。

配置规则详解:

为了验证配置中心,我们在Git仓库下创建config-repo如下目录

并根据环境创建出不同的文件

didispace.properties

didispace-dev.properties

didispace-test.properties

didspace-prod.properties

在四个配置文件中均设置一个from属性,并未每个配置文件分别设置不同的值

from=git-default-1.0

from=git-dev-1.0

from=git-test-1.0

from=git-prod-1.0

访问测试

http://localhost:7001/didispace/dev

客户端配置:

创建项目 config-client 并加入config依赖和web依赖

 

 加入如下配置文件

bootstrap.properties

spring.application.name=didispace
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/
server.port=7002

新建测试controller

访问测试

http://localhost:7002/from

至此基于spring cloud config的配置中心及客户端配置完成。

 

转载于:https://www.cnblogs.com/HanShisi/p/8328565.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值