在这篇文章中,我们将讨论Archaius,一个非常酷且易于使用的Netflix配置管理工具。

通常我们都是如何读取配置变量的呢?

一种是使用System.getProperty()方法获得JVM系统属性。例如下面这样:

String prop = System.getProperty("myProperty");
int x = DEFAULT_VALUE;
try {x = Integer.parseInt(prop);
} catch (NumberFormatException e) {// handle format issues
}
myMethod(x);

您还可以使用Spring读取属性文件 。或者你的数据库中有一个简单的键/值表,你可以从那里读取一些属性。又或者您从外部REST端点获取它们。除此之外还可以从其他类型的键/值存储中获取,比如Redis或Memcached。

无论情况如何,您的配置变量可能来自许多不同的来源,特别是如果您的应用程序使用多个,这可能会变得难以维护。另一个重要的问题,您不希望每次更改其中一个属性的值时需要重新部署,特别是在持续集成的时候。

为解决这些问题,Netflix提出了一个开源的解决方案:Archaius。Archaius是Apache公共配置库的扩展, 它允许您从多个动态源中检索属性,并且它解决了前面提到的所有问题(异构的属性源,运行时更改等)。

我们先从用Archaius读取属性文件最简单的示例开始。

public class ApplicationConfig {public String getStringProperty(String key, String defaultValue) {final DynamicStringProperty property = DynamicPropertyFactory.getInstance().getStringProperty(key,defaultValue);return property.get();}
}public class ApplicationConfigTest {private ApplicationConfig appConfig = new ApplicationConfig();@Testpublic void shouldRetrieveThePropertyByKey() {String property = appConfig.getStringProperty("hello.world.message", "default message");assertThat(property, is("Hello Archaius World!"));}@Testpublic void shouldRetrieveDefaultValueWhenKeyIsNotPresent() {String property = appConfig.getStringProperty("some.key", "default message");assertThat(property, is("default message"));}
}

该代码是读取类路径中某处的“config.properties”文件。请注意,您不需要告诉Archaius在哪里找到您的属性文件,因为他要查找的默认名称是“config.properties”。

如果您不想或不能将属性文件命名为“config.property”,该怎么办?在这种情况下,您需要告诉Archaius在哪里查找此文件。您可以轻松地更改系统属性’archaius.configurationSource.defaultFileName’,在启动应用程序时将其作为参数传递给vm:

java ... -Darchaius.configurationSource.defaultFileName=customName.properties

或者写在代码本身中:

public class ApplicationConfig {static {System.setProperty("archaius.configurationSource.defaultFileName", "customConfig.properties");}public String getStringProperty(String key, String defaultValue) {final DynamicStringProperty property = DynamicPropertyFactory.getInstance().getStringProperty(key,defaultValue);return property.get();}
}

现在,如果你想读几个属性文件怎么办?您可以从首先加载的默认文件开始,轻松定义属性文件链及其加载顺序。从那里,您可以使用键“@ next = nextFile.properties”指定一个特殊属性来告诉Archaius哪个是应该加载的下一个文件。

在我们的示例中,我们可以在“customConfig.properties”文件中添加以下行:

@next=secondConfig.properties

并将相应的“secondConfig.properties”添加到我们的resources文件夹中,其中包含以下内容:

cascade.property=cascade value

我们可以通过在ApplicationConfigTest类中添加以下测试来验证:

@Test
public void shouldReadCascadeConfigurationFiles() {String property = appConfig.getStringProperty("cascade.property", "not found");assertThat(property, is("cascade value"));
}

请注意,我们从新文件中获取属性,而不对ApplicationConfig类进行任何其他更改。从客户的角度来看,这是完全透明的。

到目前为止,我们一直在从不同的属性文件中读取属性,但如果您想从不同的源读取它们会怎么样?在最常见的情况下,您可以通过实现“com.netflix.config.PolledConfigurationSource”来编写自己的逻辑。如果新源是可以通过JDBC访问的数据库,那么Archaius已经提供了可以使用的“JDBCConfigurationSource”。您只需要告诉他应该使用什么查询来获取属性以及哪些列表示属性键和属性值。

我们的例子如下:

@Component
public class ApplicationConfig {static {System.setProperty("archaius.configurationSource.defaultFileName", "customConfig.properties");}private final DataSource dataSource;@Autowiredpublic ApplicationConfig(DataSource dataSource) {this.dataSource = dataSource;installJdbcSource();}public String getStringProperty(String key, String defaultValue) {final DynamicStringProperty property = DynamicPropertyFactory.getInstance().getStringProperty(key,defaultValue);return property.get();}private DynamicConfiguration installJdbcSource() {if (!isConfigurationInstalled()) {PolledConfigurationSource source = new JDBCConfigurationSource(dataSource,"select distinct property_key, property_value from properties", "property_key", "property_value");DynamicConfiguration configuration = new DynamicConfiguration(source,new FixedDelayPollingScheduler(0, 10000, true));ConfigurationManager.install(configuration);return configuration;}return null;}
}

我们使用Spring来自动装配数据源,该数据源将使用具有简单键/值表的基于内存的H2数据库。注意我们是如何创建一个新的 PolledConfigurationSource的,使用Archaius已经提供的JDBCConfigurationSource,然后我们注册使用新的配置 ConfigurationManager。执行此操作后,我们可以从数据库中获取任何属性,就像我们对属性文件所做的那样(即使用 DynamicPropertyFactory)。

我们现在可以添加几个测试类来确保我们实际上从数据库中读取属性,并且我们可以更新它们的值并查看动态配置中反映的更改。

@Test
public void shouldRetrievePropertyFromDB() {String property = appConfig.getStringProperty("db.property", "default message");assertThat(property, is("this is a db property"));
}@Test
public void shouldReadTheNewValueAfterTheSpecifiedDelay() throws InterruptedException, SQLException {template.update("update properties set property_value = 'changed value' where property_key = 'db.property'");String propertyValue = (String) template.queryForObject("select property_value from properties where property_key = 'db.property'", java.lang.String.class);System.out.println(propertyValue);String property = appConfig.getStringProperty("db.property", "default message");// We updated the value on the DB but Archaius polls for changes every 10000// milliseconds so it still sees the old valueassertThat(property, is("this is a db property"));Thread.sleep(30000);property = appConfig.getStringProperty("db.property", "default message");assertThat(property, is("changed value"));
}

Archaius提供的另一个非常酷的功能是可以通过JMX 将我们的配置注册为 MBean。我们可以默认设置系统属性 archaius.dynamicPropertyFactory.registerConfigWithJMX = true或使用ConfigJMXManager.registerConfigMbean(config)进行编程。

执行此操作后,我们可以通过JConsole连接,不仅可以获取所有属性的值,还可以更新它们并查看它们在Archaius中反映的新值。例如,这将允许我们在运行时更改属性文件中静态定义的属性值,而无需服务器推送。我们可以稍微修改一下ApplicationConfig类来添加一个main方法,该方法将持续运行打印不同属性的值,这样将允许我们在JConsole中使用。

public class ApplicationConfig extends Thread {private final DataSource dataSource;@Autowiredpublic ApplicationConfig(DataSource dataSource) {this.dataSource = dataSource;cascadeDefaultConfiguration();DynamicConfiguration jdbcSource = installJdbcSource();registerMBean(jdbcSource);}public String getStringProperty(String key, String defaultValue) {final DynamicStringProperty property = DynamicPropertyFactory.getInstance().getStringProperty(key,defaultValue);return property.get();}@Overridepublic void run() {while (true) {try {sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}}}private void registerMBean(DynamicConfiguration jdbcSource) {setDaemon(false);ConfigJMXManager.registerConfigMbean(jdbcSource);}public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("archaiusContext.xml");ApplicationConfig applicationConfig = (ApplicationConfig) applicationContext.getBean("applicationConfig");applicationConfig.start();while (true) {try {System.out.println(applicationConfig.getStringProperty("hello.world.message", "default message"));System.out.println(applicationConfig.getStringProperty("cascade.property", "default message"));System.out.println(applicationConfig.getStringProperty("db.property", "default message"));sleep(3000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

您还可以使用Archaius进行更多操作,例如每次属性更改时的回调,与Zookeeper或其他服务的集成。您可以在GitHub上找到本文中显示的所有代码。

使用Netflix Archaius进行配置管理相关推荐

  1. Netflix Archaius 分布式配置管理依赖构件

    Archaius 配置管理API,包含一系列配置管理API,提供动态类型化属性.线程安全配置操作.轮询框架.回调机制等功能. 概述 archaius是Netflix公司开源项目之一,基于java的配置 ...

  2. java B2B2C 源码多租户电子商城系统-Spring Cloud整合Netflix Archaius介绍

    1.概述 Netflix Archaius 是一个功能强大的配置管理库.它是一个可用于从许多不同来源收集配置属性的框架,提供对配置信息的快速及线程安全访问. 需要JAVA Spring Cloud大型 ...

  3. Spring Cloudt整合Netflix Archaius介绍

    1.概述 Netflix Archaius 是一个功能强大的配置管理库.它是一个可用于从许多不同来源收集配置属性的框架,提供对配置信息的快速及线程安全访问. 除此之外,Archaius允许属性在运行时 ...

  4. Netflix Archaius用于物业管理–基础知识

    Netflix Archaius提供了一组精巧的功能,可将动态属性加载到应用程序中. 这篇博客文章只是我所了解的Archaius范围的文档,比我在这里所记录的内容要多得多,但这应该提供一个很好的开始: ...

  5. Spring项目中的Netflix Archaius属性

    Archaius基础 Netflix Archaius是用于管理应用程序配置的库. 考虑一个属性文件" sample.properties",其中包含一个名为" mypr ...

  6. 转载的spring cloud的全家桶,有空学习下

    Spring Cloud Config:配置管理开发工具包,可以让你把配置放到远程服务器,目前支持本地存储.Git以及Subversion. Spring Cloud Bus:事件.消息总线,用于在集 ...

  7. 基于Spring Boot+Cloud构建微云架构

    链接:my.oschina.net/u/3636867/blog/1802517 前言 首先,最想说的是,当你要学习一套最新的技术时,官网的英文文档是学习的最佳渠道.因为网上流传的多数资料是官网翻译而 ...

  8. Spring Cloud 学习资料收集

    导读 关于Spring Cloud 去年开始逐渐多的出现在我的视线中,随着微服务这个词越来越热,我们或多或少的都听说过这个词,我们可以将Spring Cloud 看做是java 中Spring 为我们 ...

  9. 基于Spring Boot和Spring Cloud实现微服务架构学习--转

    原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...

  10. 基于 Spring Boot 和 Spring Cloud 实现微服务架构

    前言 首先,最想说的是,当你要学习一套最新的技术时,官网的英文文档是学习的最佳渠道.因为网上流传的多数资料是官网翻译而来,很多描述的重点也都偏向于作者自身碰到的问题,这样就很容易让你理解和操作出现偏差 ...

最新文章

  1. 【组队学习】孙健坤:基于Python的会员数据化运营
  2. C语言字符数组与字符串的使用及加结束符‘\0‘的问题
  3. 神经网络与机器学习 笔记—基本知识点(下)
  4. 【Android工具】Yandex!可以安装PCchrome插件的手机浏览器!更新网页剪辑插件测试情况...
  5. 关于开发工具环境准备事项作为故事来处理的对话
  6. 用存储过程还原数据库
  7. JDBC预状态通道设置时间格式的问题
  8. python跟谁学_Python 应该怎么学?
  9. CocosStudio的节点如何使用自定义shader
  10. MySQL的复制:MySQL系列之十三
  11. php++数据库备份,php实现数据库备份
  12. quartus仿真20:模8的二进制计数器
  13. GoogLeNet 之 Inception v1 v2 v3 v4
  14. 微信小程序-colorUI组件库
  15. 数值分析实验报告 matlab,数值分析方法与实验基于MATLAB实现
  16. VMware tools 安装失败
  17. Vue使用debugger
  18. iOS数据持久化设计
  19. 海马玩安卓模拟器linux,海马玩模拟器下载安装_海马玩模拟器Droid4X官方下载「手游模拟器」-太平洋下载中心...
  20. matlab plot函数详解

热门文章

  1. Tasker 一个配置实现微信朗读,微信消息播报+基础版的防撤回
  2. 对傅里叶变换FFT性质的理解 平移 旋转 缩放
  3. 快速删除excel中的空行
  4. JAVA购物网站商城系统毕业设计 开题报告
  5. Linux之postfix邮件服务器搭建
  6. ThingJS图表整合
  7. 从零开始学习ThingJS之创建/销毁物体
  8. IOS开源项目Telegram初试
  9. 空气阻力对乒乓球运动轨迹的影响
  10. 人力资源管理系统课程设计