Spring Cloud Config Server

When we work on multiple microservices, we tend to maintain all the application properties either on the application itself or on the servers/containers we deploy the applications. This may involve the maintenance of all such property files.

Spring Cloud Config Server helps us externalize all such properties and maintain them separately in a GIT repository, from which the config server pulls the properties and serves all the applications that are configured as config clients.

Config Properties Repository

It is nothing but a GIT repository that contains all the property files of all the applications that are part of a system.

  • The property files can be of .properties or .yml extension.
  • The generic property file names start with application and the properties within such files are applicable to all applications.
    • application.yml, application-dit.yml, application-fit.yml, etc.,
  • The application-specific property file names start with the application name (ex: app-name), and the properties within such files are applicable only to that specific application.
    • app-name.yml, app-name-dit.yml, app-name-fit.yml, etc.,
  • The environment-specific property file names end with the environment name (like dit, fit, etc.,) with a hyphen between the application name and environment.
  • If we have a property defined in all the files, then the properties in the generic files are overridden by the properties in the application-specific files. Similarly, properties in the application-specific generic file are overridden by the environment-specific files.

Let's try to understand the different types of files that can be included within the config repository.

File Type File Name Description
Generic Files application.yml This applies to all applications in all environments.
  application-dit.yml This applies to all applications in the dit environment.
  application-fit.yml This applies to all applications in the fit environment.
  application-uat.yml This applies to all applications in the uat environment.
  application-prod.yml This applies to all applications in the prod environment.
Application Specific Files app-name.yml This applies to the application app-name in all environments.
  app-name-dit.yml This applies to the application app-name in the dit environment.
  app-name-fit.yml This applies to the application app-name in the fit environment.
  app-name-uat.yml This applies to the application app-name in the uat environment.
  app-name-prod.yml This applies to the application app-name in the prod environment.

Here is a sample config properties repository that contains the property files.

Building Config Server

Spring Cloud Config Server is a simple Spring Boot application that contains configuration to read properties from the config properties GIT repository created above. Let's try to build a config server application.

Use Spring Initializer to create the project by adding the Spring Cloud Config repository as shown below.

Add the @EnableConfigServer annotation to the main application class as shown below.

Makes the below changes to the application.properties file, so that the config server interacts with the local copy of the config properties repo.

  • By default, the config server runs on port 8888, which can be changed by adding the server.port property.
  • By default, the application reads properties from the main label/branch on the config properties repo, which can be changed as shown below.
  • By default, the local copy of the GIT repository clones the properties from the remote repo upon application startup. If we are making changes to our local copy of the config properties repo for testing, then we need to set the property clone-on-start to false, otherwise, the changes are overridden when the config server is started as it pulls the properties from the remote repo every time it starts.
server.port = 8888
spring.application.name = spring-cloud-config-server
spring.cloud.config.server.git.uri = file:///C:/Users/arun.choppara/Documents/spring-cloud-config-properties
spring.cloud.config.server.git.defaultLabel = master
spring.cloud.config.server.git.clone-on-start = false

Configure Config Server to connect to Remote Config Properties Repository

If we want to read properties from a remote config properties GIT repository, then use the GIT repo URL as shown below.

  • Create a new app key/password on the GIT account, which must be used as a password to access the GIT repo as shown below.
server.port = 8888
spring.application.name = spring-cloud-config-server
spring.cloud.config.server.git.uri = https://@bitbucket.org/randomcodez/spring-cloud-config-properties.git
spring.cloud.config.server.git.username = arunchoppara_git
spring.cloud.config.server.git.password = ATBBCkJDvBCXQ5JSkWcM235EASDET
spring.cloud.config.server.git.defaultLabel = master
spring.cloud.config.server.git.clone-on-start = false
spring.cloud.config.server.git.ignore-local-ssh-settings = true

Generating "App password" on BitBucket.

Generating "access token" on GitHub.

Run Config Server

After the configurations, run the application, which is available on port 8888 as defined.

Run Config Server on Different Profiles

If we want to build/run the config server for multiple profiles, then we need to define multiple properties files as shown below.

The application.properties file contains the below properties, which determine the active profile.

server.port = 8888
spring.application.name = spring-cloud-config-server
spring.profiles.active = local

The application-local.properties file contains the below properties, which are required to connect to the local copy of the config properties repo.

server.port = 8888
spring.application.name = spring-cloud-config-server
spring.cloud.config.server.git.uri = file:///C:/Users/arun.choppara/Documents/spring-cloud-config-properties
spring.cloud.config.server.git.defaultLabel = master
spring.cloud.config.server.git.clone-on-start = false

The application-dev.properties file contains the below properties, where we used the develop branch for properties.

server.port = 8888
spring.application.name = spring-cloud-config-server
spring.cloud.config.server.git.uri = https://@bitbucket.org/randomcodez/spring-cloud-config-properties.git
spring.cloud.config.server.git.username = arunchoppara_git
spring.cloud.config.server.git.password = ATBBCkJDvBCXQ5JSkWcM235EASDET
spring.cloud.config.server.git.defaultLabel = develop
spring.cloud.config.server.git.clone-on-start = false
spring.cloud.config.server.git.ignore-local-ssh-settings = true

The application-prod.properties file contains the below properties, where we used the master branch for properties.

server.port = 8888
spring.application.name = spring-cloud-config-server
spring.cloud.config.server.git.uri = https://@bitbucket.org/randomcodez/spring-cloud-config-properties.git
spring.cloud.config.server.git.username = arunchoppara_git
spring.cloud.config.server.git.password = ATBBCkJDvBCXQ5JSkWcM235EASDET
spring.cloud.config.server.git.defaultLabel = master
spring.cloud.config.server.git.clone-on-start = false
spring.cloud.config.server.git.ignore-local-ssh-settings = true

Test Config Server

As we discussed earlier, we can access the properties by specifying the application name, environment, and label on the config server as shown below.

Building Config Client

Spring Cloud Config Client is like any other Spring Boot application that gets configuration properties from a config server at runtime. Let's try to build a config server application.

Use Spring Initializer to create the project by adding the Spring Cloud Config repository as shown below.

Add the @RefreshScope annotation to the main application class as shown below, so that the application reads the updated properties when updates happen.

Now, the application is configured to consume properties from the config server, let's add a controller method that returns properties in the response.

Here the properties are grabbed using the property names using @Value annotation as shown below.

Run Config Client

After the configurations, run the client application, which is available on port 8080 as defined.

Test Config client

We can trigger the controller request that returns data containing the properties pulled from the config server as shown below. This clearly shows that the application is able to access the config properties from the config server at runtime.

Conclusion

Now, we know how to build a spring cloud config server and client, where the server serves the properties to the client applications at run time.

References