Spring Cloud Config Server

Nil Seri
Javarevisited
Published in
3 min readSep 12, 2021

--

Spring Cloud Config Server Development

Photo by sendi gibran on Unsplash

There are many sites and posts about the details of development with Spring Boot and Spring Cloud but this post will be just about implementation and some notes about what I did and why I did it that way.

Config Server Project

If you do not want to use git but a local path for properties file location, you should add “native” to your spring profiles in your config server project:

spring.profiles.active=native,dev

So this is my final application.properties file for my config server project:

server.port=8888
spring.application.name=senoritadev-config-service

spring.profiles.active=native,dev

What makes a project a config “server” is this dependency:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

Do not forget to add @EnableConfigServer annotation to your Application class:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

Your Main Project:

In the earlier versions, you needed to create a separate properties file named “bootstrap.properties” and add the properties added below to this file. “bootstrap.properties” file is different from “application.properties” as it is loaded before main application context.

So go to your main project where you will be using these properties and add spring.application.name to your “application.properties” there.

spring.config.import=optional:configserver:http://localhost:8888
spring.application.name=senoritadev-batch-jobs
spring.profiles.active=dev

You need to add “optional” in front of your config url in properties so that if even if there is a failure, your main application will still be up and running.

After that, in your main project, just add this dependency (since your main project is the config server’s client).

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Your Main Project’s Application Properties In Config Server

In your config server project, add a file named “senoritadev-batch-jobs-dev.properties” in the same level as config server’s own application.properties file. Pay attention to how I gave the file name: my-batch-app-name + “-dev” + “.properties”. You can add a default “senoritadev-batch-jobs.properties” and also for (environmental) profile names (like senoritadev-batch-jobs-test.properties, senoritadev-batch-jobs-preprod.properties, etc.).

Syntax

You can see an example syntax of property keys application.properties here. “.” is used to group and “-” is equivalent to uppercase in Java class (“job-count” maps to “jobCount”).

Config Class

It would be better to have a separate config class for holding your properties (in your main project) rather than @Value(“${my.config.key}”) fashion in your classes.

This is a good practice for both separations of concerns and refresh policy. When you update your property value and do a refresh to apply (will mention refresh later), your classes, using these values, might be blocking each other during refresh.

However, a single config bean with @ConfigurationProperties annotation is much more an efficient way to refresh context faster.

You can read what goes on in the background here.

Get / Refresh Values

To refresh property values that your project uses after update in config server-side (manually, without Spring Cloud Config):

To GET your projects properties

curl -L -X GET ‘http://localhost:8888/senoritadev-batch-jobs/dev'

After you edit senoritadev-batch-jobs-dev.properties, use this cURL request to refresh at your project’s side (8081 is your main project’s server port). List of added/updated properties are sent as a response to this POST request.

curl -L -X POST ‘http://127.0.0.1:8081/actuator/refresh' \
-H ‘Content-Type: application/json’ \
— data-raw ‘’

Happy Coding!

--

--

Nil Seri
Javarevisited

I would love to change the world, but they won’t give me the source code | coding 👩🏻‍💻 | coffee ☕️ | jazz 🎷 | anime 🐲 | books 📚 | drawing 🎨