Centralized Configuration
Centralized Configuration
In a monolith you manage one application.properties file. Deploy ten microservices across three environments and you suddenly have thirty configuration files to keep consistent. Change a shared database URL and you must update, rebuild, and redeploy every service. Spring Cloud Config solves this by introducing a dedicated Config Server that acts as the single source of truth for all configuration, serving properties to every microservice over HTTP at startup — and optionally at runtime without a restart.
How Spring Cloud Config Works
The architecture has two moving parts:
- Config Server — a standalone Spring Boot application that reads configuration from a backend store (typically a Git repository) and exposes it via a REST API.
- Config Client — any microservice that has the
spring-cloud-starter-configdependency. It contacts the Config Server during startup, downloads its properties, and merges them into itsEnvironmentbefore any beans are created.
Setting Up the Config Server
Create a new Spring Boot project and add the server dependency:
Enable the server with a single annotation on the main class:
Configure where it reads properties from in its own application.yml:
The server now serves configuration at the URL pattern /{application}/{profile}/{label}. For example, GET /order-service/production/main returns the merged properties for the order-service application in the production profile on the main branch.
Organising the Config Repository
A typical Git config repo layout looks like this:
Spring Cloud Config merges these files with a clear precedence (most specific wins):
- Service-specific + profile (e.g.
order-service/application-production.yml) - Service-specific default (e.g.
order-service/application.yml) - Shared + profile (e.g.
application-production.yml) - Shared default (
application.yml)
application.yml. Service-specific URLs and credentials belong in per-service files. This keeps individual service configs small and focused.
Configuring a Config Client
In each microservice, replace the default Spring Boot config starter with the Config Client:
Add the minimal bootstrap configuration. In Spring Boot 3 / Spring Cloud 2022+, bootstrap processing is disabled by default; re-enable it with the spring-cloud-starter-bootstrap dependency, or — the preferred modern approach — put Config Server coordinates directly in application.yml:
When the application starts, Spring Boot processes the spring.config.import entry before any other beans are created, fetches the remote properties, and injects them into the application context exactly as if they were local .yml files.
Securing the Config Server
The Config Server can expose database passwords, API keys, and other secrets. Protect it properly:
- HTTP Basic / OAuth2: Add
spring-boot-starter-securityto the Config Server and configure credentials. Clients supply them in thespring.cloud.config.usernameandspring.cloud.config.passwordproperties (stored in a local secret manager, not in the repo). - Symmetric encryption: The Config Server has built-in support for encrypting values with a shared secret. Store the encrypted value in Git as
{cipher}AQA...— the server decrypts it before serving. SetENCRYPT_KEYas an environment variable on the server host, never in config files. - Asymmetric encryption: Use a RSA key pair stored in a JKS keystore for higher security. The server holds the private key; the Git repo contains only ciphertext.
Network Topology in a Real Cluster
In production the Config Server itself runs as a service registered with Eureka (covered in Lesson 2). Clients discover it by service ID rather than a hardcoded URL:
This removes the last hardcoded URL from your clients. The startup sequence becomes: contact Eureka → discover Config Server → fetch config → finish starting up. The tradeoff is that Eureka must be reachable before any other service can start; this is why Eureka itself is typically deployed with multiple replicas and a fixed, well-known address.
Summary
Spring Cloud Config replaces the per-service application.yml chaos with a single Git-backed server that serves versioned, environment-aware, profile-specific configuration to every microservice. Clients connect at startup via spring.config.import, the server merges properties by precedence, and sensitive values can be encrypted at rest. In the next lesson you will see how to push updated configuration to running services without restarting them.