Learn how to use HashiCorp Consul to enable dynamic service discovery and leverage its KV (Key-Value) store for storing and retrieving configuration data. You will register Spring Boot 3.4.1 microservices with Consul and explore how they can fetch dynamic properties from Consul’s KV store.
-
Download the Consul binary.
- Visit Consul Downloads and download the latest binary for your operating system.
-
Extract the Consul binary.
- Windows:
- Unzip the file (e.g.,
consul_<version>_windows_amd64.zip
) to a folder likeC:\HashiCorp\Consul
.
- Unzip the file (e.g.,
- macOS/Linux:
- Extract it to
/usr/local/bin
or another suitable directory:unzip consul_<version>_<platform>.zip -d /usr/local/bin/
- Extract it to
- Windows:
-
Add Consul to the PATH.
- Windows:
- Edit Path in Environment Variables to include the folder with
consul.exe
.
- Edit Path in Environment Variables to include the folder with
- macOS/Linux:
- In your shell config:
export PATH=$PATH:/usr/local/bin/consul
- Reload:
source ~/.bashrc
- In your shell config:
- Windows:
-
Start Consul in development mode.
- In a terminal:
consul agent -dev
- Consul listens on port 8500 by default and runs in memory.
- In a terminal:
-
Access the Consul UI.
- Go to:
http://127.0.0.1:8500
- You should see the Consul dashboard (no registered services yet).
- Go to:
-
Add a key-value pair to the KV store.
- From the Key/Value tab in the UI:
- Key:
config/sample-service
- Value:
{ "greeting": "Hello from Consul KV Store!" }
- Key:
- Save the changes.
- From the Key/Value tab in the UI:
-
Verify KV store retrieval using CLI.
- In a terminal:
consul kv get config/sample-service
- Confirm it matches the JSON value set in the UI.
- In a terminal:
-
Generate a new Spring Boot project for
ConsulServer
.- Visit https://start.spring.io/.
- Configure:
- Spring Boot Version: 3.4.1
- Group Id:
com.microservices
- Artifact Id:
consul-server
- Name:
ConsulServer
- Package Name:
com.microservices.consulserver
- Dependencies:
- Spring Web
- Spring Cloud Consul Discovery
- Spring Boot Actuator
- Extract into a folder named
ConsulServer
.
-
Import the
ConsulServer
project into your IDE. -
Enable Consul Discovery.
- In
ConsulServerApplication.java
:package com.microservices.consulserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ConsulServerApplication { public static void main(String[] args) { SpringApplication.run(ConsulServerApplication.class, args); } }
- In
-
Configure Consul in
application.properties
.- Create
src/main/resources/application.properties
:spring.application.name=consul-server spring.cloud.consul.host=127.0.0.1 spring.cloud.consul.port=8500 server.port=8081
- Create
-
Add a sample REST endpoint.
- In
src/main/java/com/microservices/consulserver/ConsulController.java
:package com.microservices.consulserver; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConsulController { @GetMapping("/greeting") public String greeting() { return "Hello from Consul Server!"; } }
- In
-
Run
ConsulServer
.- From the
ConsulServer
folder:./mvnw spring-boot:run
- Check Consul UI at:
http://127.0.0.1:8500
- Under Services, you should see
consul-server
.
- From the
-
Fetch KV store values dynamically.
- Modify
ConsulController
to retrieve a property from Consul:package com.microservices.consulserver; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConsulController { @Value("${greeting:Default Greeting}") private String greeting; @GetMapping("/greeting") public String getGreeting() { return greeting; } }
- Now,
greeting
will map toconfig/sample-service
’sgreeting
if your application name is set toconsul-server
or you override the key.
- Modify
-
Test KV store retrieval.
- Access:
http://localhost:8081/greeting
- You should see Hello from Consul KV Store! if your key and property are correctly aligned.
- Access:
-
Generate another service, e.g.
UserService
.- Repeat steps 8–11 with:
- Artifact Id:
user-service
- Name:
UserService
- Package Name:
com.microservices.userservice
- Change
server.port=8082
.
- Artifact Id:
- Repeat steps 8–11 with:
-
Verify both services in Consul.
- After running both:
./mvnw spring-boot:run
- Check the Consul UI again. You should see
consul-server
anduser-service
.
- After running both:
-
Add environment-specific KV keys.
- E.g.,
config/sample-service/dev
vs.config/sample-service/prod
. Switch profiles to see different values.
- E.g.,
-
Simulate a service failure.
- Stop or kill one microservice and observe Consul automatically mark it as unhealthy or remove it.
-
Use Consul for additional config.
- Store property sets for each environment or service in the KV store, retrieving them dynamically in your microservices.
In this lab, you:
- Installed and ran Consul in development mode.
- Created key-value pairs for dynamic configuration.
- Registered one or more Spring Boot 3.4.1 microservices with Consul using Spring Cloud Consul.
- Fetched KV store data to update microservices with dynamic configurations.
This pattern enables flexible service discovery, health checks, and a centralized configuration approach, paving the way for more advanced features (e.g., load balancing, auto-scaling, multi-datacenter replication). Enjoy building on Consul’s robust capabilities!