Spring Cloud Config is a server and client solution for externalized configuration management in a distributed system. It provides a centralized way to manage configuration properties across multiple microservices, making it easier to maintain and update configurations without redeploying applications.
In this tutorial, we will explore how to set up and configure a Spring Cloud Config Server using Spring Boot. We'll cover the basics of setting up the server, integrating it with a client application, and best practices for managing configuration properties.
Before you begin, ensure that you have the following:
You can create a new Spring Boot project using Spring Initializr. Visit Spring Initializr and configure your project with the following settings:
com.exampleconfig-serverconfig-serverConfig Servercom.example.configserverAdd the following dependencies:
Click "Generate" to download the project. Extract the downloaded ZIP file and import it into your favorite IDE.
Open src/main/resources/application.properties and add the following configuration:
server.port=8888
spring.application.name=config-server
spring.cloud.config.server.git.uri=https://github.com/your-repo/config-repo
Replace https://github.com/your-repo/config-repo with the URL of your Git repository that contains the configuration files.
Create a new Git repository or use an existing one. Inside this repository, create configuration files for each microservice. For example:
config-repo/
āāā application.properties
āāā service-a.yml
application.properties can contain default configurations:
server.port=8080
management.endpoints.web.exposure.include=*
service-a.yml can contain specific configurations for service-a:
spring:
application:
name: service-a
custom:
property: value
Open src/main/java/com/example/configserver/ConfigServerApplication.java and add the @EnableConfigServer annotation to enable the Config Server:
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);
}
}
Run the Spring Boot application. You can do this by executing the following command in your terminal:
mvn spring-boot:run
The Config Server should start on port 8888. You can verify its status by accessing http://localhost:8888.
Create another Spring Boot project using Spring Initializr with the following settings:
com.exampleservice-aservice-aService Acom.example.serviceaAdd the following dependencies:
Open src/main/resources/bootstrap.properties and add the following configuration to point to the Config Server:
spring.application.name=service-a
spring.cloud.config.uri=http://localhost:8888
Create a new Java class in your client application to demonstrate accessing the configuration properties:
package com.example.servicea;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ServiceAApplication {
@Value("${custom.property}")
private String customProperty;
public static void main(String[] args) {
SpringApplication.run(ServiceAApplication.class, args);
}
@GetMapping("/config")
public String getConfig() {
return "Custom Property: " + customProperty;
}
}
Run the Spring Boot application for the client. You can do this by executing the following command in your terminal:
mvn spring-boot:run
The client should start on its default port (usually 8080). Access http://localhost:8080/config to see the value of the custom property retrieved from the Config Server.
application-dev.properties, application-prod.yml).Spring Cloud Config Server provides a powerful way to manage configurations across multiple microservices. By centralizing configuration management, you can simplify deployment, improve maintainability, and enhance security in your microservices architecture. In this tutorial, we covered how to set up a Config Server and integrate it with a client application. Following best practices will help ensure that your configuration management is robust and scalable.
Feel free to explore additional features of Spring Cloud Config, such as support for other version control systems (e.g., SVN) and integration with distributed tracing tools like Zipkin or Jaeger.