1. Spring Boot Overview
Spring Boot is a framework that simplifies creating production-ready Spring applications. Key points:
- Provides embedded servers (Tomcat, Jetty), so you don’t need to deploy WAR files.
- Auto-configuration reduces boilerplate code.
- Supports microservices architecture.
2. Spring Cloud Config Overview
Spring Cloud Config provides centralized configuration management for distributed systems. It allows you to:
- Store configuration in Git, SVN, or filesystem.
- Support dynamic refresh of configuration without restarting services.
- Separate configuration from code, making microservices easier to maintain.
Components:
- Config Server – exposes configuration to clients.
- Config Client – Spring Boot application that fetches config from the server.
3. Example Setup
We’ll create:
- Config Server (Spring Boot app)
- Config Client (Spring Boot app)
Step 1: Config Server
pom.xml dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Application class:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
application.yml:
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/yourusername/config-repo
clone-on-start: true
Here,
config-repo
is a Git repository containing config files likeapplication.yml
.
Step 2: Config Client
pom.xml dependencies:
<dependencies>
<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>
</dependencies>
application.yml:
spring:
application:
name: client-app
cloud:
config:
uri: http://localhost:8888
Controller example:
@RestController
public class DemoController {
@Value("${message:Default message}")
private String message;
@GetMapping("/message")
public String getMessage() {
return message;
}
}
Step 3: Git Config Repository Example
In your Git repo, create a file client-app.yml
:
message: "Hello from Config Server!"
When the client app runs and you access http://localhost:8080/message
, it should return:
Hello from Config Server!
✅ How it Works
- Config Client starts → fetches config from Config Server.
- Config Server reads configuration from Git repository.
- Client uses configuration values at runtime.