You first need a Config Service to act as a sort of intermediary between your Spring applications and a (typically) version-controlled repository of configuration files. You can use Spring Cloud’s @EnableConfigServer to standup a config server that can communicate with other applications. This is a regular Spring Boot application with one annotation added to enable the config server. The following listing (from configuration-service/src/main/java/com/example/configurationservice/ConfigurationServiceApplication.java) shows such an application:
package com.example.configurationservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigurationServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigurationServiceApplication.class, args);
}
}
The Config Server needs to know which repository to manage. There are several choices here, but start with a Git-based filesystem repository. You could as easily point the Config Server to a Github or GitLab repository. On the file system, create a new directory and run git init in it. Then add a file called a-bootiful-client.properties to the Git repository. Then run git commit in it. Later, you will connect to the Config Server with a Spring Boot application whose spring.application.name property identifies it as a-bootiful-client to the Config Server. This is how the Config Server knows which set of configuration to send to a specific client. It also sends all the values from any file named application.properties or application.yml in the Git repository. Property keys in more specifically named files (such as a-bootiful-client.properties) override those in application.properties or application.yml.
Add a simple property and value (message = Hello world) to the newly created a-bootiful-client.properties file and then git commit the change.
Specify the path to the Git repository by specifying the spring.cloud.config.server.git.uri property in configuration-service/src/main/resources/application.properties. You must also specify a different server.port value to avoid port conflicts when you run both this server and another Spring Boot application on the same machine. The following listing (from configuration-service/src/main/resources/application.properties) shows such an application.properties file:
server.port=8888
spring.cloud.config.server.git.uri=${HOME}/Desktop/config
This example uses a file-based git repository at ${HOME}/Desktop/config. You can create one easily by making a new directory and running git commit on the properties and YAML files in it. The following set of commands does this work:
$ cd ~/Desktop/config
$ find .
./.git
…
./application.yml
Or you could use a remote git repository (such as Github) if you change the configuration file in the application to point to that instead.