您的位置:首页 > 编程语言 > Java开发

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

2018-01-12 18:06 387 查看

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

能有对应的数据返回



点我下载示例代码

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐