An article to understand SpringCloud's configuration management framework Archaius

An article to understand SpringCloud's configuration management framework Archaius

1. What is Archaius?

Archaius is a Java class library that provides an API to obtain properties that can be dynamically changed at runtime

2. Archaius features

1. Dynamic properties

  1. You can use concise code to dynamically obtain type-specific attributes
  2. You can create a callback after modifying an attribute
DynamicIntProperty prop = 
      DynamicPropertyFactory.getInstance().getIntProperty("myProperty", DEFAULT_VALUE);
//prop.get() may change value at runtime
myMethod(prop.get());

// callback
prop.addCallback(new Runnable() {
  public void run() {
     //...
  }
});
 

2. Read configuration source data information cyclically

  1. DynamicPropertyFactory will obtain the configuration of the URL under the classpath or specified by the parameter at a certain interval (default 1 minute) to achieve dynamic update
  2. We can implement related interfaces ourselves to customize our own data sources, load and update logic regularly, etc.
PolledConfigurationSource source = createMyOwnSource();
AbstractPollingScheduler scheduler = createMyOwnScheduler();
ConfigurationManager.install(new DynamicConfiguration(source, scheduler));
 

3. Complete the realization of some configuration sources

  1. Complete the implementation of data sources such as JDBC, DynamoDB, zk, etc., out of the box
  2. You can customize your own configuration source, refer to: github.com/Netflix/arc...

4. Configuration and release context management

  1. com.netflix.config.ConfigurationManager is a manager used to manage system-wide configuration
  2. It is the core processing class of archaius, and the subsequent custom configuration source loading also needs to go through this class

5. High-throughput and thread-safe acquisition configuration

6. Support scala

3. simple hybrid configuration architecture

  1. The core concept of archaius is hybrid configuration, which can maintain one or more configuration sources, such as JDBC, REST, local configuration, zk, container configuration, etc.
  2. When a configuration exists in multiple configuration sources, the final configuration priority depends on the hierarchical order in the figure below; for example, if you configure property A (value=1) in the database, it is also configured in config.properties Attribute A (value=2), then from the hierarchical relationship in the figure below, the configuration of value=1 will finally take effect

4. use Archaius configuration

1. Introduce dependencies

<dependency>
  <groupId>com.netflix.archaius</groupId>
  <artifactId>archaius-core</artifactId>
  <version>0.6.0</version>
 </dependency>
 

2. Use a local file as the configuration source

  1. By default archaius can use the config.properties file in the classpath as the configuration source
  2. Of course, the config.properties in the jar will also be used as the configuration source, but this priority is lower than the current project classpath
  3. We can configure config.properties in the classpath, and dynamically obtain the configuration through the configuration method in 5

3. Use external URLS as configuration source

You can use the following configuration to specify the configuration source, which has a higher priority than config.properties

-Darchaius.configurationSource.additionalUrls=http://myserver/properties
 

4. The default configuration parameters of archaius

  1. archaius.configurationSource.defaultFileName, the default value is config.properties; represents the local configuration file name
  2. archaius.fixedDelayPollingScheduler.initialDelayMills, the default is 30000; indicates the delay time to pull the configuration during initialization
  3. archaius.fixedDelayPollingScheduler.delayMills. The default is 60000; it means the interval time to pull the configuration regularly
  4. These three parameters can be reset through the system parameters

5. Use configuration

// 
System.out.println(DynamicPropertyFactory.getInstance()
                .getStringProperty("test.archaius", "fail").get());

System.out.println(DynamicProperty.getInstance("test.archaius").getString());
 

6. Summary of Configuration Manager

  1. ConfigurationManager is the manager used by archaius to manage all configurations, including the configuration of the management system, dynamicUrl, environment, and custom configuration sources (such as zk, db, etc.)
  2. ConfigurationManager is managed by instantiating a CompositionConfiguration
  3. We can get a system global configuration through ConfigurationManager.getConfigInstance(), including configuration data of all levels
  4. Specific source code will not be analyzed