Centralized config server concept is mostly popular for microservices. For monolithic applications, we maintained a properties file with application’s configurable parameters. Application properties are tied with the application code and then an application container restart is required to reflect any changes in values of the properties file. For Micro-Services, we can have multiple instance of same application. Managing configuration for all the instances individually is a hectic task. As a result to simplify things and do it more efficiently, centralized config server is a possible solution. It works by maintaining configurations in a single place and all the applications pull their configuration from this config server.

1. Building the Config Server It is very easy to build a config server with Spring Boot. ‘Bootstrap’ the application from

start.spring.io. I added Config Server dependency in pom.xml. This is final pom.xml file –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>bd.com.ronnie</groupId>
<artifactId>demo-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo-config-client</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR5</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

Our client application will consume configuration from config server. So it will have very few config values in

bootstrap.properties file –

spring.application.name=demo-config-client
    spring.cloud.config.uri=http://localhost:8888
    spring.cloud.config.fail-fast=true

spring.application.name property value is used to connect with config servers property file.

spring.cloud.config.fail-fast=true is used to make client application fail if config server is down. Now we will use property values consumed from config server in our client app. This is our DemoConfigClientApplication.java

package bd.com.ronnie.democonfigclient;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    @SpringBootApplication
    public class DemoConfigClientApplication {

        public static void main(String[] args) {
            SpringApplication.run(DemoConfigClientApplication.class, args);
        }
    }

    @RestController
    class HelloController {

        class Greeting {
            private String greeting1;
            private String greeting2;
            public Greeting(String greeting1, String greeting2) {
                this.greeting1 = greeting1;
                this.greeting2 = greeting2;
            }

            public String getGreeting1() {
                return greeting1;
            }

            public void setGreeting1(String greeting1) {
                this.greeting1 = greeting1;
            }

            public String getGreeting2() {
                return greeting2;
            }

            public void setGreeting2(String greeting2) {
                this.greeting2 = greeting2;
            }
        }

        @Value("${greeting1}")
        private String greeting1;

        @Value("${greeting2}")
        private String greeting2;

        @GetMapping(name = "greeting")
        public ResponseEntity<Greeting> greeting() {
            Greeting greeting = new Greeting(greeting1, greeting2);
            return new ResponseEntity<>(greeting, HttpStatus.OK);
        }

    }

We created a request mapping for /greeting here. With Value annotation we are assigning property values to greeting1 and greeting 2 variables. Start both applications and test visit URL 

localhost:8080/greetingcentralized-config-server-with-spring-boot It is also possible to add security for the configurations. To do this we need to add Spring Security dependency in config server application and set credentials via security.user.name(i.e user) and security.user.password(i.e 123456) properties in application.properties file. Then from client application, we will pass credentials via spring.cloud.config.username(i.e user) and spring.cloud.config.password(i.e 123456) properties in bootstrap.properties file. This link for the whole project https://github.com/ronniegnr/demo-config-01  

Previous ArticleNext Article

Leave a Reply

Your email address will not be published. Required fields are marked *