spring cloud config服务器
Spring Cloud Config提供了一种在分布式系统中外部化配置服务器和客户端的支持。配置服务器有一个中心位置,管理所有环境下的应用的外部属性。客户端和服务器映射到相同Spring Eventment 和 PropertySrouce抽象的概念,所以非常适合Spring应用,但也可以在任何语言开发的任何应用中使用。在一个应用从开发、测试到生产的过程中,你可以分别地管理开发、测试、生产环境的配置,并且在迁移的时候获取相应的配置来运行。
准备工作
准备几个配置文件,命名规范为 项目名称-环境名称.yml。本文在git仓库:https://github.com/xuwenjin中,新建目录config-repo-xwj,创建以下几个文件:
每个文件内容如下:
application.yml
profile: profile-default
config-client.yml
profile: config-client
config-client-dev.yml
profile: dev
config-client-test.yml
profile: test
代码示例
创建一个Maven项目,在pom.xml文件中添加如下内容:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<!-- 解决启动报Caused by: java.lang.ClassNotFoundException: org.eclipse.jgit.api.TransportConfigCallback -->
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.9.0.201710071750-r</version>
</dependency>
</dependencies>
启动类:
@SpringBootApplication
@EnableConfigServer // 通过@EnableConfigServer注解激活配置服务
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
} }
配置文件:application.yml
server:
port: spring:
application:
name: config-server #应用程序名称
cloud:
config:
server:
git:
uri: https://github.com/xuwenjin/config-repo-xwj #git上,配置文件地址
这样,一个Config Server就完成了。
测试工作
规则中的参数含义如下:
{application}
映射到客户端的“spring.application.name”(项目名称){profile}
映射到客户端上的“spring.profiles.active”(环境名称){label}
这是一个服务器端功能,标记“版本”的配置文件集(在git中,相当于分支名)
启动服务,也可以看到匹配规则:
先试下 {application}/{profile}[/{label}] 这个规则,请求路径是 http://localhost:18083/config-client/dev/master,返回了所有配置文件信息:
另外四种规则差不多,可使用以下路径来访问config-client-dev.yml文件,并直接返回其中的内容:
http://localhost:18083/config-client-dev.yml
http://localhost:18083/master/config-client-dev.yml
http://localhost:18083/config-client-dev.properties
http://localhost:18083/master/config-client-dev.properties
如果想要访问config-client-test.yml文件,只需要将上面的 dev 换成 test 就行。
但是如果匹配不上呢?结果如下:
可以看到,当匹配不上时,会默认读取application.yml文件。
Config Server服务端是支持热加载的,即不重启服务的情况下更改配置文件信息,是可以直接读取的
源码分析
当发送请求时(如:http://localhost:18083/master/config-client-dev.yml ),会在 EnvironmentController 中进行匹配。如下:
@RestController
@RequestMapping(method = RequestMethod.GET, path = "${spring.cloud.config.server.prefix:}")
public class EnvironmentController { private EnvironmentRepository repository; // ... @RequestMapping({ "/{label}/{name}-{profiles}.yml", "/{label}/{name}-{profiles}.yaml" })
public ResponseEntity<String> labelledYaml(@PathVariable String name, @PathVariable String profiles,
@PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws Exception {
// 校验profiles是否含义"-",如果有则抛出异常
validateProfiles(profiles);
// 获取环境信息,即远程配置文件数据
Environment environment = labelled(name, profiles, label);
// 将environment对象转为Map
Map<String, Object> result = convertToMap(environment);
if (this.stripDocument && result.size() == && result.keySet().iterator().next().equals("document")) {
Object value = result.get("document");
if (value instanceof Collection) {
return getSuccess(new Yaml().dumpAs(value, Tag.SEQ, FlowStyle.BLOCK));
} else {
return getSuccess(new Yaml().dumpAs(value, Tag.STR, FlowStyle.BLOCK));
}
}
String yaml = new Yaml().dumpAsMap(result); if (resolvePlaceholders) {
yaml = resolvePlaceholders(prepareEnvironment(environment), yaml);
} return getSuccess(yaml);
} // ... }
核心方法 labelled 中,会调用最核心的类 NativeEnvironmentRepositor 的 findOne 方法:
@RequestMapping("/{name}/{profiles}/{label:.*}")
public Environment labelled(@PathVariable String name, @PathVariable String profiles,
@PathVariable String label) {
if (name != null && name.contains("(_)")) {
// "(_)" is uncommon in a git repo name, but "/" cannot be matched
// by Spring MVC
name = name.replace("(_)", "/");
}
if (label != null && label.contains("(_)")) {
// "(_)" is uncommon in a git branch name, but "/" cannot be matched
// by Spring MVC
label = label.replace("(_)", "/");
}
Environment environment = this.repository.findOne(name, profiles, label);
return environment;
}
findOne方法会从远程git仓库中获取配置文件数据:
@Override
public Environment findOne(String config, String profile, String label) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(
PropertyPlaceholderAutoConfiguration.class);
ConfigurableEnvironment environment = getEnvironment(profile);
builder.environment(environment);
builder.web(false).bannerMode(Mode.OFF);
if (!logger.isDebugEnabled()) {
builder.logStartupInfo(false);
}
String[] args = getArgs(config, profile, label);
// 设置监听器
builder.application()
.setListeners(Arrays.asList(new ConfigFileApplicationListener()));
ConfigurableApplicationContext context = builder.run(args);
environment.getPropertySources().remove("profiles");
try {
return clean(new PassthruEnvironmentRepository(environment).findOne(config,
profile, label));
}
finally {
context.close();
}
}
获取的Environment对象,数据结构如下:
可以看到,配置文件信息,会放在 PropertySources 对象中~
spring cloud config服务器的更多相关文章
- Spring Cloud官方文档中文版-Spring Cloud Config(上)
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...
- 介绍一下Spring Cloud Config
Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持.使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring ...
- spring cloud config客户端
上篇介绍了spring cloud config服务器,本篇介绍客户端.客户端主要是从config服务器获取配置信息. 代码示例 首先创建一个Maven项目,在pom.xml文件中添加依赖: < ...
- Spring Cloud Config中文文档
https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...
- Spring Cloud官方文档中文版-Spring Cloud Config(上)-服务端(配置中心)
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...
- 9.Spring Cloud Config统一管理微服务配置
Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...
- Spring Cloud Config教程(四)快速开始
Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持.使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring ...
- Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...
- 搭建spring cloud config
很久没更新了,因为不是专职研究spring cloud,因此更新速度得看工作强度大不大,每天能抽出的时间不多,如果更新太慢了,并且有小伙伴看的话,请见谅了. Spring Cloud简介 Spring ...
随机推荐
- 小白的CTF学习之路2——二进制数据基础与运算(上)
今天的第二更,被我拖到了傍晚,嘿嘿,二进制这方面让本就数学不好的我很头疼,所以研究了一段时间 在学习之前我们先了解几个问题: 32位是几个字节? 01011100对于十进制是多少? 00001111向 ...
- 2019.02.17 spoj Query on a tree V(链分治)
传送门 题意简述: 给你一棵nnn个黑白点的树,初始全是黑点. 现在支持给一个点换颜色或者求整颗树中离某个点最近的白点跟这个点的距离. 思路: 考虑链分治维护答案,每个链顶用一个堆来维护答案,然后对于 ...
- Nginx+Django搭建
本机环境介绍 虚拟机操作系统版本如下 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) IP地址 ...
- 使用Phabricator进行代码审查
Pharicator 是FB的代码审查工具,主要开发者为Evan Priestley,是一个开源软件,可在Apache许可证第2版下作为自由软件分发.详细信息可查看官方文档.这里从应用的角度,一步一步 ...
- MySQL优化--NOT EXISTS和LEFT JOIN方式差异
在MySQL中,我们可以将NOT EXISTS语句转换为LEFT JOIN语句来进行优化,哪为什么会有性能提升呢? 使用NOT EXISTS方式SQL为: ) FROM t_monitor m WHE ...
- VS工具栏没有出现可用工具的情况
(1)没有切换到资源视图,打开具体的对话框. (2)如果你在调试状态,即使打开了具体的对话框,VS工具箱还是不会出现可用的控件的.所以不要在调试状态下添加控件.
- Codeforces Round #553 (Div. 2) C. Problem for Nazar 数学
题意:从奇数列 1 3 5 7 9 .... 偶数列2 4 6 8 10...分别轮流取 1 2 4 ....2^n 个数构成新数列 求新数列的区间和 (就一次询问) 思路:首先单次区间和就是一个简 ...
- fiddler电脑抓包和手机抓包
概述 以前听别人说抓包抓包的,听起来很神秘高大上的样子,想入门又不知道从何学起.今天偶然在工作中遇到了以下2个需求: 改线上的代码,特别是PC端js代码. 写了一个移动端页面,由于跨域,改了host地 ...
- 脚手架vue-cli系列二:vue-cli的工程模板与构建工具
上篇文章我们提到了vue-cli的工程模板.这里我们来详细的进行介绍. vue-cli提供的脚手架只是一个最基础的,也可以说是Vue团队认为的工程结构的一种最佳实践.对于初学者或者以前曾从事Angul ...
- Java 单元测试顺序执行
坑死我了,原来@Before会执行多次. 通过函数名可以实现顺序执行,执行顺序和函数的位置无关. import org.junit.Before; import org.junit.BeforeCla ...