Netflix Archaius动态配置 [英] Netflix Archaius Dynamic Configuration

查看:1047
本文介绍了Netflix Archaius动态配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am integrating Hystrix in to my existing project and I want to read the configuration values from an xml file instead of feeding the configuration properties using Configuration Manager. When the values are updated in the xml file I want Hystrix configuration to be updated at runtime.

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

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

I understand so far that I can use PolledConfigurationSource and the following code:

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

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

How do I point PolledConfigurationSource to an xml file to read the properties after a fixed time interval?

推荐答案

以下代码为我做了诀窍

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源代码

XMLPolledConfigurationSource source code

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);
  }
}

这篇关于Netflix Archaius动态配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆