Netflix Archaius动态配置

时间:2016-03-01 00:16:20

标签: java hystrix netflix-archaius

我正在将Hystrix集成到我现有的项目中,我希望从xml文件中读取配置值,而不是使用Configuration Manager提供配置属性。当在xml文件中更新值时,我希望在运行时更新Hystrix配置。

这是我所遵循的指南: https://github.com/Netflix/archaius/wiki/Users-Guide

到目前为止,我了解到我可以使用PolledConfigurationSource和以下代码:

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

如何在固定时间间隔后将PolledConfigurationSource指向xml文件以读取属性?

2 个答案:

答案 0 :(得分: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 :(得分:-1)

您是否尝试过根据文档设置此系统变量? -Darchaius.configurationSource.additionalUrls=file:///apps/myapp/application.xml 通过文档,我指的是入门网页https://github.com/Netflix/archaius/wiki/Getting-Started