1. Overview

Netflix Archaius is a powerful configuration management library.

Simply put, it’s a framework that can be used to gather configuration properties from many different sources, offering fast, thread-safe access to them.

On top of this, the library allows properties to change dynamically at runtime, making it possible for the system to get these variations without having to restart the application.

In this introductory tutorial, we’ll set up a simple Spring Cloud Archaius configuration, we’ll explain what’s happening under the hood, and finally, we’ll see how Spring allows extending the basic setup.

2. Netflix Archaius Features

As we know, Spring Boot already provides instruments to manage externalized configurations, so why bother setting up a different mechanism?

Well, Archaius offers some handy and interesting features that aren’t contemplated by any other configuration framework. Some of its key points are:

  • Dynamic and Typed properties
  • A Callback mechanism that gets invoked on property mutations
  • Ready for use implementations of dynamic configuration sources such as URLs, JDBC and Amazon DynamoDB
  • A JMX MBean that can be accessed by Spring Boot Actuator or JConsole to inspect and manipulate the properties
  • Dynamic properties validation

These perks can be beneficial in many scenarios.

Therefore, Spring Cloud has worked on a library that allows to easily configure a ‘Spring Environment Bridge’ so that Archaius can read properties from the Spring Environment.

3. Dependencies

Let’s add the spring-cloud-starter-netflix-archaius to our application, it’ll add all the necessary dependencies to our project*.*

Optionally, we can also add spring-cloud-netflix to our dependencyManagement section and rely on its specification of the versions of the artifacts:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-archaius</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix</artifactId>
            <version>2.0.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Note: we can check Maven Central to verify we’re using the latest version of the starter library.

4. Usage

Once we add the required dependency, we’ll be able to access the properties managed by the framework:

DynamicStringProperty dynamicProperty 
  = DynamicPropertyFactory.getInstance()
  .getStringProperty("baeldung.archaius.property", "default value");

String propertyCurrentValue = dynamicProperty.get();

Let’s work on a short example to see how this is available just out-of-the-box.

4.1. Quick Example

By default, it manages dynamically all the properties defined in a file named config.properties in the application’s classpath.

So let’s add it to our resources folder with some arbitrary properties:

#config.properties
baeldung.archaius.properties.one=one FROM:config.properties

Now we’ll need a way to check the properties’ values at any specific moment. In this case, we’ll create a RestController that retrieves the values as a JSON response:

@RestController
public class ConfigPropertiesController {
    
    private DynamicStringProperty propertyOneWithDynamic
      = DynamicPropertyFactory.getInstance()
      .getStringProperty("baeldung.archaius.properties.one", "not found!");
    
    @GetMapping("/property-from-dynamic-management")
    public String getPropertyValue() {
    return propertyOneWithDynamic.getName() + ": " + propertyOneWithDynamic.get();
    }
}

Let’s try it out. We can send a request to this endpoint, and the service will retrieve the values stored in config.properties as expected.

No big deal so far, right? Ok, let’s go on and change the values of the property in the classpath file, without restarting the service. As a result, after a minute or so, a call to the endpoint should retrieve the new values. Pretty cool, isn’t it?

Next, we’ll try to understand what’s going on under the hood.

5. How Does It Work?

First of all, let’s attempt to comprehend the big picture.

Archaius is an extension of the Apache’s Commons Configuration library, adding some nice features like a polling framework for dynamic sources, with high throughput and thread-safe implementation.

The spring-cloud-netflix-archaius library comes then into play, merging all the different property sources, and auto-configuring the Archaius tools with these sources.

5.1. The Netflix Archaius Library

It operates defining a Composite Configuration, a collection of various configurations obtained from different sources.

Moreover, some of those Configuration Sources might support being polled at runtime for changes. Archaius provides interfaces and some pre-defined implementations to configure these types of sources.

The collection of sources is hierarchized so that if a property is present in multiple configurations, the final value will be the one in the topmost slot.

Finally, a ConfigurationManager handles the system-wide Configuration and deployment context. It can install the final Composite Configuration, or retrieve the installed one for modification.

5.2. Spring Cloud Support

The main task of the Spring Cloud Archaius library is to merge all the different configuration sources as a ConcurrentCompositeConfiguration and install it using the ConfigurationManager.

The order of precedence in which the library defines the sources is:

  1. Any Apache Common Configuration AbstractConfiguration bean defined in the context
  2. All the sources defined in the Autowired Spring ConfigurableEnvironment
  3. The default Archaius sources, which we saw in the example above
  4. Apache’s SystemConfiguration and EnvironmentConfiguration sources

Another useful feature this Spring Cloud library provides is the definition of an Actuator Endpoint to monitor and interact with the properties. Its usage is out of the scope of this tutorial.

6. Adapting and Extending the Archaius Configuration

Now that we have a better understanding of how Archaius works, we are in good shape to analyze how to adapt the configuration to our application, or how to extend the functionality using our configuration sources.

6.1. Archaius Supported Configuration Properties

If we want Archaius to take into account other configuration files similar to the config.properties one, we can define the archaius.configurationSource.additionalUrls system property.

The value is parsed to a list of URLs separated by a comma, so, for example, we can add this system property when we launch the application:

-Darchaius.configurationSource.additionalUrls=
  "classpath:other-dir/extra.properties,
  file:///home/user/other-extra.properties"

Archaius will read the config.properties file first, and then the other ones, in the specified order. Because of this, the properties defined in the latter files will have priority over the prior ones.

There are a couple of other system properties we can use to configure various aspects of the Archaius default configuration:

  • archaius.configurationSource.defaultFileName: the default configuration file name in the classpath
  • archaius.fixedDelayPollingScheduler.initialDelayMills: initial delay before reading the configuration source
  • archaius.fixedDelayPollingScheduler.delayMills: delay between two reads of the source; the default value is 1 minute

6.2. Adding Additional Configuration Sources with Spring

How could we add a different Configuration Source to be managed by the described framework? And how could we manage dynamic properties with higher precedence than the ones defined in the Spring Environment?

Reviewing what we mentioned in section 4.2, we can realize that the highest configurations in the Composite Configuration defined by Spring are the AbstractConfiguration beans defined in the context.

Thus, all we need to do is add an implementation of this Apache’s abstract class to our Spring Context using some of the functionality provided by Archaius, and the Spring’s autoconfiguration will spontaneously add it to the managed configuration properties.

To keep things simple, we’ll see an example where we configure a properties file similar to the default config.properties but with the difference of having a higher precedence than the rest of Spring environment and application properties:

@Bean
public AbstractConfiguration addApplicationPropertiesSource() {
    URL configPropertyURL = (new ClassPathResource("other-config.properties")).getURL();
    PolledConfigurationSource source = new URLConfigurationSource(configPropertyURL);
    return new DynamicConfiguration(source, new FixedDelayPollingScheduler());
}

Lucky for us, it contemplates several configuration sources that we can set up with almost no effort. Their configuration is out of the scope of this introductory tutorial.

7. Conclusion

To sum up, we’ve learned about Archaius and some of the cool features it offers to take advantage of configuration management.

Also, we saw how the Spring Cloud autoconfiguration library comes into play allowing us to use this library’s API conveniently.

Once more, we can find all the examples shown in this tutorial and others in our Github repo.


» 下一篇: Chaos Monkey 教程