Java Microservices, Microservices, Spring Boot

Spring Boot Consul Distributed Configuration Example

This post shows an example of a Spring Boot application that consumes distributed configuration from a Consul instance in a local development environment setup. We’ll generate a Spring Boot project and then modify it a bit to access Consul.

Requisites

The requisites are similar to those of  Micronaut Consul Distributed Configuration. However, we’re not using Micronaut. Hence, to list them out, these are the stuff for this post.

  1. IntelliJ IDEA
  2. Spring Boot 2.4.4
  3. JDK 14
  4. Consul
  5. Docker for Windows
  6. Windows 10

Create A Spring Boot Application For Consul Distributed Configuration Dependency

First, generate a Spring Boot application in Intellij IDEA with two dependencies. These include the Spring Web and Consul Configuration dependencies, as shown below.

Spring Boot Consul Distributed Configuration

 

The Spring Web makes our application a web application. On the other hand, the Consul Configuration allows our codes to read configuration data in Consul. Once we generated the application, we have the following project structure, which doesn’t have many files.

Spring Boot Consul Distributed Configuration

 

Next, we create a Java class. This allows us to verify the values of from.consul1 and from.consul2 on the browser.

Then, we add the following line in the application.properties for our Spring Boot application to connect to Consul with default settings. This one-liner enables our Spring Boot codes to consume a distributed configuration from Consul. 

Note that this is retrieving configuration from Consul and requires the Consul Configuration Dependency. Otherwise, we’ll get the following error during application startup.

We’re done on this part, but don’t start the Spring Boot application yet.

Start Consul Docker Container Up

Next, we start up a Consul Docker container in our local development machine using the following command.

Note that this command may not work if we’d started up a Consul before. Therefore, we may need a slightly different command.

For example, list out all Docker containers we used last time (e.g., yesterday). Then, and pick the container name or ID to start it again.

Spring Boot Consul Distributed Configuration

Therefore, we could use either of the following commands to start the Consul up.

 

Manually Prepare Distributed Configuration In Consul Server

We could automate preparing distributed configuration, but we’ll do it manually on this post for brevity. Go to the Consul Dashboard via HTTP://localhost:8500. Next, go to the Key/Value tab and click the Create button. Specify the path config/application/  and click Save to create new folders. Then, navigate into the config/application/ folders and create two keys with values.

 

Key from.consul1

 

Key from.consul2

 

Finally, once Consul is up and running with sample distributed configuration, our Spring Boot application can now access and consume the Consul server’s distributed configuration. Start our Spring Boot application up from the IDE.

Spring Boot And Consul Distributed Configuration Demo

Open a browser and go to HTTP://localhost:8080/mycontroller.

For further reading, please read Spring Boot Cong Data Import.

You Might Also Like