Spring Cloud Config

Doğukan HAN
5 min readAug 28, 2022

Hi everybody! Today I’m writing about Spring Cloud Config.

One of the challenging things in software development is organizing configuration files. Spring Cloud Config helps your applications for accessing configuration files with an API. You can manage different conf-files for different applications and environments at once with SCC. Even if you don’t use Spring, you can make HTTP requests to the API.

Content

  1. Create a Spring Cloud Config Application
  2. Create the Properties File
  3. Access SCC by Postman
  4. Create Client Applications and Connect them to SCC
  5. Encrypting the Values in the Properties.

1. Spring Cloud Config Application

I added only Config Server to keep it simple in Spring Initializr. Everything else depends on your implementation. (You can access Spring Initializr from here.)

The first step is you must add @EnableConfigServer to the application class. This annotation enables the application to be a Spring Config Server.

@SpringBootApplication
@EnableConfigServer
public class ConfServerApplication {

public static void main(String[] args) {
SpringApplication.run(ConfServerApplication.class, args);
}

}

The second step is setting up the properties file. The most important thing is the URL for the properties repository. Create a folder and give the path. We will add configurations to the folder later.
Also, Specify a different port so that the client and server can run at the same time. You can also give a name to the application( Not required).

spring.application.name=conf-server
server.port=8880
spring.cloud.config.server.git.uri=C:\\Users\\dogukan\\Desktop\\config

2. Create the Properties Repository

We have to initialize a git repository and add some configurations to the folder.

Let’s create files first so we can commit. I created four different files. Each has its scope.

  1. application.properties
    It matches every application.
  2. conf-client.properties
    It matches the application whose name is conf-client in every profile.
  3. conf-client-dev.properties
    It matches the application whose name is conf-client and whose profile is dev.
  4. conf-client-prod.properties
    It matches the application whose name is conf-client and whose profile is prod.

Files contain a key and a value. Values are different in each property file to check if it works. For example, conf-client-dev.properties looks like this:

message=Message from dev

Then write magic commands to create the first commit. Make sure you are working in the master branch. Otherwise, change the default branch in the Spring Config Server.

git init
git add .
git commit -m “Initial Commit”

3. Access SCC by Postman

You can start the Configuration Server after creating the git repository. You can call the config API to see if everything is working.

Try this link for our configuration http://localhost:8880/conf-client/prod
Plenty of option is available to call the API. The most important ones:

  1. http://localhost:8880/{applicationName}/{profile}
  2. http://localhost:8880/{applicationName}/{profile}/{gitBranch}

You will see plenty of property sources when you call the API. These are available for your application. If both of any available configuration has the same keys, the application selects the value that is in the most specific file. What does it means if you have the same key in both application.properties and conf-client.properties, you will see the value from the conf-client.properties as long as your application’s name is conf-client.

{
"name": "conf-client",
"profiles": [
"prod"
],
"label": null,
"version": "0465db7daba8dde0f2c7c2d45e66900a5f217ccf",
"state": null,
"propertySources": [
{
"name": "conf-client-prod.properties",
"source": {
"message": "Message from prod"
}
},
{
"name": "conf-client.properties",
"source": {
"message": "Message from conf-server"
}
},
{
"name": "application.properties",
"source": {
"message": "general message"
}
}
]
}

4. Create Client Applications and Connect them to SCC

I didn’t add too much dependency. These are just what we need.

You have to specify the spring.application.name. Make sure it matches the name of your configuration. You have to give the URL of the configuration API. Lastly, set the active profile so you can use different configuration files for your application.

spring.application.name=conf-client
spring.config.import=configserver:http://localhost:8880/
spring.profiles.active=dev

You can create a controller to see if everything is working as excepted.

@RefreshScope
@RestController
class MessageRestController {

@Value("${message:Hello default}")
private String message;

@RequestMapping("/message")
String getMessage() {
return this.message;
}
}

You can make a request to “http://localhost:8080/message” to see ‘Message from dev’.

Change the active profile or application name to see the message value in the different configuration files.

5. Encrypting the Values in the Properties.

If you need security, you can encrypt values in the properties file. After encryption, you can clearly store the files anywhere.

The first step is generating a key-pair. Then add config-server.jks to the resources folder of the config server.

keytool -genkeypair -alias config-server-key \
-keyalg RSA -keysize 4096 -sigalg SHA512withRSA \
-dname ‘O=Dh’ \
-keypass keysecret -keystore config-server.jks \
-storepass storesecret

Add these properties to the config server’s configuration file. Make sure the settings are the same as in the command you used to generate the key.

encrypt.keyStore.location=classpath:/config-server.jks
encrypt.keyStore.password=keysecret
encrypt.keyStore.alias=config-server-key
encrypt.keyStore.secret=storesecret

The best way to encrypt your values is using the config server API. The path for encryption is ‘/encrypt’. -> http://localhost:8880/encrypt

The API will give you ciphertext that you should copy and paste to the property file. You must add the ‘{chiper}’ notation before the value so that the Configuration Server decrypts before sending.

Example:

password={cipher}AgAajPWc7lBTkeIvQIW364RNJc6Qhdg1xeNdx...

Conclusion

You can store your application-specific configs easily and securely. The config applications need almost zero coding. But don’t forget these rules:

  1. Always commit your changes in properties.
  2. Make sure you are working in the right branch. The default branch is master, but you can change it.
  3. Make sure you match the application and the related property file names.
  4. Start the config server before the client application.

You can download the source codes here: https://github.com/dogukanhan/spring-cloud-config-example

Resources:

  1. https://cloud.spring.io/spring-cloud-config/reference/html/
  2. https://github.com/spring-cloud/spring-cloud-config
  3. https://www.baeldung.com/spring-cloud-configuration

--

--