Introduction
As our applications grow larger and more complex, managing configuration across multiple environments and services becomes increasingly difficult. Hard-coding configuration properties directly into each service is problematic - it leads to duplication, makes changes cumbersome, and can risk exposing sensitive information in code repositories. The solution is to externalize configuration into a centralized service that all applications can access.
What is the Spring Cloud Config Server?
The Spring Cloud Config Server provides a centralized external configuration service that applications can connect to and retrieve their configuration from, instead of bundling configuration files inside each application. This brings several key benefits:
- Separates configuration from application code
- Easier management of configurations across multiple services/environments
- Ability to change configurations dynamically without restarting services
- Configurations can be stored securely external to application code
Setting Up a Local Config Server
Let's walk through setting up a local Spring Cloud Config Server instance that serves configuration properties from the local filesystem.
1. Create a new Spring Boot project, adding the "Config Server" dependency.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2. In the main class, enable the "@EnableConfigServer" annotation:
package com.aggrandizer.BrainiacBox.local_config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer // This annotation is used to make this application a config server
public class LocalConfigApplication {
public static void main(String[] args) {
SpringApplication.run(LocalConfigApplication.class, args);
}
}
3. Configure the server to use the local filesystem by adding this to application.properties:
server:
port: 8191
spring:
profiles:
active: native
This tells the config server to load configuration files from the local ~/config-repo directory.
4. Create the ~/config-repo directory and add property files for each service/application organized by name. For example:
service:
greeting: "Hello from service 1!"
id: 839210839
# Path: src/main/resources/config/service1.yml
service:
greeting: "Hello from service 2!"
id: 4372489743
# Path: src/main/resources/config/service2.yml
That's it! The config server will now serve up configuration based on the service name (service1/service2) by looking in the corresponding service.yml file.
Connecting Applications to the Config Server
To connect an application to the config server and have it load external configuration properties, you can use the "@ConfigurationProperties" annotation along with "@EnableConfigurationProperties":
@ConfigurationProperties("service")
public class ServiceConfigProperties {
private String property;
// getters/setters
}
@SpringBootApplication
@EnableConfigurationProperties(ServiceConfigProperties.class)
public class MyApplication {
@Autowired
private ServiceConfigProperties serviceConfig;
//...
}
The property value will now be loaded from the service.yml file served by the config server at runtime. Any changes to this file will be immediately picked up without requiring an application restart.
Wrapping Up
In this post, we learned how to set up a local Spring Cloud Config Server instance to externalize configuration properties for our applications. In the next post, we'll look at using a git-based centralized config server solution like Github to securely manage configurations across environments.
The full code examples are available at[repo link].
Let me know if you have any other questions!