Spring boot with embedded config server via spring cloud config

When it comes to micro-services, it is really normal to have a configuration server that all your services will connect to fetch its own configuration but what about if you just need to externalize your configuration and make it manageable via source control like Git and your infrastructure is not yet ready for micro-services deployment and operation model.

manage-distributed-configuration-and-secrets-with-spring-cloud-and-vault-spring-io-2017-10-638

What if you have spring boot app and you want to use spring cloud config semantic to do the same for you , is it possible to start embedded spring cloud config inside the your spring boot app to fetch its configuration remotely from Git for example ? the answer is yes and i am going to show how :

configServerBoot

The steps needed are the following :

1- add the following as a maven dependency in your spring boot app pom.xml:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

2- add the spring cloud configuration to point to your Git configuration server in bootstrap.yml file of your spring boot application :

spring:
# change the application name for your project name , this name is reserved for the maven archetype code generation
application:
name: Application
# DO NOT FORGET TO ADD YOUR YAML CONFIG FILE IN config server as shown below
spring:
cloud:
config:
failFast: true
server:
bootstrap: true
git:
uri: https://github.com/Romeh/config.git

view raw
bootstrap.yml
hosted with ❤ by GitHub

3- add your application configuration yml file in the server with the same spring boot application name

4- start your app and you should see in the console that is fetching your yml file from the remote Git server as being shown below

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.6.RELEASE)
[2017-12-03 19:48:24] [main] INFO o.s.c.a.AnnotationConfigApplicationContext – Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c2db130: startup date [Sun Dec 03 19:48:24 CET 2017]; root of context hierarchy
[2017-12-03 19:48:24] [main] INFO o.s.c.c.s.e.NativeEnvironmentRepository – Adding property source: file:/var/folders/dl/660f5pxs0q18dmp4zh0smfgh0000gn/T/config-repo-5816360046828309118/Application.yml
[2017-12-03 19:48:24] [main] INFO o.s.c.c.s.e.NativeEnvironmentRepository – Adding property source: file:/var/folders/dl/660f5pxs0q18dmp4zh0smfgh0000gn/T/config-repo-5816360046828309118/application.yml
[2017-12-03 19:48:24] [main] INFO o.s.c.a.AnnotationConfigApplicationContext – Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c2db130: startup date [Sun Dec 03 19:48:24 CET 2017]; root of context hierarchy
[2017-12-03 19:48:24] [main] INFO o.s.c.b.c.PropertySourceBootstrapConfiguration – Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='https://github.com/Romeh/config.git/Application.yml'%5D, MapPropertySource [name='https://github.com/Romeh/config.git/application.yml'%5D%5D%5D
[2017-12-03 19:48:29] [main] INFO com.test.sampleapp.Application – No active profile set, falling back to default profiles: default
[2017-12-03 19:48:34] [main] INFO o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext – Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@159e366: startup date [Sun Dec 03 19:48:34 CET 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@140c9f39
[2017-12-03 19:48:35] [main] INFO o.s.cloud.context.scope.GenericScope – BeanFactory id=854c5363-0b16-3c3b-9814-dceae903c2af

view raw
config.log
hosted with ❤ by GitHub

Remarks :

  1. you should in production enable HTTPS and authentication between your embedded config server and Git repo
  2. you can use spring cloud config encryption to encrypt any sensitive data in your configuration like passwords
  3. you should use spring streams with kafka or other options(ex: spring cloud bus) to push the configuration change and force reload of the values without forcing yourself to restart the app

References :

  1. Spring cloud config : https://cloud.spring.io/spring-cloud-config/
  2. Code sample in GitHub : https://github.com/Romeh/spring-boot-sample-app

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s