Intro of Spring Cloud Config

Sometimes we have to keep some of the configurations code out side of our application just to make sure the changes in those config wont make us to re-deploy our whole application again and again. There could be number of ways to achieve the same but in this tutorial, we are going to use Spring’s cloud config way.

Even in Spring cloud config the end goal can be achieved through number of ways, like keeping configuration in local, in GIT or some where else remotely. This tutorial going to use the GIT way. We are going to keep our configurations in GIT and our server will access those from GIT and client will access those configs through config server.

So to achieve the same, we can divide our modules in three parts.

  • Configurations,
  • Server and
  • Client

Configurations

Configurations are something which holds the actual data which might vary environments to environments based on need or the actual values which will be replaced with the placeholders in our code.

Let’s create three property files having different configurations. i.e.

lets have below data in above files respectively

  • msg = Hello from Development environment.
  • msg = Hello from Production environment.
  • msg = Hello from config server.

You checkin those files in GIT and keep the repo URL handy.

Server

Server is the thing which will access those property bundles on demand from client. Server will have the information of these bundles. Let’s get one server ready.

We have created one simple Spring Boot ready using spring-initializer with the below two main and mandatory dependencies

To make our application to behave like a server, we need to have few action on place like annotate the controller with @EnableConfigServer and have below config ready in bootstrap.yml. Your default downloaded application wont have bootstrap file so please delete application.properties and put the config in bootstrap.yml.

server:
   port: 8888
spring:
   cloud:
      config:
         enabled: false
         server:
            bootstrap: true
            git:
               uri: https://github.com/tektutorial/config-server-repo.git
               username: <your_git_username>
               password: <your_git_password>

The spring.cloud.config.server.git.uri attribute will keep the information of your configuration’s GIT location.

Build it and start your Application.java. To check whether your server application is able to come up successfully and can access the configuration in GIT, hit below urls locally.

http://localhost:8888/config-server-client/development this hit will give you something like below screenshot. Try the same for /production url as well.

You might be asked for username and password at the login page. UserId will be “user” and password will be auto generated and can be found in the console window of your IDE while starting of your application. Like below

Client

Client will be the consumer of your configurations via server. Let’s create one spring boot application using spring spring-initializer adding below mandatory dependencies like below.

Once you download your spring boot application, let’s create on rest controller like below.

@RestController
public class ClientRestController {

    @Value("${msg:Config Server is not working...please check}")
    private String msg;
 
    @RequestMapping("/message")
    String getMsg() {
        return this.msg;
    }
}

Your bootstrap.yml need to have something like below configs to make it actual spring config client.

spring:
   application:
      name: config-server-client #this app name should be as same as your property name in config repo.
   profiles:
      active: development
   cloud:
      config:
         fail-fast: true #with this tag, application wont start if there is anything wrong.
         enabled: true
         uri: http://localhost:8888   #Your local server's address
         username: user #default user name
         password: 8237a670-308f-4c1a-8be5-0ebdc30e8a3a #this password can be fetched from server console window as shown above.
server:
   port: 8082

Once you build and start your client application and hit /message you will see something like below.

Since our active profile was set to ‘development’, we got message from development property files. Response will be different if you keep your active profile as ‘production’.

Good to read why bootstrap.yml not application.properties

The above code repos can be found at below locations.

Configuration, Server, Client

Hope this must have cleared some of your doubt about Spring Cloud Config.

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.