Spring Cloud Config with Managing Application Configuration

Anh Trần Tuấn - Aug 22 - - Dev Community

1. What is Spring Cloud Config?

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With Spring Cloud Config, you can store your configuration files in a remote repository (e.g., Git, SVN) and access them from multiple applications and environments.

Key Features:

+ Centralized configuration management

+ Dynamic property changes without redeploying applications

+ Support for various backend repositories (Git, SVN, native file system)

+ Encryption and decryption of sensitive properties

2. Setting Up Spring Cloud Config Server

The Spring Cloud Config Server acts as a configuration management service that provides configuration properties to client applications. Let's start by creating a Config Server.

Step 1 : Create a New Spring Boot Application

Create a new Spring Boot application with the following dependencies:

+ Spring Cloud Config Server

+ Spring Boot DevTools

+ Spring Web

Maven Configuration (pom.xml):

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR12</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Enter fullscreen mode Exit fullscreen mode

Step 2 : Enable the Config Server

In the main application class, annotate with @EnableConfigServer to enable the configuration server.

Main Application Class (ConfigServerApplication.java):

package com.example.configserver;

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3 : Configure the Config Server

In the application.yml file, configure the Git repository that the Config Server will use to fetch configuration properties.

application.yml

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-username/config-repo
          clone-on-start: true
Enter fullscreen mode Exit fullscreen mode

Here, replace https://github.com/your-username/config-repo with your own Git repository URL containing configuration files.

3. Creating Configuration Files in the Git Repository

In your Git repository, create a new configuration file named application.yml or environment-specific files like application-dev.yml, application-prod.yml.

Example Configuration (application.yml):

server:
  port: 8080

message:
  greeting: "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

4. Setting Up Spring Cloud Config Client

Now, let's create a Spring Boot client application that retrieves its configuration from the Config Server.

Step 1 : Create a New Spring Boot Application

Create another Spring Boot application with the following dependencies:

+ Spring Cloud Config Client

+ Spring Boot DevTools

+ Spring Web

Maven Configuration (pom.xml):

<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>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Step 2 : Configure the Config Client

In the bootstrap.yml file, configure the Config Client to point to the Config Server.

bootstrap.yml:

spring:
  application:
    name: demo-service
  cloud:
    config:
      uri: http://localhost:8888
Enter fullscreen mode Exit fullscreen mode

Step 3 : Create a Rest Controller to Access Config Properties

Create a simple REST controller to demonstrate accessing properties from the Config Server.

Controller Class (MessageController.java):

package com.example.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

    @Value("${message.greeting}")
    private String greetingMessage;

    @GetMapping("/greeting")
    public String getGreetingMessage() {
        return greetingMessage;
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Running the Demo

Start the Config Server : Run the ConfigServerApplication class.

Start the Config Client : Run the client application.

Once both applications are running, you can access the configuration properties via the client application.

Accessing Configuration Properties:

Open your browser and navigate to http://localhost:8080/greeting.

Expected Output:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

6. Dynamic Configuration Updates

One of the powerful features of Spring Cloud Config is the ability to update configurations dynamically without restarting your application. Let's see this in action.

Step 1 : Update the Configuration in the Git Repository

Change the message.greeting property in your application.yml file to:

message:
  greeting: "Hello, Spring Cloud Config!"
Enter fullscreen mode Exit fullscreen mode

Commit and push the changes to your Git repository.

Step 2 : Refresh the Client Application

Use Spring Boot Actuator's /actuator/refresh endpoint to refresh the configuration properties without restarting the application.

Enable Actuator:

Add the following dependency and configuration in the client application.

Maven Dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

application.yml:

management:
  endpoints:
    web:
      exposure:
        include: refresh
Enter fullscreen mode Exit fullscreen mode

Trigger the Refresh:

Send a POST request to http://localhost:8080/actuator/refresh.

Re-check the Greeting Message:

Refresh the http://localhost:8080/greeting endpoint. You should see:

Hello, Spring Cloud Config!
Enter fullscreen mode Exit fullscreen mode

7. Conclusion

Spring Cloud Config simplifies configuration management in a distributed environment by centralizing and externalizing application properties. With its ability to handle dynamic configuration changes, support for various repositories, and seamless integration with Spring Boot, it is an indispensable tool for modern microservices architectures.

By following this guide, you should be able to set up a basic Spring Cloud Config Server and client, and experience the power of centralized and dynamic configuration management in your applications.

Read posts more at : Spring Cloud Config with Managing Application Configuration

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .