Spring Cloud 覆写远端的配置属性实例详解
作者:Aoho 发布时间:2022-09-10 22:06:23
应用的配置源通常都是远端的Config Server服务器,默认情况下,本地的配置优先级低于远端配置仓库。如果想实现本地应用的系统变量和config文件覆盖远端仓库中的属性值,可以通过如下设置:
spring:
cloud:
config:
allowOverride: true
overrideNone: true
overrideSystemProperties: false
overrideNone:当allowOverride为true时,overrideNone设置为true,外部的配置优先级更低,而且不能覆盖任何存在的属性源。默认为false
allowOverride:标识overrideSystemProperties属性是否启用。默认为true,设置为false意为禁止用户的设置
overrideSystemProperties:用来标识外部配置是否能够覆盖系统属性,默认为true
客户端通过如上配置,可以实现本地配置优先级更高,且不能被覆盖。由于我们基于的Spring Cloud
当前版本是 Edgware.RELEASE
,上面的设置并不能起作用,而是使用了 PropertySourceBootstrapProperties
中的默认值。具体情况见issue: https://github.com/spring-cloud/spring-cloud-commons/pull/250 ,我们在下面分析时会讲到具体的bug源。
源码分析
ConfigServicePropertySourceLocator
覆写远端的配置属性归根结底与客户端的启动时获取配置有关,在获取到配置之后如何处理?我们看一下spring cloud config中的资源获取类 ConfigServicePropertySourceLocator 的类图。
ConfigServicePropertySourceLocator 实质是一个属性资源 * ,其主要方法是 locate(Environment environment) 。首先用当前运行应用的环境的application、profile和label替换configClientProperties中的占位符并初始化RestTemplate,然后遍历labels数组直到获取到有效的配置信息,最后还会根据是否快速失败进行重试。主要流程如下:
locate(Environment environment) 调用 getRemoteEnvironment(restTemplate, properties, label, state) 方法通过http的方式获取远程服务器上的配置数据。实现也很简单,显示替换请求路径path中占位符,然后进行头部headers组装,组装好了就可以发送请求,最后返回结果。
在上面的实现中,我们看到获取到的配置信息存放在 CompositePropertySource ,那是如何使用它的呢?这边补充另一个重要的类是PropertySourceBootstrapConfiguration,它实现了ApplicationContextInitializer接口,该接口会在应用上下文刷新之前 refresh() 被回调,从而执行初始化操作,应用启动后的调用栈如下:
SpringApplicationBuilder.run() -> SpringApplication.run() -> SpringApplication.createAndRefreshContext() -> SpringApplication.applyInitializers() -> PropertySourceBootstrapConfiguration.initialize()
PropertySourceBootstrapConfiguration
而上述 ConfigServicePropertySourceLocator 的locate方法会在initialize中被调用,从而保证上下文在刷新之前能够拿到必要的配置信息。具体看一下initialize方法:
public class PropertySourceBootstrapConfigurationimplements
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
private int order = Ordered.HIGHEST_PRECEDENCE + 10;
@Autowired(required = false)
private List<PropertySourceLocator> propertySourceLocators = new ArrayList<>();
@Override
public void initialize(ConfigurableApplicationContext applicationContext){
CompositePropertySource composite = new CompositePropertySource(
BOOTSTRAP_PROPERTY_SOURCE_NAME);
//对propertySourceLocators数组进行排序,根据默认的AnnotationAwareOrderComparator
AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
boolean empty = true;
//获取运行的环境上下文
ConfigurableEnvironment environment = applicationContext.getEnvironment();
for (PropertySourceLocator locator : this.propertySourceLocators) {
//遍历this.propertySourceLocators
PropertySource<?> source = null;
source = locator.locate(environment);
if (source == null) {
continue;
}
logger.info("Located property source: " + source);
//将source添加到PropertySource的链表中
composite.addPropertySource(source);
empty = false;
}
//只有source不为空的情况,才会设置到environment中
if (!empty) {
//返回Environment的可变形式,可进行的操作如addFirst、addLast
MutablePropertySources propertySources = environment.getPropertySources();
String logConfig = environment.resolvePlaceholders("${logging.config:}");
LogFile logFile = LogFile.get(environment);
if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
//移除bootstrapProperties
propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME);
}
//根据config server覆写的规则,设置propertySources
insertPropertySources(propertySources, composite);
reinitializeLoggingSystem(environment, logConfig, logFile);
setLogLevels(environment);
//处理多个active profiles的配置信息
handleIncludedProfiles(environment);
}
}
//...
}
下面我们看一下,在 initialize 方法中进行了哪些操作。
根据默认的 AnnotationAwareOrderComparator 排序规则对propertySourceLocators数组进行排序
获取运行的环境上下文ConfigurableEnvironment
遍历propertySourceLocators时
调用 locate 方法,传入获取的上下文environment
将source添加到PropertySource的链表中
设置source是否为空的标识标量empty
source不为空的情况,才会设置到environment中
返回Environment的可变形式,可进行的操作如addFirst、addLast
移除propertySources中的bootstrapProperties
根据config server覆写的规则,设置propertySources
处理多个active profiles的配置信息
初始化方法 initialize 处理时,先将所有PropertySourceLocator类型的对象的 locate 方法遍历,然后将各种方式得到的属性值放到CompositePropertySource中,最后调用 insertPropertySources(propertySources, composite) 方法设置到Environment中。Spring Cloud Context中提供了覆写远端属性的 PropertySourceBootstrapProperties ,利用该配置类进行判断属性源的优先级。
private void insertPropertySources(MutablePropertySources propertySources,
CompositePropertySource composite) {
MutablePropertySources incoming = new MutablePropertySources();
incoming.addFirst(composite);
PropertySourceBootstrapProperties remoteProperties = new PropertySourceBootstrapProperties();
new RelaxedDataBinder(remoteProperties, "spring.cloud.config")
.bind(new PropertySourcesPropertyValues(incoming));
//如果不允许本地覆写
if (!remoteProperties.isAllowOverride() || (!remoteProperties.isOverrideNone()
&& remoteProperties.isOverrideSystemProperties())) {
propertySources.addFirst(composite);
return;
}
//overrideNone为true,外部配置优先级最低
if (remoteProperties.isOverrideNone()) {
propertySources.addLast(composite);
return;
}
if (propertySources
.contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
//根据overrideSystemProperties,设置外部配置的优先级
if (!remoteProperties.isOverrideSystemProperties()) {
propertySources.addAfter(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
composite);
}
else {
propertySources.addBefore(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
composite);
}
}
else {
propertySources.addLast(composite);
}
}
上述实现主要是根据 PropertySourceBootstrapProperties 中的属性,调整多个配置源的优先级。从其实现可以看到 PropertySourceBootstrapProperties 对象的是被直接初始化,使用的是默认的属性值而并未注入我们在配置文件中设置的。
修复后的实现:
@Autowired(required = false)
private PropertySourceBootstrapProperties remotePropertiesForOverriding;
@Override
public int getOrder(){
return this.order;
private void insertPropertySources(MutablePropertySources propertySources,
CompositePropertySource composite) {
MutablePropertySources incoming = new MutablePropertySources();
incoming.addFirst(composite);
PropertySourceBootstrapProperties remoteProperties = remotePropertiesForOverriding == null
? new PropertySourceBootstrapProperties()
: remotePropertiesForOverriding;
总结
以上所述是小编给大家介绍的Spring Cloud 覆写远端的配置属性实例详解网站的支持!
来源:http://blueskykong.com/2018/01/23/config-server2


猜你喜欢
- 本文实例讲述了C#判断一个矩阵是否为对称矩阵及反称矩阵的方法。分享给大家供大家参考。具体如下:1.判断对称矩阵对任意i和j,有a[i,j]=
- 一、题目描述二、思路语法基础:StringBuilder 类似列表,可以更改元素。package Practice;public class
- 本文将是JVM 性能优化系列的第二篇文章(第一篇:传送门),Java 编译器将是本文讨论的核心内容。本文中,作者(Eva Andreasso
- 参数传递即将参数传输到程序后台中,后台可能做一些处理,然后再将内容存入数据库之类嗒!参数传递的方法较多,一一说明如下。1、Action中直接
- Android ListView的Item点击效果的定制
- TCP和UDP在网络传输中非常重要,在Android开发中同样重要。首先我们来看一下什么是TCP和UDP。什么是TCP?TCP:Transm
- SpringBoot中的SpringMVC配置功能都是在WebMvcAutoConfiguration类中,xxxxAutoConfigur
- 1. 启用AOPa. 在类上添加@Aspect注解b. 注入该类, 可以使用@Component进行注入到Spring容器中2. 通过Poi
- 本文实例为大家分享了DrawerLayout和触摸事件分发实现抽屉侧滑效果的具体代码,供大家参考,具体内容如下效果展示 还是看代码实在,直接
- 调用示例: 执行效果: 2.实现代码:/// <summary> 2 &
- 关于Unity的换装,网上有几篇文章,我之前也简单的描述过实现。不过那个时候只是粗略的试验了下。今天好好梳理了下代码。先上代码(自己的游戏项
- Android Support Annotations &
- 一、Mybatis1、mybatis-config.xml<?xml version="1.0" encoding
- using System;using System.Collections.Generic;using System.Globalizati
- 前言本节将介绍如何设置和使用MongoDB 驱动程序,通过 java实现与MongoDB服务端的通信功能,用户可以在此基础上进行各种Java
- 本文实例中的自定义类PictureBox继承于UserControl,最终实现简单的分屏功能。分享给大家供大家参考之用。具体实现代码如下:p
- using System;using System.Collections.Generic;using System.Text;using
- 一、事务的基本原理Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的。对于纯JDBC操
- public partial class MonthCalendarForm : Form{ public MonthCalen
- 仅做学习交流,如有侵犯联系必删。前言一篇酷狗app安卓逆向的文章,难度适中。样本: 酷狗app v10.8.8工具: jadx、Pixel3