How to setup Envconsul for dynamic configuration in your application(or services).


So, You want to integrate Envconsul into your application for dynamic configuration.Let’s see How it can be achieved and what all needs to be done.

Using Envconsul you can have the dynamic configuration in your services without taking them down i.e Services will never go down and you can make changes in your services(dynamic configuration).You can change things like the PORT number of any service, db password, access keys and IP address on runtime and You don’t have to worry about the service downtime.

Step1. The very first job would be to setup Consul. In consul, we will be dealing with Consul KV store to allow to define KV(Key Value) pairs that can be pulled into running services.Let’s see How we can setup Consul and then add KV pairs into Consul KV store.

1. First, install the consul from here https://www.consul.io/intro/getting-started/install.html
2. Run the Consul Agent using this command “consul agent -dev”
3. Then add KV pairs into Consul KV store using the following commands

consul kv put redis/config/minconns 1

//where redis/config is the folder and minconns is the key and 1 is the value for the key minconns.

For more information check here https://www.consul.io/intro/getting-started/kv.html

Now our Consul has been setup, Now You can check the KV pairs and running services at

http://localhost:8500

For KV pair visit

http://localhost:8500/ui/#/dc1/kv/

Step2. Setup Envconsul
Envconsul is a tool which is basically used to read and set environmental variables for processes from Consul.Envconsul provides a convenient way to populate values from Consul into a child process environment using the envconsul daemon.
The daemon envconsul allows applications to be configured with environment variables, without having knowledge about the existence of Consul. This makes it especially easy to configure applications throughout all your environments: development, testing, production, etc.

Now you can download the Envconsul from here https://releases.hashicorp.com/envconsul/

Once the envconsul is downloaded unzip it and put it in the /usr/local/bin. It is the same path where we put consul at.

Now both Consul and Envconsul are setup.

Step3. Access Environment variables set by envconsul into the application.
I am using Java.So, There are basically two ways in Java.

1.The java.lang.System.getenv(String name) method gets the value of the specified environment variable. An environment variable is a system-dependent external named value.

For Example
String value = System.getenv(“TEST”); //where TEST is the key and we are trying to fetch the value of key “TEST” over here.

2.Another way is to access the environment variables through application.conf.

In application.conf do the following
test = test
test = ${?TEST}//TEST is an environment variable.

After this you can access the test variable in your application like below

String testEnvVariable = ConfigFactory.load().getString("test");

Step4. Now finally we will run our application using envconsul like below

$envconsul -prefix=Test env mvn lagom:runAll

// Test is the folder where all the key value pairs reside and env is where all the environment variables reside and mvn lagom:runAll is the way to run my application

Now, You are done and everthing is in its place. To check everything works fine and we are able to make dynamic configuration, You can make changes to Consul’s KV store values and see if the updated values are reflected in your application while it is running,If it does you have achieved it cheers!
If the updated values do not reflect into the running application then you might have to go back to previous steps and ensure everything is correct and configured properly. That’s it!

If you find any challenge, Do let me know in the comments.
If you enjoyed this post, I’d be very grateful if you’d help it spread.Keep smiling, Keep coding! Cheers!

About deepak028

There is nothing much to describe me.However, I am a very ordinary person who believes in sharing knowledge.
This entry was posted in Scala. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s