在spring-cloud中使用了config-server之后,需要在client端加入bootstrap作为配置文件,其中通常包含如下:
spring.application.name=ms-asset
spring.cloud.config.label=master
spring.cloud.config.profile=test
spring.cloud.config.uri=http://ms-config-server-srv:8001/
但是这样的配置存在问题,因其固定了使用的配置环境和label,如采用Docker部署,则无法通过环境变量更改,通过阅读源码可知,Spring-cloud 通过两个JVM参数控制加载的配置文件。
-Dspring.cloud.config.label=master
-Dspring.cloud.config.profile=test
其中label表示分支,profile表示加载的配置环境。
其源码非常简单,在ConfigClientProperties中,如下所示:
public ConfigClientProperties override(
org.springframework.core.env.Environment environment) {
ConfigClientProperties override = new ConfigClientProperties();
BeanUtils.copyProperties(this, override);
override.setName(
environment.resolvePlaceholders("${" + ConfigClientProperties.PREFIX
+ ".name:${spring.application.name:application}}"));
if (environment.containsProperty(ConfigClientProperties.PREFIX + ".profile")) {
override.setProfile(
environment.getProperty(ConfigClientProperties.PREFIX + ".profile"));
}
if (environment.containsProperty(ConfigClientProperties.PREFIX + ".label")) {
override.setLabel(
environment.getProperty(ConfigClientProperties.PREFIX + ".label"));
}
return override;
}
可以看出,spring cloud仅支持profile和label这两个配置注入,在一般情况下足够用了。
另外存在一种修改spring-boot配置文件的方式,原理是监测spring初始化完成的事件,然后将环境变量注入配置中,可用于覆盖spring cloud config server的统一配置,但由于bootstrap处在加载链的最前端,在初始化之前即以完成的远程配置文件的加载,因此该方法无效,一并贴出,有缘人自取。
package com.example;

import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; @Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ConfigServicePropertyDeprioritizer
implements ApplicationListener<ApplicationPreparedEvent>
{
private static final String CONFIG_SOURCE = "bootstrap"; private static final String PRIORITY_SOURCE = "systemEnvironment"; @Override
public void onApplicationEvent(ApplicationPreparedEvent event)
{
ConfigurableEnvironment environment = event.getApplicationContext()
.getEnvironment();
MutablePropertySources sources = environment.getPropertySources();
PropertySource<?> bootstrap = findSourceToMove(sources); if (bootstrap != null)
{
sources.addAfter(PRIORITY_SOURCE, bootstrap);
}
} private PropertySource<?> findSourceToMove(MutablePropertySources sources)
{
boolean foundPrioritySource = false; for (PropertySource<?> source : sources)
{
if (PRIORITY_SOURCE.equals(source.getName()))
{
foundPrioritySource = true;
continue;
} if (CONFIG_SOURCE.equals(source.getName()))
{
// during bootstrapping, the "bootstrap" PropertySource
// is a simple MapPropertySource, which we don't want to
// use, as it's eventually removed. The real values will
// be in a CompositePropertySource
if (source instanceof CompositePropertySource)
{
return foundPrioritySource ? null : source;
}
}
} return null;
}
}
 
 
 
 

重写spring cloud config 本地bootstrap的更多相关文章

  1. spring cloud config的bootstrap.yml与application.proterties的区别

    bootstrap.yml  和application.yml  都可以用来配置参数 bootstrap.yml可以理解成系统级别的一些参数配置,这些参数一般是不会变动的 application.ym ...

  2. Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...

  3. Spring Cloud Config中文文档

    https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...

  4. spring cloud config配置

    参考: http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html http://www.ityouknow ...

  5. spring cloud config —— git配置管理

    目录 talk is cheep, show your the code Server端 pom.xml server的application.yml 配置文件 测试Server client端 po ...

  6. Spring Cloud Config入门(本地配置)

    spring cloud config 简介 Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持.使用Config Server,您可以在所有环境中管理应用程序的外 ...

  7. spring cloud --- config 配置中心 [本地、git获取配置文件]

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...

  8. Spring Cloud官方文档中文版-Spring Cloud Config(上)

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

  9. Spring Cloud Config - RSA简介以及使用RSA加密配置文件

    简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...

随机推荐

  1. Node.js+MySQL管理工作的详细信息所遇到的问题

    问题陈述: Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: '\xE8\xB4\xAD\xE7\x89\xA9' ...

  2. [漏洞检测]Proxpy Web Scan设计与实现(未完待续)

    Proxpy Web Scan设计与实现 1.简介:          Proxpy Web Scan是基于开源的python漏洞扫描框架wapiti改造的web漏洞扫描器,其主要解决以下几个问题而生 ...

  3. [典型漏洞分享]一个典型的XSS盲打漏洞可导致全网用户cookie被盗取

    偶平时在做安全测试时,一般是以发现问题为主,点到为止,但做安全的同学可能也遇到过这样的问题,当你尝试向开发的同学描述一个漏洞危害怎么怎么样的时候,双方经常会有一种鸡同鸭讲的感觉,甚至他们觉得我们在夸大 ...

  4. 为什么fis没有freemarker的解决方案啊?_前端吧_百度贴吧

    为什么fis没有freemarker的解决方案啊?_前端吧_百度贴吧 fis-plus:适用于PHP+Smarty后端选型jello:适用于Java+Velocity后端选型goiz:适用于go+ma ...

  5. HDU 4584 Building bridges (水题)

    Building bridges Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  6. (转)C++ STL中的vector的内存分配与释放

    C++ STL中的vector的内存分配与释放http://www.cnblogs.com/biyeymyhjob/archive/2012/09/12/2674004.html 1.vector的内 ...

  7. python笔记15-ini配置文件(configparser)

    前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser configPars ...

  8. 【iOS开发】canOpenURL: failed for URL

    控制台输出 如图是在我启动一个 Xcode 7 + iOS 9 的 App 之后,控制台的输出. 这在 Xcode 6.4 + iOS 8 时,是不会有的情况,原因是[为了强制增强数据访问安全, iO ...

  9. centOS6.5 Hadoop1.0.4安装

    前段时间去培训,按照教程装了一遍Hadoop.回来又重新装一次,捋下思路,加深理解. 基本配置如下,三个节点,一个namenode,两个datanode. Namenode 192.168.59.14 ...

  10. MySql Replication基本原理

    Replication的思想是将数据在集群的多个节点同步.备份,以提高集群数据的可用性(HA):Mysql使用Replication架构来实现上述目的,同时可以提升了集群整体的并发能力.5.6版本作为 ...