archaius是什么,能做什么?
archaius是Netflix公司开源项目之一,基于java的配置管理类库,主要用于多配置存储的动态获取。主要功能是对apache common configuration类库的扩展。在云平台开发中可以将其用作分布式配置管理依赖构件。同时,它有如下一些特性:
- 动态类型化属性
- 高效和线程安全的配置操作
- 配置改变时的回调机制
- jmx
- 组合配置(核心内容)
At the heart of Archaius is the concept of a Composite Configuration which can hold one or more Configurations. Each Configuration can be sourced from a Configuration Source such as: JDBC, REST, .properties file etc. Configuration Sources can optionally be polled at runtime for changes (In the above diagram, the Persisted DB Configuration Source; an RDBMS containing properties in a table, is polled every so often for changes).
The final value of a property is determined based on the top most Configuration that contains that property. i.e. If a property is present in multiple configurations, the actual value seen by the application will be the value that is present in the topmost slot in the hierarchy of Configurations. The hierarchy can be configured.
动态属性(Dynamic properties)
String prop = System.getProperty("myProperty");
int x = DEFAULT_VALUE;
try {
x = Integer.parseInt(prop);
} catch (NumberFormatException e) {
// handle format issues
}
myMethod(x);
// ...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
更简洁的方式
DynamicIntProperty prop =
DynamicPropertyFactory.getInstance().getIntProperty("myProperty", DEFAULT_VALUE);
// prop.get() may change value at runtime
myMethod(prop.get());
- 1
- 2
- 3
- 4
同时,我们可以加入事件回调
prop.addCallback(new Runnable() {
public void run() {
// ...
}
});
- 1
- 2
- 3
- 4
- 5
动态属性如何实现动态,轮询框架
使用属性来源和定时器进行轮询
PolledConfigurationSource source = createMyOwnSource();
AbstractPollingScheduler scheduler = createMyOwnScheduler();
ConfigurationManager.install(new DynamicConfiguration(source, scheduler));
- 1
- 2
- 3
目前框架核心默认实现了两种来源,JDBCConfigurationSource、URLConfigurationSource
发布上下文(Deployment context)
比如应用程序需要读取一个数据库配置文件database-*.properties,但是*可能是prod和test,我们要从中选一个,这时需要咨询发布上下文,我们可以调用发布上下文的一个方法DeploymentContext.getDeploymentEnvironment(),然后我们就可以取得实际的数据库配置文件database-${environment}.properties
Deployment context properties can be used in many ways. Archaius uses deployment context to load cascaded configuration files. Here is the use case:
Application defines a set of default properties
At runtime, those properties should be overridden by deployment context specific values.
For example, an application has a configuration file database.properties, which contains default set of database properties. It has two other properties filedatabase-prod.properties, which contains overridden values for “prod” environment
database-test.properties, which contains overridden values for “test” environment
To load any one of the above file, the ConfigurationManager will consult with DeploymentContext object set with it to determine what environment it is in (through DeploymentContext.getDeploymentEnvironment() method), and load additional database-${environment} file to override the default values.
高效和线程安全的配置
框架提供了高效和线程安全的config(apache common configuration),比如:ConcurrentMapConfiguration、ConcurrentCompositeConfiguration,支持高并发和组合配置
参考文献
https://github.com/Netflix/archaius
https://github.com/Netflix/archaius/wiki
http://www.tuicool.com/articles/uQRn6f
http://commons.apache.org/proper/commons-configuration/
http://commons.apache.org/proper/commons-configuration/userguide_v1.10/user_guide.html