SpringCloud系列教程(5)-- Config服务端配置

本文介绍Spring Cloud Config组件,用于管理不同环境的配置文件,实现配置的热更新与集中管理,降低项目维护成本。

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

###Config有什么用
config,就是我们管理properties的一个springCloud组件,在实际的项目中,我们可能有很多配置参数,一般都会配置在properties文件中,而实际的开发中,我们又会分多个环境,比如,开发环境,测试环境,预发环境,生产环境,对应的就会是多套properties,在各个阶段,需要人工干预切换到个套环境配置文件。而且在实际中,我们又会碰到一个问题,比如某些业务参数我们配置在properties的一个参数,突然在线上需要调整,按照常理,我们只能修改properties文件中的值,重新打包,重新部署,最后重新上线,来处理。但是config给了我们一套新的思路,通过config来配置properties文件,而且还能在某些地方做到热变更(当然不是全部,比如数据库连接等),并且能够统一管理,这样就减轻了项目的维护成本。

###我们怎么使用Config
1.新建项目 cloud-config-server ,引入 config-server模块

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

2.在项目启动类 CloudConfigServerApplication.java 上新增@EnableConfigServer 注解

@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {

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

3.配置application.properties,文件,config 支持多种配置,这里采用默认配置GIT的方式

server.port=8082
spring.application.name=cloud-server
spring.cloud.config.server.git.uri=git地址
spring.cloud.config.server.git.username=git用户名
spring.cloud.config.server.git.password=git密码
#配置文件存放的git的文件路径
spring.cloud.config.server.git.searchPaths=config-repo

4.我们在git上新建一个config-repo,然后新建 cloud-client-dev.properties,cloud-client-test.properties两个文件
这里着重说明:文件格式名字是客户端的
文件格式名字是:客户端application+profile.properties,
application代表客户端名称,profile代表环境名称,还有一个label(可选)代表分支名称,比如我们在master分支 lable=master
在cloud-client-dev.properties 我们写入:

foo=this is dev properties

在cloud-client-test.properties 我们写入:

foo=this is test properties

然后提交GIT服务器。

5.启动项目
访问的路径 IP:端口/{application}/{profile}/{lable}
因此我们依次访问
http://127.0.0.1:8082/cloud-client/dev/master
http://127.0.0.1:8082/cloud-client/test/master
能有对应的数据返回
数据验证
###点我下载示例代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值