Dynamic Configurations with Annotations and NetflixOSS Archaius 2

NetflixOSS Archius 2 is a great Dynamic Configuration solution for microservices. Archaius is based on Apache Commons Configurations project.

Using Archaius we can load configurations from several sources such as OS env vars or any Database like Oracle or even from Zookeeper. If there is a missing configuration source you can add it pretty easy and load your configs. 

Archaius can be used in any java project no matter if is a microservice or not. 

Archaius also support dynamic configuration refresh via callbacks -- In short, this means you can reload your configs without re-deploy or downtime in your microservice -- which is really great.

Archaius has many nice features. Archaius also is very modular and easy to extend there are some nice community extensions available as well. 



Archaius 2 has some many nice and desirable features such as:
  • Dynamic, Typed Properties
  • High throughput and Thread Safe Configuration operations
  • A polling framework that allows obtaining property changes of a Configuration Source
  • A Callback mechanism that gets invoked on effective/"winning" property mutations (in the ordered hierarchy of Configurations)
  • A JMX MBean that can be accessed via JConsole to inspect and invoke operations on properties
  • Out of the box, Composite Configurations (With ordered hierarchy) for applications (and most web applications willing to use convention based property file locations)
  • Implementations of dynamic configuration sources for URLs, JDBC and Amazon DynamoDB
  • Scala dynamic property wrappers
Using Archaius 2 with Java Annotations

build.gralde


Already so we defined all archaius 2 dependencies needed. Now we can define the configurations we need - We will do that using Java annotations.

Let's create a Java interface called AppConfig.java.


First of all, we can see a class level annotation called @Configuration with this annotation we will define a configuration prefix so in this way, we don't need to repeat our selfs in other annotations.

Here we defined 3 methods using @PropertyName annotation and we also set up some default values using @DefaultValue annotation from archaius 2.

Now we need define some DefaultPropertyFactory just to make Archaius 2 injections happy - we could apply some customizations here but we really don't need it.

DefaultPropertyFactory.java

Next, we need to define GuiceArchaiusModule where we will add all other Guice bindings and archaius 2 configurations.



Here we have a couple of configurations. We need to define a Decoder and PropertyFactory in order to use Archaius. It's not likely you need to configure this -  just do the bindings like I did.

The getconfigs method is quite interesting and cool. Basically, I'm providing configuration from multiple sources with custom code. I'm getting all system config and also reading configs from a property file called app.properties if present and also doing some trick with os env variables. I'm converting all "_" to ".". Why? Because often OS Env vars cannot have "." and in Java, we use to use "." on property names so this conversation makes OS envs compatible with java.  Also, a very simple way to customize configurations.

Ok. Now we can create a simple class to read archaius configs.

SimpleService.java


Guice will create a proxy and inject the configs for us using the config source if they are not found the default values will kick in and be used.

Finally, we have our main class in other to load the Guice and Archaius 2 modules and run the application.

Main.java


src/main/resources/app.properties
app.has.persistence=false
#app.name.simple=HA

Archaius 2 is very useful and using annotations it's very sexy and works very well for Java developers.

You can also download the code in my github here.

Cheers,
Diego Pacheco

Popular posts from this blog

C Unit Testing with Check

Having fun with Zig Language

HMAC in Java