Learn how to set up centralized configuration management for microservices using Spring Boot 3.4.1 and Spring Cloud Config. You will create:
- A Config Service that reads a local property initially.
- A Config Server that manages configurations from a Git repo.
- A Config Client that fetches and refreshes properties dynamically from the server.
-
Generate a new Spring Boot project for
ConfigService
.- Visit https://start.spring.io/.
- Configure the project:
- Spring Boot Version: 3.4.1
- Group Id:
com.microservices
- Artifact Id:
config-service
- Package Name:
com.microservices.configservice
- Dependencies:
- Spring Web
- Click Generate to download the project zip file.
- Extract it into a folder named
ConfigService
.
-
Import the project into your IDE.
- Open your IDE and import the
ConfigService
project as a Maven project.
- Open your IDE and import the
-
Add a local property in
application.properties
.- In
src/main/resources/application.properties
, add:message=HelloWorld
- In
-
Create a controller to verify the property.
- In
src/main/java/com/microservices/configservice
, createHelloController.java
:package com.microservices.configservice; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${message:Default message}") private String message; @GetMapping("/message") public String getMessage() { return message; } }
- In
-
Run and test the Config Service.
- Use:
./mvnw spring-boot:run
- Access
http://localhost:8080/message
. You should see HelloWorld.
- Use:
-
Create a Git repository.
- Go to GitHub (or another Git provider).
- Create a repo named
config-repo
(public or private).
-
Clone the repository locally.
- In a terminal:
git clone https://github.com/<your-username>/config-repo.git cd config-repo
- In a terminal:
-
Add configuration files to the repo.
- Create two files for now:
application.properties
:message=Hello from Config Server!
config-service.properties
:message=Hello from Config Service!
- Commit and push:
git add . git commit -m "Initial config files" git push origin main
- Create two files for now:
-
Generate a new Spring Boot project for
ConfigServer
.- Visit https://start.spring.io/.
- Configure the project:
- Spring Boot Version: 3.4.1
- Group Id:
com.microservices
- Artifact Id:
config-server
- Name:
ConfigServer
- Package Name:
com.microservices.configserver
- Dependencies:
- Spring Web
- Spring Cloud Config Server
- Click Generate and extract into
ConfigServer
.
-
Import
ConfigServer
into your IDE. -
Enable the Config Server.
- In
ConfigServerApplication.java
, add:package com.microservices.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
- In
-
Configure the Config Server to use your Git repo.
- In
src/main/resources/application.properties
, add:server.port=8888 spring.application.name=config-server spring.cloud.config.server.git.uri=https://github.com/<your-username>/config-repo
- Replace
<your-username>
with your actual GitHub username.
- In
-
Run and test the Config Server.
- Use:
./mvnw spring-boot:run
- Check:
http://localhost:8888/application/default
http://localhost:8888/config-service/default
- You should see your Git-based properties.
- Use:
-
Add Spring Cloud Config dependency to
ConfigService
.- In
ConfigService
’spom.xml
:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
- In
-
Configure
ConfigService
to import configs from the server.- In
application.properties
ofConfigService
, add:spring.application.name=config-service spring.cloud.import=configserver:http://localhost:8888
- This is the updated approach for Spring Boot 3.4.x / Spring Cloud 2023.0+.
- In
-
Restart the
ConfigService
.- Again:
./mvnw spring-boot:run
- Check logs to ensure it pulls from
config-server
.
- Again:
-
Test dynamic retrieval.
- Access
http://localhost:8080/message
. - Now you should see
Hello from Config Service!
(based onconfig-service.properties
in Git).
- Access
-
Add Spring Actuator to enable refresh.
- In
ConfigService
’spom.xml
:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- In
-
Expose the refresh endpoint.
- In
application.properties
:management.endpoints.web.exposure.include=refresh
- In
-
Update a property in Git and refresh.
- Modify
config-service.properties
in yourconfig-repo
:message=Hello from Updated Config Server!
- Commit and push:
git add . git commit -m "Update message property" git push origin main
- Modify
-
Trigger a refresh and verify.
- Use:
curl -X POST http://localhost:8080/actuator/refresh
- Access
http://localhost:8080/message
and confirm the updated message is shown.
- Use:
- Add environment-specific configuration.
- In your Git repo, create
application-dev.properties
andapplication-prod.properties
with differentmessage
values. - In
ConfigService
’sapplication.properties
, set:spring.profiles.active=dev
- Verify different
message
values fordev
orprod
by switching profiles.
- In your Git repo, create
You have successfully:
- Created a microservice (
ConfigService
) that initially used a local property. - Set up a Git-based
ConfigServer
. - Pulled configurations dynamically using Spring Cloud Config with Spring Boot 3.4.1.
- Optionally, refreshed configurations at runtime and handled environment-specific setups.
Enjoy centralized configuration management for your microservices!