SpringCloud的Archaius - 动态管理属性配置

时间:2023-03-09 04:03:08
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等)