Dynamic Configuration Management with Netflix Archaius and Apache ZooKeeper, Property-Files...

Datetime:2016-08-23 00:31:17         Topic: ZooKeeper          Share        Original >>
Here to See The Original Article!!!

Though having written about other configuration management libraries for Java before, I would like to demonstrate another one today: Netflix Archaius.

Archaius offers some nice features like dynamic typed properties, thread-safe operations, an event system for property changes/updates, a JMX MBean to read and update properties and adaptors for a variety of dynamic configuration sources like Amazon DynamoDB, JDBC, URLs and Apache ZooKeeper.

In the following tutorial I’d like to demonstrate how to read and update application configuration properties with Archaius and data sources like property-files, system-properties, JMX and Apache ZooKeeper.

Updating Archaius settings with JMX and JConsole



Using Maven we’re adding the following three dependencies to our project’s pom.xml :

  • archaius-core : The Archaius library
  • archaius-zookeeper : Apache ZooKeeper support for Archaius
  • curator-test : Lets us create ZooKeeper instances for test environments with ease

Sample Application

As we’re going to demonstrate reading configuration files from different sources, we’re adding the following configuration file to our project ( src/main/resources ):



And this is our application in a single class.

What is happening here?

  1. We’re starting a new ZooKeeper instance for demonstration purpose
  2. We’re initializing the ZooKeeper client and we’re setting a value ‘ http://www.test.de for the znode path /config/search.url
  3. We’re setting a system property that allows us to modify configuration values using JMX and the Netflix MBeans
  4. We’re initializing different configuration sources: a source for our property file, a source for reading system properties, a source for ZooKeeper
  5. We’re adding all these configuration sources to a combined configuration, our final configuration. Please note, that the order of the configuration sources is important as it defines the way, we’re searching for specific configuration values from the sources defined!
  6. Now we’re creating a new dynamic property for the key search.url and we’re adding a callback that prints the new value of this property when it has changed
  7. We’re now printing the current value of this property which should be http://www.test.de
  8. Then we’re updating the value in our ZooKeeper instance using the test client and the callback should print that value (http://www.foo.de)
package com.hascode.tutorial;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryOneTime;
import com.google.common.io.Files;
import com.netflix.config.ConcurrentCompositeConfiguration;
import com.netflix.config.ConcurrentMapConfiguration;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.netflix.config.DynamicWatchedConfiguration;
import com.netflix.config.source.ZooKeeperConfigurationSource;
import com.netflix.curator.test.TestingServer;
public class App {
	public static void main(String[] args) throws Exception {
		try (TestingServer zkTestServer = new TestingServer(2181, Files.createTempDir());
				CuratorFramework testClient = CuratorFrameworkFactory.newClient(zkTestServer.getConnectString(),
						new RetryOneTime(2000))) {
			testClient.create().forPath("/config/search.url", "http://www.test.de".getBytes());
			// enable JMX dynamic configuration
			System.setProperty(DynamicPropertyFactory.ENABLE_JMX, "true");
			// enable config from properties file
			ConcurrentMapConfiguration configFile = new ConcurrentMapConfiguration(
					new PropertiesConfiguration("config.properties"));
			// enable configuration from system properties
			ConcurrentMapConfiguration systemProps = new ConcurrentMapConfiguration(new SystemConfiguration());
			CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181",
					new ExponentialBackoffRetry(1000, 3));
			ZooKeeperConfigurationSource zkConfigSource = new ZooKeeperConfigurationSource(client, "/config");
			System.out.println("value in zookeeper: " + zkConfigSource.getCurrentData().get("search.url"));
			DynamicWatchedConfiguration zkDynamicConfig = new DynamicWatchedConfiguration(zkConfigSource);
			// composite configuration
			ConcurrentCompositeConfiguration config = new ConcurrentCompositeConfiguration();
			DynamicStringProperty prop = DynamicPropertyFactory.getInstance().getStringProperty("search.url",
			prop.addCallback(() -> System.out.println("search url has changed to " + prop.get()));
			System.out.println("search url is: " + prop.get());
			testClient.setData().forPath("/config/search.url", "http://www.foo.de".getBytes());
			System.console().readLine("press enter to shutdown application\n");
			System.out.println("shutting down application..");

Running the Example

We’re now ready to run our example application in our IDE of choice or using Maven in the command line like this:

$ mvn exec:java -Dexec.mainClass=com.hascode.tutorial.App
[INFO] ------------------------------------------------------------------------
[INFO] Building netflix-archaius-tutorial 1.0.0
[INFO] ------------------------------------------------------------------------
value in zookeeper: http://www.test.de
search url is: http://www.test.de
press enter to shutdown application
search url has changed to http://www.foo.de
shutting down application..

To update the configuration value we may simply use JConsole, connect to our process and update the value as show on the following screenshot:

Updating Archaius settings with JMX and JConsole

Tutorial Sources

Please feel free to download the tutorial sources from my Bitbucket repository , fork it there or clone it using Git:

git clone https://bitbucket.org/hascode/netflix-archaius-tutorial.git


Alternative: Java Config-Builder

I have written about another configuration library in my tutorial “ Using Java Config-Builder to assemble your Application Configuration “, please feel free to read.

About List