Centralized Configuration with Spring Cloud Config Server

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.

Centralized Configuration with Spring Cloud Config Server

📦 Project Structure


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)



    
        org.springframework.cloud
        spring-cloud-config-server
    



Also include Spring Cloud BOM in dependencyManagement:



    
        
            org.springframework.cloud
            spring-cloud-dependencies
            2023.0.0
            pom
            import
        
    


✅ Enable Config Server


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)


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:


https://github.com/kscodes/config-repo

🔧 Add configuration files:


product-service.yml

product-service.yml:


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



    org.springframework.cloud
    spring-cloud-starter-config


    org.springframework.cloud
    spring-cloud-starter-bootstrap


📁 bootstrap.yml (in product-service)


spring:
  application:
    name: product-service
  cloud:
    config:
      uri: http://localhost:8888
      fail-fast: true

🔧 application.yml


server:
  port: 8081

🔁 Step 4: Run and Test

✅ Steps:

  1. Start the Config Server on port 8888.
  2. Start product-service.
  3. Product-service will automatically fetch its config from:

http://localhost:8888/product-service/default

You’ll see logs like:


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:



    org.springframework.boot
    spring-boot-starter-actuator



management:
  endpoints:
    web:
      exposure:
        include: refresh

Use a POST call to /actuator/refresh when configs change:


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:


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.