SpringCloud的Archaius - 动态管理属性配置
参考链接:http://www.th7.cn/Program/java/201608/919853.shtml
一、Archaius是什么?
Archaius用于动态管理属性配置文件。
参考自Getting-Started
* 引入项目中*
<dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.6.0</version>
</dependency>
使用本地配置文件作为配置源
默认的,Archaius将查找classpath下名为config.properties文件并读取,这个配置文件可以包含在一个jar包的根路径下。另外,你可以使用属性archaius.configurationSource.additionalUrls来包含url形式的文件,多个文件用逗号分割。
使用下面的API在程序中得到你需要的属性
// create a property whose value is type long and use 1000 as the default
// if the property is not defined
DynamicLongProperty timeToWait = DynamicPropertyFactory.getInstance().getLongProperty("lock.waitTime", 1000);
// ...
ReentrantLock lock = ...;
// ...
lock.tryLock(timeToWait.get(), TimeUnit.MILLISECONDS); // timeToWait.get() returns up-to-date value of the property
默认的:Archaius会每分钟去重新加载下属性配置,多属性文件时,最后读到的属性会覆盖前面相同的属性
列出我们可以修改的一些系统属性
Operation HTTP action Notes
archaius.configurationSource.defaultFileName 指定Archaius默认加载的配置源属性文件名,默认:classpath:config.properties config.properties
archaius.fixedDelayPollingScheduler.initialDelayMills 延迟加载,默认30秒 30000
archaius.fixedDelayPollingScheduler.delayMills 两次属性读取时间间隔,默认1分钟 60000
高级使用:自定义configuration source和polling scheduler,即自己设计动态属性配置方案。
二、一个简单的例子
1. 获取配置源
public class DynamicConfigurationSource implements PolledConfigurationSource {
@Override
public PollResult poll(boolean initial,Object checkPoint) throws Exception {
Map<String,Object> map = new HashMap<>();
map.put("test",UUID.randomUUID().toString());
return PollResult.createFull(map);
}
}
2. 定义调度器
AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(2000,2000,false);
3. 定义动态配置
DynamicConfiguration configuration = new DynamicConfiguration(source,scheduler);
4.简单单元测试
@org.testng.annotations.Test
public void testArchaius() throws Exception {
PolledConfigurationSource source = new DynamicConfigurationSource();
AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(2000,2000,false);
DynamicConfiguration configuration = new DynamicConfiguration(source,scheduler);
ConfigurationManager.install(configuration);
final DynamicStringProperty stringProperty = DynamicPropertyFactory.getInstance().getStringProperty("test","nodata");
Helpers.subscribePrint(Observable.interval(1,TimeUnit.SECONDS).take(20).doOnNext(new Action1<Long>() {
@Override
public void call(Long aLong) {
System.out.println(stringProperty.get());
}
}),"test");
TimeUnit.MINUTES.sleep(1);
}
实现
1. 启动轮询任务
public synchronized void startPolling(PolledConfigurationSource source, AbstractPollingScheduler scheduler) {
this.scheduler = scheduler;
this.source = source;
init(source, scheduler);
scheduler.startPolling(source, this);
}
2.轮询的Runnable和初始化:实现是一致的
PollResult result = null;
try {
result = source.poll(false,getNextCheckPoint(checkPoint));
checkPoint = result.getCheckPoint();
fireEvent(EventType.POLL_SUCCESS, result, null);
} catch (Throwable e) {
log.error("Error getting result from polling source", e);
fireEvent(EventType.POLL_FAILURE, null, e);
return;
}
try {
populateProperties(result, config);
} catch (Throwable e) {
log.error("Error occured applying properties", e);
}
注意到,会调用source.poll方法,即PolledConfigurationSource的polled,我们实现的数据源接口,可以自定义数据源(jdbc,文件,scm等)
SpringCloud的Archaius - 动态管理属性配置的更多相关文章
- zookeeper 动态管理nginx配置
假设我们有一个场景,所有服务器共享同一份配置文件,我们肯定不可能单独手动维护每台服务器,这时可以利用zookeeper的配置管理功能. 环境:python + nginx + zookeeper 目的 ...
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...
- Linux网络属性配置命令和管理详解
一.Linux网络属性配置 1.Linux主机接入到网络方式 IP/NETMASK:实现本地网络通信 路由(网关):可以进行跨网络通信 DNS服务器地址:基于主机名的通信,Linux可以有三个DNS地 ...
- 【SpringCloud构建微服务系列】使用Spring Cloud Config统一管理服务配置
一.为什么要统一管理微服务配置 对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护 ...
- 白话SpringCloud | 第七章:分布式配置中心的使用
前言 介绍完服务的容错保护处理,接下来我们来了解下关于分布式配置中心的相关知识和使用.众所周知,随着项目的越来越多,日益庞大,每个子项目都会伴随着不同的配置项,于此也就多了很多的配置文件.倘若某些配置 ...
- SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- SpringCloud系列十:SpringCloudConfig 高级配置(密钥加密处理(JCE)、KeyStore 加密处理、SpringCloudConfig 高可用机制、SpringCloudBus 服务总线)
1.概念:SpringCloudConfig 高级配置 2.具体内容 在 SpringCloudConfig 之中考虑到所有配置文件都暴露在远程仓库之中的安全性问题,所以提供有安全访问的处理机制,这样 ...
- 【linux相识相知】网络属性配置
当我们拥有一个崭新的计算机的时候,第一步恐怕都是迫不及待的下载各种软件,看视频,听音乐等,这里的关键的一点是要有网络.现在的个人计算机大部分都是windows操作系统的,接入网络网络很简单,插上网线也 ...
- 基于SpringCloud的Microservices架构实战案例-配置文件属性内容加解密
使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些.打开application.properties或application.yml,比如mysq ...
随机推荐
- Java的大数计算BigNumber
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- IO查找文件复制到指定路径
public class IOTest { // 存储获取的文件绝对路径 private static List<String> list = new ArrayList<Strin ...
- mysql 数据库基本命令语句
mysql mariadb 客户端连接 mysql -uroot -p; 客户端退出exit 或 \q 显示所有数据库show databases;show schemas; 创建数据库create ...
- Samba原理和配置
Samba原理和配置 个人原创,转载请注明,否则追究法律责任. 一,原理及安装 1,Samba是在Linux和UNIX系统上实现在局域网上共享文件一种通信协议,它为局域网内的不同计算机之间提供文件等资 ...
- jmeter--简单的接口测试(GET/POST)
最近在学习接口测试,本文就简单的谈一谈对接口相关知识的理解. 一.什么是接口? 程序接口:由一套陈述.功能.选项.其它表达程序结构的形式.以及程序师使用的程序或者程序语言提供的数据组成(百度百科定义) ...
- JAVA 调用mysql存储过程
public class Test { //连接mysql数据库 public static final String DRIVER_CLASS = "com.mysql.jdbc.Driv ...
- three.js 实现全景以及优化(2)
继昨天全景实现后,再做了一个全景图切换实验; code:https://github.com/Thinkia/threejs_/blob/master/test/test1-panorama/inde ...
- 多个input连接在一起的时候如何实现输入一个数字跳入下一个
这个是页面内容 ,我分了12格子,作为一个12位的会员卡号的输入;其实就是12个input我把他们放在了一个div里面 这样配上背景图,看着是一个大的输入框. <div id="A ...
- Spark Kudu 结合
Kudu的背景 Hadoop中有很多组件,为了实现复杂的功能通常都是使用混合架构, Hbase:实现快速插入和修改,对大量的小规模查询也很迅速 HDFS/Parquet + Impala/Hive:对 ...
- 小白学PYTHON时最容易犯的6个错误,看看你遇到过几个
最近又在跟之前的同学一起学习python,一起进步,发现很多测试同学在初学python的时候很容易犯一些错误,特意总结了一下.其实这些错误不仅是在学python时会碰到,在学习其他语言的时候也同样会碰 ...