How to build a configuration server using Spring cloud config

 

        Are you tired of maintaining your application’s property files? To track changes in property files as the application grows? Facing difficulties to sync property files for different environments? Are you looking for a central place to manage external properties for applications across all environments? A Configuration server using Spring Cloud Config could be a solution for you.

Spring Cloud Config provides server and client-side support to externalized configuration in a distributed system. With the Config Server you can have a central place to manage external properties for applications across all environments. As an application moves through the deployment pipeline from development to test and finally to production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate.

Configuration Server:

A Configuration server using Spring Cloud Config can be developed using simple steps as:

  1. Create a Git repository for property files. e.g. amisoft_property_configuration.
  2. Create a Spring Boot project using IDE or https://start.spring.io.
  3. Add the below dependencies in the project:
    <dependencies>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-config-server</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-security</artifactId>
       </dependency>
    
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
       </dependency>
    </dependencies>
  4. Delete the “application.properties” . ( Can ignore the step, if prefer  *.properties files instead of yml)
  5. Create an “application.yml” file with the below configuration:
    ---
    
    
    spring:
      cloud:
        config:
          server:
            git:
              uri: https://github.com/amitaucs/amisoft_property_configuration.git # Git repository loction of property files
              search-paths: 'amisoft/*/*'  # Folder structure in which property files located.
              #username: needs if private repository. The Git user name.
              #password: needs if private repository. The Git password.
      application:
        name: amisoft-config-server
    security:
      basic:
        enabled: true
      user:
        name: amisoft
        password: amisoftpass
    server:
      port: 9000
  6. Go to the main application file which will have the name as <project name>Application.java. e.g. ConfigServerApplication.java.
  7. Add the annotation “@EnableConfigServer” at the class.
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApplication {
    
       public static void main(String[] args) {
          SpringApplication.run(ConfigServerApplication.class, args);
       }
    }
  8. Run the class mentioned in step 6.
  9. Application should be available at port 9000. ( or the port mentioned in “server.port” in “application.yml”).
  10. Access the url http://localhost:9000/health“.
  11. Browser will show  a health status up.
    {"status":"UP"}
  12. Congrats , we have just developed a Configuration server using Spring Cloud Config to read properties from our Git  property repository.

Now since we have developed a configuration server using Spring Cloud Config, let’s develop a client micro service to consume the configuration server. When a config client starts up it binds to the Config Server (via the bootstrap configuration property spring.cloud.config.uri) and initializes Spring Environment  with remote property sources.The net result of this is that all client apps that want to use the Config Server need a bootstrap.yml (or an environment variable) with the server address in spring.cloud.config.uri.

Client Service to consume Configuration service:

So let’s start building a micro service to consume configuration service step by step:

  1. Create a new Spring boot project using IDE or https://start.spring.io.
  2. Add the below dependencies in the project:
    <dependencies>
    
       <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</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-web</artifactId>
       </dependency>
    
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
       </dependency>
       
    </dependencies>
  3. Delete the “application.properties” . ( Can ignore the step, if prefer  *.properties files instead of yml)
  4. Create an “application.yml” file with the below configuration:
    ---
    spring:
      application:
        name: amisoft_service_welcome  # This should be the same name of property file in Git.
    server:
      port: 9001

    Important  : The application name must be same as the property file name in the Git repository. Property file name can be appended with <application name>-<profile> e.g amisoft_service_welcome-qa.properties for QA profile.

  5. Create a “bootstrap.yml” with the below configuration:
    ---
    spring:
      profiles:
        active: qa
      cloud:
        config:
          uri: http://localhost:9000  #Url of Configuration server
          username: amisoft
          password: amisoftpass

    Since the “spring.profiles.active=qa”, hence application will read the property from the “amisoft_service_welcome-qa.properties”. If the profile set as “spring.profile.active=default”, application will read the property from the “amisoft_service_welcome.properties”. The “spring.profile.active” value defined at “bootstrap.yml” can be overwrite using  “-Dspring.profiles.active=<profile>” e.g java -jar -Dspring.profiles.active=qa  amisoft_service_welcome.jar.

  6. Create a package called “controller”.
  7. Create a java class “amisoftWelcomeController.java” with the below code:
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by amitdatta on 10/06/17.
     */
    
    @RestController
    public class amisoftWelcomeController {
    
        @Value(value = "${amisoft.service.welcome}")
        String message;
    
        @RequestMapping(value = "/welcome", method= RequestMethod.GET)
        public ResponseEntity<String> welcomeMessage(){
    
            ResponseEntity<String> responseStatus = null;
            return responseStatus = ResponseEntity.status(HttpStatus.OK).body
                    ("Welcome message :" + message);
        }
    }

    Note : We are accessing the property using @Value(value = “${amisoft.service.welcome}”)

  8. Run the main Spring Boot Application class  e.g. “ClientServiceApplication.java”.
  9. Application should be available at port 9001. ( or the port mentioned in “server.port” in “application.yml”).
  10. Access the URL “http://localhost:9001/health“.
  11. Browser will show  a health status up.
    {"status":"UP"}
  12. Access the URL “http://localhost:9001/welcome
  13. Browser will show :  Welcome message :You are using amisoft welcome service with qa profile.
  14. Congrats, our micro service is up and running. We have developed a spring boot micro service  access properties using configuration server.

 

Git Location:

  1. Property files.
  2. Configuration server.
  3. Amisoft Welcome Service.

Next : How to build an Oauth server to secure the micro services.

Referencehttps://cloud.spring.io/spring-cloud-config/spring-cloud-config.html

2 thoughts on “How to build a configuration server using Spring cloud config”

  1. Thank you for this post. It looks like this is the only page on the web which documents using spring.profile.active=default for explicitly running the default profile.

  2. Ohh Thank you Amit for this post. This was exactly I was looking for.
    Its very crisp and clear.

    Keep up the good work going 🙂

Leave a Reply

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

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