文章目录
1. 简介
Spring Cloud Config provides server-side 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.
- Spring Cloud Config 支持服务端与客户端
- 是一个分布式配置中心
- 提供配置信息多环境切换
- 配置信息更新,实时同步
2. 代码实现
2.1 涉及的模块及整体步骤
2.1.1 涉及的模块
- config-server:配置中心服务端
- config-client:配置中心客户端,验证配置信息访问规则
- config-another-client:配置中心客户端,验证配置信息访问规则
- config-repository:放置于GitHub的配置
2.1.2 整体步骤
- GitHub创建存放配置信息的config-repository目录与配置信息
config-client-dev.properties
、config-another-client-test.properties
,可通过demo中的config-repository
模块关联GitHub上的配置。 - 实现config-server:关键是启动Spring Cloud Config Server功能,指定配置仓库的配置信息
- 实现config-client:从配置仓库读取配置信息
- 实现config-another-client:从配置仓库读取配置信息
- 实现config-repository:放置3、4步骤的配置信息
2.2 源代码
2.2.1 Github地址
https://github.com/andyChenHuaYing/spring-cloud-demo
2.2.2 配置信息地址
配置信息在dev-20180827、与master分支都有,但是代码中使用的是dev-20180827分支的代码,如果想修改值验证,记得确认客户端bootstrap.properties中配置信息与想要访问的仓库地址一致。
https://github.com/andyChenHuaYing/spring-cloud-demo/tree/dev-20180827/config-repository
2.3 config-server
2.3.1 整体实现
- pom.xml引入
spring-cloud-config-server
- application.yml指定配置仓库连接信息
- 启动类使用注解启动类使用注解
@EnableConfigServer
开启配置服务功能
2.3.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-finchley-demo</artifactId>
<groupId>org.oscar.scd</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project