As microservices scale, managing their configurations across environments becomes complex. Hardcoding properties in each service is inefficient and error-prone.
Spring Cloud Config Server solves this by providing a centralized way to serve external configuration for all environments and services, with support for version control systems like Git or SVN.
In this post, we’ll walk through how to create a Spring Cloud Config Server, connect it to a Git repo, and configure clients to fetch their properties at runtime.

📦 Project Structure
1 2 3 4 5 6 7 |
com.kscodes.springboot.microservice ├── config-server → Centralized Config Server ├── product-service → Reads config from config-server └── config-repo (Git) → Stores application configuration files |
🧰 Step 1: Set Up Spring Cloud Config Server
🔧 Dependencies (pom.xml
)
1 2 3 4 5 6 7 8 9 10 |
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> |
Also include Spring Cloud BOM in dependencyManagement
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2023.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
✅ Enable Config Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kscodes.springboot.microservice.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); } } |
⚙️ application.yml (for Config Server)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
server: port: 8888 spring: cloud: config: server: git: uri: https://github.com/kscodes/config-repo clone-on-start: true application: name: config-server |
📁 Step 2: Prepare the Git Configuration Repository
Create a public or private Git repository such as:
1 2 3 4 |
https://github.com/kscodes/config-repo |
🔧 Add configuration files:
1 2 3 4 |
product-service.yml |
product-service.yml:
1 2 3 4 5 6 7 8 9 10 |
spring: application: name: product-service datasource: url: jdbc:postgresql://localhost:5432/products username: postgres password: secret |
You can create different files for environments:
product-service-dev.yml
product-service-prod.yml
🧩 Step 3: Configure Client (product-service)
🔧 Dependencies
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> |
📁 bootstrap.yml (in product-service
)
1 2 3 4 5 6 7 8 9 10 |
spring: application: name: product-service cloud: config: uri: http://localhost:8888 fail-fast: true |
🔧 application.yml
1 2 3 4 |
server: port: 8081 |
🔁 Step 4: Run and Test
✅ Steps:
- Start the Config Server on port
8888
. - Start product-service.
- Product-service will automatically fetch its config from:
1 2 3 4 |
http://localhost:8888/product-service/default |
You’ll see logs like:
1 2 3 4 5 |
Fetching config from server at http://localhost:8888 Located environment: [product-service] with profiles: [default] |
🔄 Optional: Refresh Config at Runtime
Add spring-boot-actuator
and enable /actuator/refresh
:
1 2 3 4 5 6 7 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |
1 2 3 4 5 6 7 8 |
management: endpoints: web: exposure: include: refresh |
Use a POST call to /actuator/refresh
when configs change:
1 2 3 4 |
curl -X POST http://localhost:8081/actuator/refresh |
🔐 Secure Config Server (Optional)
You can secure access to the config server using Basic Auth or OAuth2:
1 2 3 4 5 6 7 8 |
spring: security: user: name: configadmin password: secret123 |
📈 Benefits
- ✅ Centralized configuration for all environments
- ✅ Version-controlled using Git/SVN
- ✅ Easy to manage updates and rollbacks
- ✅ Dynamic refresh capability
- ✅ Secure and scalable
🏁 Conclusion
Spring Cloud Config Server is an essential tool for managing microservice configurations at scale. By externalizing and centralizing configuration, it enables cleaner, more secure, and maintainable deployments across environments.
This approach is a cornerstone for building robust cloud-native applications.