Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Spring Cloud Config Tutorial

Overview

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. It allows you to manage the configuration of your microservices in a centralized and flexible manner.

Key Features of Spring Cloud Config

Spring Cloud Config offers several features that make it a popular choice for managing configuration in a microservices architecture:

  • Centralized Configuration: Manage configurations for multiple applications in a single place.
  • Environment-Specific Configuration: Support different configurations for different environments (e.g., development, testing, production).
  • Dynamic Configuration Updates: Refresh configurations without restarting the applications.
  • Support for Various Backends: Store configuration data in Git, SVN, JDBC, and other backends.

Setting Up Spring Cloud Config Server

To set up a Spring Cloud Config Server, follow these steps:

// ConfigServerApplication.java
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

// application.properties
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/your-repo/config-repo

The application.properties file configures the server to use a Git repository for storing configuration data.

Setting Up Spring Cloud Config Client

To set up a Spring Cloud Config Client, follow these steps:

// ConfigClientApplication.java
@SpringBootApplication
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

// bootstrap.properties
spring.application.name=config-client
spring.cloud.config.uri=http://localhost:8888

The bootstrap.properties file configures the client to connect to the Config Server.

Example Configuration Files

Create a Git repository with the following structure to store your configuration files:

config-repo/
    config-client.properties
    config-client-dev.properties
    config-client-prod.properties

Here is an example of a config-client.properties file:

# config-client.properties
message=Hello from Spring Cloud Config

Accessing Configuration in Spring Boot Application

You can access the configuration properties in your Spring Boot application using the @Value annotation:

// ExampleController.java
@RestController
public class ExampleController {
    @Value("${message:Default Hello}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return message;
    }
}

Refreshing Configuration Properties

Spring Cloud Config provides support for dynamically refreshing configuration properties without restarting the application. Add the Spring Boot Actuator dependency and enable the refresh scope:

// build.gradle
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
}

// ExampleController.java
@RestController
@RefreshScope
public class ExampleController {
    @Value("${message:Default Hello}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return message;
    }
}

// application.properties
management.endpoints.web.exposure.include=refresh

You can trigger a refresh of the configuration properties by sending a POST request to the /actuator/refresh endpoint:

curl -X POST http://localhost:8080/actuator/refresh

Environment-Specific Configuration

Spring Cloud Config supports environment-specific configuration files. For example, you can create different configuration files for development and production environments:

# config-client-dev.properties
message=Hello from Spring Cloud Config (Development)

# config-client-prod.properties
message=Hello from Spring Cloud Config (Production)

The appropriate configuration file will be loaded based on the active profile of the client application.

Key Points

  • Spring Cloud Config provides centralized configuration management for distributed systems.
  • It supports environment-specific configurations and dynamic updates without restarting applications.
  • Configurations can be stored in various backends such as Git, SVN, and JDBC.
  • Spring Cloud Config integrates with Spring Boot Actuator for refreshing configuration properties.
  • Environment-specific configuration files allow different settings for different environments.

Conclusion

Spring Cloud Config is a powerful tool for managing configuration in a microservices architecture. By centralizing and externalizing configuration, it simplifies the management of configurations for multiple applications and environments. Happy coding!