Step-by-Step Guide to Setting Up a Spring Cloud Config Server and Client
Spring Cloud Config is an essential tool for centralized management of configuration properties in microservices. With this framework, developers can store all configuration files in a version-controlled environment (usually Git), simplifying management, security, and scalability across distributed systems.
What Is Spring Cloud Config?
Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. Centralizing configuration in one location enhances control and visibility, making it easier to update, audit, and roll back changes across multiple microservices.
Part 1: Setting Up the Spring Cloud Config Server
Step 1: Create the Spring Boot Project
- Go to Spring Initializer.
- Select Maven or Gradle, Java, and latest Spring Boot version.
- Add the dependency: Spring Cloud Config Server.
- Download and extract the project.
Step 2: Add Dependencies
For Maven:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
For Gradle:
implementation 'org.springframework.cloud:spring-cloud-config-server'
Step 3: Main Class Configuration
Add the @EnableConfigServer
annotation to your main class.
@SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
Step 4: Configure application.properties
Set the server port and the Git repository URI (or local filesystem).
server.port=8888 spring.cloud.config.server.git.uri=file:///E:/Dev/config/
Or for remote Git:
spring.cloud.config.server.git.uri=https://github.com/your-org/your-config-repo.git
Your Config Server is now set up to serve configuration files from the given location.
Part 2: Setting Up the Spring Cloud Config Client
Step 1: Create the Spring Boot Client Project
- Go to Spring Initializer.
- Add dependencies:
- Spring Web
- Spring Cloud Config Client.
Step 2: Add Dependencies
For Maven:
<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>
Step 3: Configure application.properties (or bootstrap.properties)
spring.application.name=clientapp spring.profiles.active=dev spring.cloud.config.label=latest spring.cloud.config.uri=http://localhost:8888 management.endpoints.web.exposure.include=health,info,refresh
spring.application.name
must match the config repo filename for the client.spring.cloud.config.uri
points your microservice to the Config Server.
Step 4: Create a REST Controller to Use a Config Property
@RestController public class MessageController { @Value("${message}") private String message; @GetMapping("/message") public String getMessage() { return this.message; } }
This endpoint will return the value of message
from the config repository.
Step 5: Run and Test
- Start the Config Server (
localhost:8888
). - Start your microservice client (default port
8080
). - Access
http://localhost:8080/message
to see the configured message. - Update the relevant property in the Git repo, commit, and push your changes.
- Restart your client app or trigger a refresh endpoint (
/actuator/refresh
) if enabled, to see updates instantly.
Conclusion
Setting up Spring Cloud Config Server and Client allows you to centralize configuration management, reduce redundancy, and enhance control within microservices and distributed architectures. With the above step-by-step guide, you’re equipped to integrate Spring Cloud Config into your next Java-based project, improving both maintainability and scalability.
Keywords: Spring Cloud Config, centralized configuration management, microservices, Spring Boot, Git configuration, distributed system configuration.
Feel free to ask about advanced features, production-grade setup, or troubleshooting!