Overview

Chapter 3. Controlling your configuration with Spring Cloud configuration server

This chapter explains why configuration management becomes a major concern in microservice systems and why it should be treated as a first-class architectural problem rather than an afterthought. Hard-coding values or scattering property files across many services creates operational complexity, configuration drift, and costly outages as systems grow. The recommended approach is to segregate configuration from code, abstract access through a service, centralize configuration as much as possible, and harden the solution so it is highly available and easy to manage in a cloud environment.

To illustrate those ideas, the chapter introduces Spring Cloud Config Server as a practical way to serve configuration data to Spring Boot services at startup. The config server is shown as a REST-based service that can back onto a filesystem or a Git repository, with Git emphasized as the better long-term choice because it naturally supports version control and easier integration into deployment pipelines. The book walks through setting up the server, organizing configuration by application and profile, and showing how a service can request default or environment-specific properties from the server during bootstrapping.

The second half connects a sample licensing microservice to the config server and expands it into a database-backed service using Spring Data JPA and PostgreSQL. The service reads startup details such as the config server URI and active profile from a bootstrap file or environment variables, then pulls its application-specific settings from the centralized configuration source. The chapter also covers refreshing properties with Spring Cloud’s refresh support and addresses security by encrypting sensitive values like database passwords, including the option of decrypting them on the client side so secrets are not exposed in plain text when configuration is retrieved.

Figure 3.1. The application configuration data is read during the service bootstrapping phase.

Figure 3.2. Configuration management conceptual architecture

Figure 3.3. Spring Cloud configuration exposes environment-specific properties as HTTP-based endpoints.

Figure 3.4. Retrieving default configuration information for the licensing service

Figure 3.5. Retrieving configuration information for the licensing service using the dev profile

Figure 3.6. Retrieving configuration information using the dev profile

Figure 3.7. The configuration the licensing service loads can be checked by calling the /env endpoint.

Figure 3.8. Using the /encrypt endpoint you can encrypt values.

Figure 3.9. While the spring.datasource.password is encrypted in the property file, it’s decrypted when the configuration for the licensing service is retrieved. This is still problematic.

Figure 3.10. With client-side decryption turned on, sensitive properties will no longer be returned in plain text from the Spring Cloud Config REST call. Instead, the property will be decrypted by the calling service when it loads its properties from Spring Cloud Config.

Summary

  • Spring Cloud configuration server allows you to set up application properties with environment specific values.
  • Spring uses Spring profiles to launch a service to determine what environment properties are to be retrieved from the Spring Cloud Config service.
  • Spring Cloud configuration service can use a file-based or Git-based application configuration repository to store application properties.
  • Spring Cloud configuration service allows you to encrypt sensitive property files using symmetric and asymmetric encryption.

FAQ

What is the main goal of using Spring Cloud Config in a microservices architecture?Spring Cloud Config separates configuration from application code so services can be deployed as immutable artifacts and still receive environment-specific settings at startup from a centralized repository.
Why is hard-coding configuration values directly in service code considered a bad practice?Hard-coded values require recompilation and redeployment for every change, which increases maintenance effort and can lead to errors, configuration drift, and outages.
What are the four key configuration management principles discussed in this chapter?The four principles are: segregate configuration from code, abstract access through a service interface, centralize configuration in as few repositories as possible, and harden the solution for high availability and redundancy.
Why does configuration management become more complex in cloud-based microservices?Because there may be hundreds of services and multiple instances of each one, creating many environment-specific configuration files and making versioning, deployment, and maintenance much harder.
What is the purpose of the Spring Cloud Config server?The Spring Cloud Config server provides a REST-based service that serves configuration data to client applications from a backend repository such as a filesystem, Git, Consul, Eureka, or another supported source.
How does a Spring Boot service connect to Spring Cloud Config?A service uses a bootstrap.yml file to define its application name, active profile, and the URI of the config server so it can fetch configuration before the rest of the application starts.
Why is the spring.application.name property important when using Spring Cloud Config?It must match the configuration directory or application name in the config repository so the config server knows which set of properties to return for that service.
Why does the chapter recommend Git over a filesystem for production configuration storage?Git is better for cloud environments because it provides version control, integrates well with deployment pipelines, and avoids the operational burden of maintaining shared filesystem mounts across servers.
How are configuration changes refreshed in running Spring Boot applications?Spring Boot applications normally read configuration at startup, but you can use @RefreshScope and the /refresh endpoint to reload custom configuration values without restarting the service.
How are sensitive properties like database passwords protected in Spring Cloud Config?Sensitive values can be encrypted using Spring Cloud Config’s encryption support. By default the server decrypts them before sending them, or you can configure client-side decryption so the service decrypts them itself using the same encryption key.