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.
- IntelliJ IDEA
- Spring Boot 2.4.4
- JDK 14
- Consul
- Docker for Windows
- 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.
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.
Next, we create a Java class. This allows us to verify the values of from.consul1 and from.consul2 on the browser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.turreta.springcloud.consult.distributedconfig.consultdistributedconfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @Value("${from.consul1}") String myConsule1; @Value("${from.consul2}") String myConsule2; @GetMapping("/mycontroller") public String getValues() { return "{v1: '" + myConsule1 + "', v2: '" + myConsule2 + "'}"; } } |
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.
1 | spring.config.import=consul:localhost:8500 |
Note that this is retrieving configuration from Consul and requires the Consul Configuration Dependency. Otherwise, we’ll get the following error during application startup.
1 | java.lang.IllegalStateException: Unable to load config data from 'consul:localhost:8500' |
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.
1 | docker run -p 8500:8500 consul |
Note that this command may not work if we’d started up a Consul before. Therefore, we may need a slightly different command.
1 | docker start [CONTAINER_NAME | CONTAINER_ID] |
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.
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.
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.