2016-03-01 129 views
0

我將Hystrix集成到我現有的項目中,我想從XML文件讀取配置值,而不是使用配置管理器提供配置屬性。在xml文件中更新值時,我希望Hystrix配置在運行時更新。Netflix Archaius動態配置

這是我下面的指南:

PolledConfigurationSource source = ... 
AbstractPollingScheduler scheduler = ... 
DynamicConfiguration configuration = new DynamicConfiguration(source, scheduler); 
ConfigurationManager.install(configuration); 

我怎麼一點PolledConfigurationSource到一個XML文件中讀取: https://github.com/Netflix/archaius/wiki/Users-Guide

我到目前爲止,我可以使用PolledConfigurationSource和下面的代碼理解固定時間間隔後的屬性?

回答

1

下面的代碼的伎倆,我

private void initializeConfiguration() { 

    // FixedDelayPollingScheduler is initialized with default system 
    // settings 
    // Fixed delay in milliseconds between two reads of the configuration 
    // URLs 
    // archaius.fixedDelayPollingScheduler.delayMills = 60000 
    // Initial delay in milliseconds of reading from the configuration 
    // source 
    // archaius.fixedDelayPollingScheduler.initialDelayMills = 30000 
    AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(); 

    // Configuration source that brings dynamic changes to the configuration 
    // via polling 
    PolledConfigurationSource source = new XMLPolledConfigurationSource(); 

    // Configuration that polls a PolledConfigurationSource according to the 
    // schedule set by a scheduler 
    DynamicConfiguration configuration = new DynamicConfiguration(source, scheduler); 

    ConfigurationManager.install(configuration); 

    // Registering configuration with an MBean and will be accessible for 
    // read and update via JConsole 
    ConfigJMXManager.registerConfigMbean(configuration); 
} 

XMLPolledConfigurationSource源代碼

public class XMLPolledConfigurationSource implements PolledConfigurationSource { 

@SuppressWarnings("static-access") 
@Override 
public PollResult poll(boolean inital, Object checkPoint) throws Exception { 
    PollResult pollResult = null; 
    Map<String, Object> map = new HashMap<>(); 
    // Code to read content from the resource 
    return pollResult.createFull(map); 
    } 
} 
-1

您是否嘗試過按照文檔設置此係統變量? -Darchaius.configurationSource.additionalUrls=file:///apps/myapp/application.xml 通過文件,我指的是入門網頁https://github.com/Netflix/archaius/wiki/Getting-Started

+0

嗯,我怎麼能設置-Darchaius .configurationSource.additionalUrls到src/main/resource文件夾中的xml文件? – yousafsajjad

+0

除此之外,有人能夠使用PolledConfigurationSource嗎? 正如指南中所建議的那樣,我將擴展PolledConfigurationSource類並使用StAX解析器從文件中讀取內容,將它們放入HashMap中並設置PollResult.createFull(map); – yousafsajjad

+0

當您運行java應用程序並僅使用文件名時,將它作爲參數傳遞,它將在類路徑中解析它的路徑...如果它是自行執行的jar,您可以這樣做:java -jar myapp.jar -Darchaius ... = config.xml'。根據文檔 – Filip