我正在将 Hystrix 集成到现有项目中,并且希望从 xml 文件中读取配置值,而不是使用配置管理器提供配置属性。当 xml 文件中的值更新时,我希望 Hystrix 配置在运行时更新。
这是我遵循的指南:https://github.com/Netflix/archaius/wiki/Users-Guide https://github.com/Netflix/archaius/wiki/Users-Guide
到目前为止我明白我可以使用 PolledConfigurationSource 和以下代码:
PolledConfigurationSource source = ...
AbstractPollingScheduler scheduler = ...
DynamicConfiguration configuration = new DynamicConfiguration(source, scheduler);
ConfigurationManager.install(configuration);
如何将 PolledConfigurationSource 指向 xml 文件以在固定时间间隔后读取属性?
以下代码对我有用
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)