How does Spring Cloud Config help in centralized configuration management?

Problem Without Centralized Configuration

In microservices:

  • Each service has its own application.properties

  • Environment changes (dev/qa/prod) require redeployment

  • Configuration drift occurs

  • Security risk (secrets in code)


πŸ”Ή What is Spring Cloud Config?

Spring Cloud Config provides externalized, centralized configuration for all microservices.

It uses a Config Server that fetches configuration from:

  • Git (most common)

  • File system

  • Vault


πŸ”Ή Architecture

Client Service
⬇
Config Server
⬇
Git Repository

Each microservice fetches config at startup or runtime.


πŸ”Ή How It Works (Flow)

  1. Config Server starts

  2. Reads configuration from Git repo

  3. Microservice starts

  4. Microservice fetches config from Config Server

  5. Config is applied based on:

    • application name

    • profile (dev/prod)


πŸ”Ή Example Structure (Git)

config-repo/
β”œβ”€β”€ order-service.yml
β”œβ”€β”€ order-service-dev.yml
β”œβ”€β”€ payment-service.yml

πŸ”Ή Example Client Configuration

spring:
application:
name: order-service
config:
import: configserver:http://localhost:8888

πŸ”Ή Refreshing Config Without Restart

Use:

  • @RefreshScope

  • Actuator /refresh endpoint

@RefreshScope
@Component
public class ConfigBean {
@Value("${discount.rate}")
private int discount;
}

πŸ”Ή Advantages

βœ… Centralized config
βœ… Environment-based config
βœ… No redeployment for config change
βœ… Secure secrets management
βœ… Consistency across services


πŸ”Ή Disadvantages

❌ Additional infrastructure
❌ Config Server becomes critical dependency
❌ Needs HA setup


πŸ”Ή Real-World Example

πŸ’³ Change DB password in Git
β†’ Refresh Config
β†’ All services updated without restart


⭐ Interview One-Liner

β€œSpring Cloud Config externalizes configuration and allows centralized, environment-specific, and dynamic configuration management.”


πŸ”Ή Follow-Up Questions Interviewers Ask

  • How do you secure Config Server?

  • Difference between Spring Cloud Config & Kubernetes ConfigMaps?

  • How does @RefreshScope work internally?

Leave a Reply