Java, Java Microservices, Micronaut, Microservices

Micronaut Consul Distributed Configuration Example In Java

When we start to build something based on Microservice design, we’d inevitably need a distributed configuration for our applications. Having a distributed configuration makes scaling them out a lot easier. This post shows how to configure Micronaut to pick up a distributed configuration from Consul. All these in a local development environment.

Requisites

These are the following items we used in this post.

  1. JDK 14 for 64-bit for Windows 10
  2. Micronaut 2.4.1
  3. IntelliJ IDEA  2020.3 – Optional
  4. Docker for Windows
  5. Consul Docker image

Create a Micronaut Application In IntelliJ IDEA

First, let’s create a Micronaut Application in IntelliJ, as shown below. Go to File > New > Project..., choose Micronaut, and click Next.

 

Then, fill in the Group, Artifact, and Application type. Choose Application (A Micronaut Application).

 

Finally, include Consul Distributed Configuration. This enables our Micronaut application to consume distributed configuration from Consul. Click Next to create the Micronaut project.

 

Our Micronaut project will have the following project structure and files.

Micronaut Consul Distributed Configuration

 

Now, we have a template project to work on.

Modify Our Micronaut Application

First, modify bootstrap.yml, as shown below. We added the last two lines.

Next, create a Web controller that’ll greet the user when he visits a specific URI. For example, HTTP://localhost:8080/hello. Within this controller, we’ll display the value of a property named greeting.msg, which we’ll define in Consul.

Don’t start up our Micronaut application yet, because we still don’t have a Consul instance running.

Startup a Consul Docker Container For Our Distributed Configuration

Next, let’s start up a Consult Docker container using the following command.

Micronaut Consul Distributed Configuration

 

Define Distributed Configuration In Consul For Our Micronaut Application To Use

Then, go to the Consul dashboard via http://localhost:8500/.

Micronaut Consul Distributed Configuration

 

We can now define a value for the greeting.msg property in Consul. Go to the Key/Value tab and create the folder config and its sub-folder application.

Micronaut Consul Distributed Configuration

 

Finally, create the greeting.msg property.

Micronaut Consul Distributed Configuration

Startup Our Micronaut Application To Consume Distributed Configuration From Consul

Now, we can start up our Micronaut application to consume a distributed configuration from Consul.

 

Then, access the URL HTTP://localhost:8080/hello.

 

Why did we create those folders in the first place? The /config folder is the default path where Micronaut (not Consul) looks for key/pair values for properties like greeting.msg. However, the search path is configurable via the consul.client.config.path property. Meaning, if we modified our bootstrap.yml file as follows, we’d get an error when accessing the same URI.

We’d need to define the same property in /otherpath/application. How about the application/ folder? It is one of the sub-folders Micronaut looks for configuration in Consul.

Configuration Resolution Precedence

DirectoryDescription
/applicationConfiguration shared by all applications
/application,prodConfiguration shared by all applications for the prod Environment
/[APPLICATION_NAME]Application-specific configuration, example /config/hello-world

/[APPLICATION_NAME],prod
Application-specific configuration for an active Environment

The APPLICATION_NAME property refers to the name of the application we defined in the bootstrap.yml (see micronaut.application.name). Meaning, for our Micronaut application, we can define the same property under the /config/distributeconfig path, and the property greeting.msg will be resolved if it hasn’t been defined elsewhere.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like

One comment

  1. 1

    The Micronaut starter service (https://launch.micronaut.io) appears to create the

    file when we include the Consul Distributed Configuration feature. We tried creating a Micronaut application without it and an

    application.yml is created instead.

Comments are closed.