jav spring boot mastery

In a microservices architecture, managing configurations across multiple services becomes difficult. Spring Cloud Config provides centralized configuration management, ensuring consistency and dynamic updates without redeploying services.

🎯 Why Centralized Configuration?

✅ Central place to manage configuration for all services
✅ Environment-specific settings (dev, qa, prod)
✅ Dynamic refresh of properties
✅ Secure secrets handling

🧰 Components

ComponentRole
Config ServerCentral service that provides configurations
Config ClientYour microservices that fetch configs from server
Git/Native backendSource of config (Git repo, filesystem, etc.)

⚙️ Step-by-Step Setup

🛠️ Step 1: Create the Spring Cloud Config Server

🔸 pom.xml

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>

🔸 @EnableConfigServer

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

🔸 application.yml

server:
port: 8888

spring:
cloud:
config:
server:
git:
uri: https://github.com/your-org/config-repo
clone-on-start: true

📂 In the Git repo, add files like:

  • application.yml
  • service-a.yml
  • service-b-dev.yml
  • service-c-prod.yml

🛠️ Step 2: Set Up a Config Client (e.g., Service A)

🔸 pom.xml

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

🔸 bootstrap.yml (or application.yml in latest Spring Boot)

spring:
application:
name: service-a
cloud:
config:
uri: http://localhost:8888

💡 spring.application.name must match the file in your Git config repo (service-a.yml)

🧪 Test the Setup

Start:

  1. Config Server (port 8888)
  2. Service A (port 8080)

Visit:

http://localhost:8888/service-a/default

→ You’ll see the properties from your Git repo.

🔄 Dynamic Property Refresh

Use @RefreshScope on a Spring-managed bean:

@RestController
@RefreshScope
public class GreetingController {

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

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

🔁 Trigger Refresh (Actuator Required)

Add Actuator Dependency:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Enable refresh endpoint in application.yml:

management:
  endpoints:
    web:
      exposure:
        include: refresh

Trigger refresh:

cu rl -X POST http://localhost:8080 /actuator/refresh

🧱 Git Repo Structure Example

config-repo/
├── application.yml
├── service-a.yml
├── service-b-dev.yml
└── service-c-prod.yml

Spring uses {application-name}-{profile}.yml naming convention.

🔐 Securing Config Server

Add credentials to access Git:

spring:
  cloud:
    config:
      server:
        git:
          username: your-username
          password: your-token

Or use environment variables for secrets.

🔧 Native File-Based Config (Alternative to Git)

spring:
cloud:
config:
server:
native:
search-locations: file:///path/to/configs

Use --spring.profiles.active=native when running the config server.

🌍 Real-World Use Case

  • Use Git repo for versioned config.
  • Services like service-a, service-b, and gateway fetch their configs.
  • You can change any property and just call the /actuator/refresh endpoint — no need to restart the service.

✅ Summary

FeatureBenefit
Centralized ConfigManage all configs in one place
Environment SupportEasy separation of dev, prod, etc.
Dynamic RefreshUpdate properties without restart
Git IntegrationVersion control for configs

➡️ Next Up: Part 20 – Spring Cloud Gateway for API Routing & Filters

Similar Posts