alexgorbatchev

Saturday, January 23, 2016

Spring Cloud - Config Server



Setting up the Spring Cloud Config Server

In this post we will be setting up a Spring Cloud Config Server to externalize configuration properties for future Grails 3 applications.  This will be a bare bones application without security for now.

Setup

The easiest method to get started is to create a Spring Boot application from the Spring Initializr and include the Spring Cloud Config Server.  I will be using a Gradle project and Spring Boot 1.3.2.  Enter your desired group (com.morkenbarware) and artifact (config) in the Spring Initializr and download a new project.

Spring Initializr

Configuration

Configuration changes to this ConfigApplication will be pretty minimal as most of the default Boot application settings will be sufficient for now, but we do need to add an annotation and a few config settings to enable the configuration endpoints.

Annotation:

In order to enable the configuration endpoints that other applications will use, we need to add one annotation to the Application class (ConfigApplication) in the application.

Add the import import org.springframework.cloud.config.server.EnableConfigServer; and the annotation @EnableConfigServer to the Application (ConfigApplication) class as follows:


Configuration Settings:

Spring Cloud Config Server primarily uses Git repositories to store properties, so we first need to create a new Git repository to hold our application properties.

Create Local Git Repository (to hold our external properties)

In production this would probably be a web-based Git repo or at least a remote repo, but for simplicity, I will create a local repo to hold my configuration information.  I keep all of my development projects in a projects folder under my user directory, so my config repo will be in /home/me/projects/config.  In this directory, create a file application.properties or application.yml depending on your configuration preference.

In addition to the application.properties or yml file create another file called example.properties or example.yml depending on your configuration preference.
Once these two files have been created, commit the changes (git commit -am 'initial properties') to the Git repo.

By adding an application.properties/yml file to the repo, any Cloud Config Client application that connects to this Config Server will receive these properties unless overriden by a more specific property/yml file.  In our case, when we specify:
 audit.config.source = cloud in the application.yml and
 audit.config.source = cloud example in the example.yml file,
if we create an application called example, it will receive "cloud example" because the example.yml file is more specific to that application. (application.yml > example.yml > example-production.yml)

Config Server Settings:

Next, we need to configure the actual Config Server Application.  We need to "connect" to our local Git repo using spring.cloud.config.server.git.uri and expose the config server on the conventional cloud config port 8888 using server.port. Your application.properties/yml should look like the following.


With that done... let's fire up the config server and see what we have accomplished.
./gradlew bootRun

After executing the gradle task, you should see a lot of output and eventually something that says:
Tomcat started on port 8888.  If you go to the running application and change the url to http://localhost:8888/example/development/ you should see two different property values for audit.config.source and if you go to http://localhost:8888/someapp/development/ you will only see one audit.config.source from application.properties that applies to all applications.

Next, we will create a Grails 3 application to connect to our Cloud Config Server and see how they work together.

References:
  • http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html