TP13.2: Spring Cloud Config Server
🎯 Objectives of the Lab
- Create a Config Server with Git backend
- Configure services as Config Clients
- Implement hot configuration refresh
- Manage profiles (dev, prod)
Estimated duration: 1h
📋 Architecture
Part 1: Create the Configuration Repository (15 min)
1.1 Create a Local Git Repo (or GitHub)
# Create the folder
mkdir config-repo
cd config-repo
git init
# Create the configuration files
1.2 Common Configuration (application.yml)
# config-repo/application.yml
# Configuration shared by ALL services
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
management:
endpoints:
web:
exposure:
include: health,info,refresh
logging:
level:
root: INFO
1.3 User Service Configuration
# config-repo/user-service.yml
server:
port: 8081
user:
default-role: USER
password-min-length: 8
welcome-message: "Welcome to User Service!"
# config-repo/user-service-dev.yml
user:
welcome-message: "Welcome to User Service (DEV)!"
logging:
level:
com.example: DEBUG
# config-repo/user-service-prod.yml
user:
welcome-message: "Welcome to User Service (PRODUCTION)"
password-min-length: 12
logging:
level:
com.example: WARN
1.4 Product Service Configuration
# config-repo/product-service.yml
server:
port: 8083
product:
max-per-page: 20
currency: CAD
featured-enabled: true
# config-repo/product-service-dev.yml
product:
currency: DEV-CAD
featured-enabled: true
logging:
level:
com.example: DEBUG
1.5 Commit the Files
cd config-repo
git add .
git commit -m "Initial configuration files"
Part 2: Config Server (20 min)
2.1 Create the Project
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>
<groupId>com.example</groupId>
<artifactId>config-server</artifactId>
<version>1.0.0</version>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 Main Application
package com.example.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);
}
}
2.3 Configuration (with Local Git)
# src/main/resources/application.yml
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
# Path to your local repo (Windows)
uri: file:///C:/path/to/config-repo
# OR Linux/Mac path
# uri: file://${user.home}/config-repo
default-label: master
clone-on-start: true
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
2.4 Alternative Configuration (GitHub)
spring:
cloud:
config:
server:
git:
uri: https://github.com/your-username/config-repo
default-label: main
# If private repo:
# username: ${GIT_USERNAME}
# password: ${GIT_TOKEN}
2.5 Test the Config Server
# Start the Config Server
cd config-server
mvn spring-boot:run
# Test the endpoints
# Format: /{application}/{profile}
curl http://localhost:8888/user-service/default
curl http://localhost:8888/user-service/dev
curl http://localhost:8888/user-service/prod
curl http://localhost:8888/product-service/dev
Expected response for /user-service/dev:
{
"name": "user-service",
"profiles": ["dev"],
"label": null,
"version": "abc123...",
"propertySources": [
{
"name": "file:///C:/config-repo/user-service-dev.yml",
"source": {
"user.welcome-message": "Welcome to User Service (DEV)!",
"logging.level.com.example": "DEBUG"
}
},
{
"name": "file:///C:/config-repo/user-service.yml",
"source": {
"server.port": 8081,
"user.default-role": "USER"
}
},
{
"name": "file:///C:/config-repo/application.yml",
"source": {
"eureka.client.service-url.defaultZone": "http://localhost:8761/eureka/"
}
}
]
}
Part 3: Config Client (User Service) (15 min)
3.1 Add Dependencies
Modify the pom.xml of User Service:
<dependencies>
<!-- Existing... -->
<!-- Config Client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Bootstrap (to load config BEFORE the app) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
3.2 Create bootstrap.yml
# src/main/resources/bootstrap.yml
spring:
application:
name: user-service
profiles:
active: dev
cloud:
config:
uri: http://localhost:8888
fail-fast: true
retry:
initial-interval: 1000
max-attempts: 6
3.3 Simplify Local application.yml
# src/main/resources/application.yml
# Most config now comes from the Config Server
# Keep only what's local/override
# Almost nothing - everything comes from the Config Server!
3.4 Use Externalized Properties
package com.example.userservice.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/api/config")
@RefreshScope // Allows hot refresh!
public class ConfigController {
@Value("${user.welcome-message}")
private String welcomeMessage;
@Value("${user.default-role}")
private String defaultRole;
@Value("${user.password-min-length}")
private int passwordMinLength;
@GetMapping
public ResponseEntity<Map<String, Object>> getConfig() {
return ResponseEntity.ok(Map.of(
"welcomeMessage", welcomeMessage,
"defaultRole", defaultRole,
"passwordMinLength", passwordMinLength
));
}
@GetMapping("/welcome")
public ResponseEntity<String> getWelcome() {
return ResponseEntity.ok(welcomeMessage);
}
}
3.5 Test
# Startup order:
# 1. Eureka Server (8761)
# 2. Config Server (8888)
# 3. User Service (8081)
cd user-service
mvn spring-boot:run
# Check that the config is loaded
curl http://localhost:8081/api/config
Expected result:
{
"welcomeMessage": "Welcome to User Service (DEV)!",
"defaultRole": "USER",
"passwordMinLength": 8
}
Part 4: Hot Refresh (10 min)
4.1 Modify the Configuration
Edit config-repo/user-service-dev.yml:
user:
welcome-message: "Welcome to User Service (DEV) - UPDATED!"
logging:
level:
com.example: DEBUG
cd config-repo
git add .
git commit -m "Update welcome message"
4.2 Check Before Refresh
curl http://localhost:8081/api/config/welcome
# Result: "Welcome to User Service (DEV)!"
# (Old value - not refreshed yet)
4.3 Trigger the Refresh
curl -X POST http://localhost:8081/actuator/refresh
Response:
["user.welcome-message"]
4.4 Check After Refresh
curl http://localhost:8081/api/config/welcome
# Result: "Welcome to User Service (DEV) - UPDATED!"
🎉 The configuration has been updated without restarting the service!
✅ Validation Checklist
- Config Server starts on http://localhost:8888
-
curl http://localhost:8888/user-service/devreturns the config - User Service loads its config from Config Server
-
/api/configdisplays externalized properties - Modification + commit +
/actuator/refresh→ new config
🎯 Bonus Exercises
Exercise 1: Production Profile
Start User Service with the prod profile:
mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=prod
Check that passwordMinLength is 12.
Exercise 2: Discovery via Eureka
Instead of a fixed URL, use Eureka:
# bootstrap.yml
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
Exercise 3: Product Service Configuration
Configure Product Service as a Config Client and expose /api/config.