Spring Cloud Config Server with Multiples Repositories

Fernando Soliva
Jun 18, 2018 · 4 min read

It is a fast overview about Spring Cloud Config Server and one of most useful features for me in this service, multiple repositories as property source. Spring Cloud Config Server uses a git repository as property source or a file in your file system. The client can fetch configurations through a simple HTTP GET request. Java developers using Spring Framework can use the configuration client as you can see here.

The Config Server

First of all we need our server working. To simplify I prepare a git repository with the Config Server pre configured or you can use this docker image jonthecode/config-server-example to run a simple example. To run a new config server from scratch you can use the Spring Getting Started, it is very easy.

As said before the properties will be fetch from a git repository so let’s configure that.

First of all you will need clone the repository running in your terminal git clone https://github.com/solivaf/sc-config-server-example.git . After cloning the project you will find a file named application.yml in y src/main/resources/. This file has the app configuration as server port and repository URL.

# application.ymlspring:
cloud:
config:
server:
git:
uri:
https://github.com/solivaf/config-properties-foo

Navigate to repository URL you will find a repository with three configuration files: application.yml, application-prod.yml and config.properties.

Let’s see how it works. Run the command below:

$ mvn spring-boot:run

And performing the command

$ curl localhost:8080/message-dev.properties
bar.foo: testDevPropertiesYml
foo.bar: testDevProperties

Or

$ curl localhost:8080/application-prod.yml
bar:
foo: testProdPropertiesYml

Or even using the pattern {application}/{profile} as you can see below

$ curl localhost:8080/fooapp/prod
{
"name": "fooapp",
"profiles": [
"dev"
],
"label": null,
"version": "430031ed47d68d5e1681095f091e9bde9bb07515",
"state": null,
"propertySources": [
{
"name": "https://github.com/solivaf/config-properties-foo/application-dev.yml",
"source": {
"bar.foo": "testDevPropertiesYml"
}
},
{
"name": "https://github.com/solivaf/config-properties-foo/application.yml",
"source": {
"bar.foo": "testPropertiesYml"
}
}
]
}

In this last request we got a long response. By default, spring will return the application-{profile}.yml file and the default file if these file exists.

But can we use more than one repository for config server instance? Let’s see.

Config Server with Multiples Repositories

If you run the config server you will realized that is a small and very simple application but imagine if we have 15 applications and they don’t share the same repository. Now with the we have until we need deploy 15 config server instances, one for each application.

The Spring Cloud Config Server provides a configuration with multiples repositories and they can be accessed by url like the first example. The config server will fetch each configuration by a pattern, so your label {application} in the path will be the key to find the correct repository. Let’s configure our application.yml in our config server.

Until now we have this application.yml:

spring:
cloud:
config:
server:
git:
uri:
https://github.com/solivaf/config-properties-foo

Now we should add our additional repositories as you can see below:

spring:
cloud:
config:
server:
git:
uri:
https://github.com/solivaf/config-properties-foo
repos:
appFoo:
pattern: app-foo
uri:
https://github.com/solivaf/config-properties-bar

Restart the config server and perform the requests below.

$ curl localhost:8080/fooapp/prod
{
"name": "fooapp",
"profiles": [
"prod"
],
"label": null,
"version": "8686fb74f9af0aead98bd20d6e20e84a37028781",
"state": null,
"propertySources": [
{
"name": "https://github.com/solivaf/config-properties-foo/application-prod.yml",
"source": {
"bar.foo": "testProdPropertiesYml"
}
},
{
"name": "https://github.com/solivaf/config-properties-foo/application.yml",
"source": {
"bar.foo": "testPropertiesYml"
}
}
]
}

Now we can see the repository used to application fooapp, as we don’t have any pattern mapped to this application, the config server will use the default app, now if we specify the pattern app-foo which is mapped in our config-server property file we should get another repository as response.

$ curl localhost:8080/app-foo/prod
{
"name": "app-foo",
"profiles": [
"prod"
],
"label": null,
"version": "f34ced0565042be4cf87c937c1dab2703e0b8ed2",
"state": null,
"propertySources": [
{
"name": "https://github.com/solivaf/config-properties-bar/app-foo-prod.yml",
"source": {
"foo.bar": "testProdPropertiesYml"
}
},
{
"name": "https://github.com/solivaf/config-properties-bar/application-prod.yml",
"source": {
"foo.bar": "testProdPropertiesYml"
}
},
{
"name": "https://github.com/solivaf/config-properties-bar/application.yml",
"source": {
"foo.bar": "testPropertiesYml"
}
}
]
}

Now, we have the correct repository which was mapped in our property file and all files which represents our app-foo application. The response order represents the hierarchy of the file being the most priority the first file in the list.

That’s It!

As simple as it can be, Spring Config Server helps a lot when we you manage a lot of configuration properties. It is easy to use and deploy and not only Java developers can use this service.

I hope this short story could help you in some way. See you in the next short story.

jamalsoliva

desc

Fernando Soliva

Written by

Software Engineer - Go Prog - Kubernetes - AWS

jamalsoliva

desc