文章目录
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>
2.3.3 application.yml
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/andyChenHuaYing/spring-cloud-demo
searchPaths: config-repository
username:
password:
label: dev-20180827
server:
port: 8769
2.3.4 ConfigClientApplication
package org.oscar.scd.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
2.4 config-client
2.4.1 整体实现
- pom.xml文件中引入依赖
spring-cloud-starter-config
- bootstrap.properties中指定Config Server连接信息以及需要访问配置中心的具体配置信息
- ConfigClientApplication常规Spring Boot启动类
2.4.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-client</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
2.4.3 bootstrap.properties
下方信息与配置中心的配置文件的对应关系见后续验证部分
spring.application.name=config-client
spring.cloud.config.label=dev-20180827
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8769/
server.port=8770
2.5 config-another-client
与config-client基本一致,区别在于`bootstrap.yml`中的配置信息
2.5.1 整体实现
- pom.xml文件中引入依赖
spring-cloud-starter-config
- bootstrap.properties中指定Config Server连接信息以及需要访问配置中心的具体配置信息
- ConfigClientApplication常规Spring Boot启动类
2.5.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-client</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
2.4.3 bootstrap.yml
spring:
application:
name: config-another-client
cloud:
config:
label: dev-20180827
profile: test
uri: http://localhost:8769/
server:
port: 8771
2.4.4 ConfigAnotherClientApplication
package org.oscar.scd.config.another.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class ConfigAnotherClientApplication {
@Value("${foo}")
private String foo;
@RequestMapping("/readFooProp")
public String readFooProp() {
return this.foo;
}
public static void main(String[] args) {
SpringApplication.run(ConfigAnotherClientApplication.class, args);
}
}
2.6 config-repository
保存配置文件
2.6.1 config-client配置文件
config-client-dev.properties
foo=config-client-dev value
2.6.2 config-another-client配置文件
config-another-client-test.properties
foo=config-another-client foo value.
3. 验证
3.1 创建SpringBoot启动类
简单创建Spring Boot启动类即可
3.1.1 ConfigServerApplication
3.1.2 ConfigClientApplication
参考上一节
3.1.3 ConfigAnotherClientApplication
参考上一节
3.2 启动
ConfigServerApplication
ConfigClientApplication
ConfigAnotherClientApplication
观察ConfigClientApplication
、ConfigAnotherClientApplication
输出日志,会发现程序在启动过程中会通过PropertySourceBootstrapConfiguration
将远程配置信息关联起来:
3.3 读取远程配置信息
3.3.1 ConfigServerApplication
- 启动之后可以根据指定规则访问远程配置完整信息,
/{name}/{profile}/{label}
- name:
spring.application.name
- profile:spring boot 的profile,即 application-{profile}.yml中的profile值,如application-dev.yml文件的profile是dev。
- label:application.yml配置文件中
spring.cloud.config.label
的值,这里是dev-20180827
- name:
- 查看config-client的bootstrap.properties信息可知,其对应的远程配置文件为
https://github.com/andyChenHuaYing/spring-cloud-demo/config-repository/config-client-dev.properties
- 查看config-another-client的bootstrap.properties信息可知,其对应的远程配置文件为
https://github.com/andyChenHuaYing/spring-cloud-demo/config-repository/config-another-client-test.properties
访问地址:http://localhost:8769/config-client/dev/dev-20180827 可查看config-client完整配置信息
3.3.2 ConfigClientApplication
- 浏览器中输入地址:http://localhost:8770/readFooProp
- 预期:读取的是:https://github.com/andyChenHuaYing/spring-cloud-demo/config-repository/config-client-dev.properties 文件中
foo=config-client-dev value
- 实际返回结果:
3.3.3 ConfigAnotherClientApplication
- 浏览器中输入地址:http://localhost:8770/readFooProp
- 预期:读取的是:https://github.com/andyChenHuaYing/spring-cloud-demo/config-repository/config-another-client-test.properties 文件中
foo=config-another-client foo value.
- 实际返回结果:
4. 思考
- 配置中心如何做高可用
- 远程仓库的配置修改如何实时同步?
- 如何简化或者说是封装配置项,让实际开发中只关注使用某具体配置项即可
- 配置信息安全性如何保证,用户名密码?安全传输?鉴权?应用白名单?
- 支持几种远程仓库形式?如SVN,本地或者其他?
- 有没有支持图形化动态修改配置项值的组件,方便配置项统一管理
- 如何将多套环境区分开,如dev、qa、线上,并且结合运维手段实现代码发布到不同环境时,环境自动切换
5. 补充
5.1 资料
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_client.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_server.html