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 :

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

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
Advertisements

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 )

w

Connecting to %s