← Kursa Dön
📄 Text · 30 min

Config Server

Mikroservis mimarisinde düzinelerce servis, her biri farklı ortamda (dev, staging, prod) farklı konfigürasyonlarla çalışır. Veritabanı URL'leri, API key'leri, feature flag'leri — bunların hepsini her servisin kendi application.yml dosyasında yönetmek kabus olur. Spring Cloud Config Server, tüm servislerin konfigürasyonunu merkezi bir yerden yönetmenizi sağlar.

Config Server Kurulumu

Yeni bir Spring Boot projesi:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

Git Backend

En yaygın backend, Git repository'dir. Konfigürasyon dosyaları bir Git repo'sunda saklanır:

# Config Server application.yml
server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/myorg/config-repo
          default-label: main
          search-paths: '{application}'
          clone-on-start: true
          # Private repo için
          username: ${GIT_USER}
          password: ${GIT_TOKEN}

Git repo yapısı:

config-repo/
├── application.yml           # tüm servisler için ortak
├── user-service.yml          # user-service özel
├── user-service-prod.yml     # user-service production
├── order-service.yml         # order-service özel
└── order-service-dev.yml     # order-service development

Native (Dosya Sistemi) Backend

Geliştirme ortamında Git yerine lokal dosya sistemi kullanılabilir:

spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/configs, file:///opt/configs

Config Client (Servis Tarafı)

Her mikroservis Config Server'dan konfigürasyonunu çeker:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
# application.yml
spring:
  application:
    name: user-service
  config:
    import: optional:configserver:http://localhost:8888
  cloud:
    config:
      fail-fast: true   # Config Server yoksa başlama
      retry:
        initial-interval: 1000
        max-attempts: 6

@RefreshScope — Çalışma Zamanında Güncelleme

Normalde konfigürasyon değişiklikleri servisin yeniden başlatılmasını gerektirir. @RefreshScope ile bean'ler /actuator/refresh endpoint'i çağrıldığında yeniden oluşturulur:

@RestController
@RefreshScope
public class MessageController {

    @Value("${app.welcome-message:Hello}")
    private String welcomeMessage;

    @GetMapping("/message")
    public String getMessage() {
        return welcomeMessage;
    }
}
# Git repo'da değişiklik yaptıktan sonra
curl -X POST http://user-service:8081/actuator/refresh
# ["app.welcome-message"]  → değişen property'ler

Spring Cloud Bus

Her servisi tek tek refresh etmek yerine, Spring Cloud Bus tüm servislere değişikliği yayınlar. RabbitMQ veya Kafka üzerinden çalışır:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
# Tek bir çağrı ile TÜM servisler güncellenir
curl -X POST http://config-server:8888/actuator/busrefresh

Konfigürasyon Şifreleme

Hassas bilgileri (veritabanı şifresi, API key) şifreleyerek saklayabilirsiniz:

# Şifrelenmiş değer (Git repo'daki yml dosyasında)
spring:
  datasource:
    password: '{cipher}AQBh7kL9...'
# Config Server — simetrik şifreleme anahtarı
encrypt:
  key: ${ENCRYPT_KEY}
# Değer şifreleme
curl -X POST http://localhost:8888/encrypt -d "my-secret-password"
# AQBh7kL9...

# Değer çözme
curl -X POST http://localhost:8888/decrypt -d "AQBh7kL9..."
# my-secret-password

Profile-Specific Config

Config Server URL yapısı:

GET /{application}/{profile}
GET /{application}/{profile}/{label}

GET /user-service/default     → user-service.yml
GET /user-service/prod        → user-service-prod.yml
GET /user-service/dev/feature → feature branch'teki user-service-dev.yml

Client tarafında aktif profile belirleme:

spring:
  profiles:
    active: prod

Config Server, mikroservis konfigürasyonunu merkezi, versiyonlanmış ve güvenli bir şekilde yönetmenin standart yoludur. Git backend ile tüm değişiklikler izlenebilir ve geri alınabilir.