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等)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM