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

 2023-09-10 阅读 26 评论 0

摘要:参考链接:http://www.th7.cn/Program/java/201608/919853.shtml 一、Archaius是什么? Archaius用于动态管理属性配置文件。 参考自Getting-Started * 引入项目中*<dependency> <groupId>com.netflix.archaius</groupId> <artifactId>ar

参考链接: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形式的文件,多个文件用逗号分割。

docker环境变量?使用下面的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.Testpublic 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等)

vue router base?转载于:https://www.cnblogs.com/lexiaofei/p/7169483.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/32896.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息