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
能有对应的数据返回
点我下载示例代码
相关文章推荐
- SpringCloud系列教程(8)-- Config数据库JDBC配置
- SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)
- springCloud系列 Config配置中心
- SpringCloud系列十:SpringCloudConfig 高级配置(密钥加密处理(JCE)、KeyStore 加密处理、SpringCloudConfig 高可用机制、SpringCloudBus 服务总线)
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- 六、Spring Cloud系列:Config服务端与客户端常用配置
- SpringCloud | Docker 学习系列 | Kubernetes 学习 将SpringCloud Config 配置中心部署到docker中并放入到Kubernetes中管理
- Dojo1.8系列教程四 ---- Dojo的配置(dojoConfig的使用)
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- 树莓派开发系列教程3——树莓派rasp-config配置
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 树莓派开发系列教程3——树莓派rasp-config配置
- 树莓派开发系列教程3——树莓派rasp-config配置
- SpringCloud系列教程(7)-- Config集群部署