What is archaius and what can it do?
Archaius is one of Netflix's open source projects, a java-based configuration management class library, mainly used for dynamic acquisition of multiple configuration storage. The main function is an extension to the apache common configuration class library. It can be used as a distributed configuration management dependency component in cloud platform development. At the same time, it has the following features:
- Dynamically typed properties
- Efficient and thread-safe configuration operations
- Callback mechanism when configuration changes
- jmx
- Combination configuration (core content)
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);
// ...
more concise way
DynamicIntProperty prop =
DynamicPropertyFactory.getInstance().getIntProperty("myProperty", DEFAULT_VALUE);
// prop.get() may change value at runtime
myMethod(prop.get());
At the same time, we can add event callbacks
prop.addCallback(new Runnable() {
public void run() {
// ...
}
});
How dynamic properties implement dynamic, polling framework
Polling with property sources and timers
PolledConfigurationSource source = createMyOwnSource();
AbstractPollingScheduler scheduler = createMyOwnScheduler();
ConfigurationManager.install(new DynamicConfiguration(source, scheduler));
At present, the core of the framework implements two sources by default, JDBCConfigurationSource, URLConfigurationSource
Deployment context
For example, the application needs to read a database configuration file database-*.properties, but * may be prod and test, we need to choose one, then we need to consult the publishing context, we can call a method of the publishing context DeploymentContext.getDeploymentEnvironment() , then we can get the actual database configuration file 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
http://blog.csdn.net/zhangfb95/article/details/48297907