在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. Android测试之Keycode

    问题: 昨天做测试Case,发现一个网游APK运行界面,uiautomator无法捕捉获取. 因而输入的时候只得运用(dut.onclick(int a, int y))坐标点击的方法来输入用户名和密 ...

  2. ubuntu BCM43142 lenovo网卡驱动安装(Broadcom)

    ubuntu13.10 lenovo网卡驱动安装(Broadcom)   ubuntu当月刚出了13.10版本,在升级了操作系统版本后无法搜索无线热点,联想官网提供的驱动下载都是为windows准备的 ...

  3. MYSQL数据库迁移总结

    Auth: JinDate: 2014-04-03 前端操作,后续测试对外开放不在本文 准备阶段操作阶段后续阶段 需求:phpcms和anquanzuo在一个mysql实例中anquanzuo有一张视 ...

  4. WPF Interaction框架简介(一)——Behavior

    在WPF 4.0中,引入了一个比较实用的库——Interactions,这个库主要是通过附加属性来对UI控件注入一些新的功能,除了内置了一系列比较好用的功能外,还提供了比较良好的扩展接口.本文这里简单 ...

  5. Linux PHP 编译参数详解(二)

    对于喜欢玩开源软件的童鞋么,都喜欢自己编译安装程序,本文说明下如何编译安装php的详细参数. 示例: ./configure \ --prefix=/usr/local/php --with-zlib ...

  6. for of 与 for in的区别2

    遍历数组通常使用for循环,ES5的话也可以使用forEach,ES5具有遍历数组功能的还有map.filter.some.every.reduce.reduceRight等,只不过他们的返回结果不一 ...

  7. 微服务:spring-cloud-archaius 起步

    原文:http://blog.csdn.net/qq_18675693/article/details/53337941 微服务:spring-cloud-archaius 起步 原创 2016年11 ...

  8. java Servlet Filter 拦截Ajax请求,统一处理session超时的问题

    后台增加filter,注意不要把druid也屏蔽了 import java.io.IOException; import javax.servlet.Filter; import javax.serv ...

  9. hive如何使用中文查询条件

    直接在hql中使用中文会报错:org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.lang.RuntimeExceptio ...

  10. java界面编程(3) ------ 控制布局

    本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 在java 中,组件放置在窗口上的方式可能与其它的GUI系统都不同样.首先,它全 ...