Spring Cloud Config: รวมศูนย์การจัดการ Configuration ด้วย GitHub, Bitbucket และ AWS S3

Sharing is caring!

Spring Cloud Config คืออะไร?

Spring Cloud Config เป็นหนึ่งในเครื่องมือสำคัญในระบบ Microservices ที่ช่วยให้เราสามารถจัดการไฟล์ configuration ของแต่ละ service ได้จากศูนย์กลาง (Centralized Configuration Management) โดยไม่จำเป็นต้องฝังค่าคงที่ (hardcode) ไว้ในโปรเจกต์แต่ละตัว

การตั้งค่า Configuration แบบรวมศูนย์ช่วยให้ระบบสามารถเปลี่ยนแปลงค่าได้ทันทีโดยไม่ต้อง build ใหม่ หรือ deploy ใหม่ ทั้งยังรองรับ version control, auditing และการแยก profile ได้อีกด้วย


ภาพรวมการทำงาน

  1. Spring Cloud Config Server คือ service ที่ใช้ให้ client อ่านค่า config
  2. สามารถดึงค่าจาก GitHub, Bitbucket, หรือ AWS S3
  3. Client สามารถเรียกใช้งาน config ตาม application name และ profile ได้แบบ dynamic

โครงสร้างไฟล์ Configuration

config-repo/
├── application.yml
├── myservice-dev.yml
├── myservice-prod.yml

Spring Cloud Config Server จะอ่านไฟล์เหล่านี้และส่งให้กับ client ตาม path: http://localhost:8888/myservice/dev


1. การตั้งค่า Config Server ให้เชื่อม GitHub

ไฟล์ application.yml ของ Config Server

server:
  port: 8888

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

การเปิดใช้งาน Config Server

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

2. การเชื่อม Bitbucket

spring:
  cloud:
    config:
      server:
        git:
          uri: https://bitbucket.org/your-org/config-repo
          username: your-bitbucket-user
          password: your-bitbucket-app-password

Bitbucket แนะนำให้สร้าง App Password เพื่อใช้งานแทนรหัสผ่านหลัก


3. การเชื่อม AWS S3

Spring Cloud Config ไม่รองรับ S3 โดยตรงใน starter หลัก แต่สามารถทำได้โดยใช้ third-party หรือ custom backend

แนวทาง:

  1. สร้างไฟล์ config ไว้ใน S3 เช่น application-dev.yml
  2. เขียน custom ConfigEnvironmentRepository ที่โหลดจาก S3
  3. ใช้ AWS SDK (เช่น S3Client) เพื่อโหลดไฟล์แบบ runtime
@Component
public class S3EnvironmentRepository implements EnvironmentRepository {

    private final S3Client s3Client;

    public S3EnvironmentRepository(S3Client s3Client) {
        this.s3Client = s3Client;
    }

    @Override
    public Environment findOne(String application, String profile, String label) {
        // ดึง config จาก S3 และแปลงเป็น Environment object
    }
}

การใช้งานจาก Spring Boot Client

ให้ client application เพิ่ม dependency:

implementation 'org.springframework.cloud:spring-cloud-starter-config'

ตั้งค่า bootstrap.yml

spring:
  application:
    name: myservice
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8888

เมื่อ application start จะเรียก config จาก /myservice/dev


รองรับ Refresh Runtime ด้วย Actuator

เพิ่ม dependency:

implementation 'org.springframework.boot:spring-boot-starter-actuator'

เปิดใช้งาน refresh endpoint

management:
  endpoints:
    web:
      exposure:
        include: refresh

เรียก Refresh Runtime

curl -X POST http://localhost:8080/actuator/refresh

ข้อดีของ Spring Cloud Config

  • รวมศูนย์การจัดการ config หลาย service
  • รองรับการเปลี่ยนค่า runtime ด้วย refresh
  • เชื่อมต่อกับ GitHub, Bitbucket และ source อื่น ๆ ได้ง่าย
  • ใช้ร่วมกับ Spring Boot ได้อย่างลงตัว
  • รองรับการ versioning และ auditing ผ่าน git
  • ช่วยลดข้อผิดพลาดจากการ duplicate config

ข้อเสียของ Spring Cloud Config

  • มีความซับซ้อนเพิ่มขึ้นสำหรับระบบขนาดเล็ก
  • ต้องดูแล config server เพิ่มเติม (HA, scaling)
  • หาก git repo ใช้งานไม่ได้ จะส่งผลต่อการ start service
  • refresh runtime ไม่สามารถทำงานกับค่าบางชนิดที่ load ครั้งเดียว เช่น @Value

แนวทางการใช้งานจริง

  • ใช้ร่วมกับ GitHub Actions หรือ Jenkins เพื่อตรวจสอบและ deploy config ได้อย่างปลอดภัย
  • แยกไฟล์ config ตาม profile เช่น dev / staging / prod
  • ตั้งชื่อไฟล์ config ตาม service name เพื่อให้แต่ละ service อ่าน config ของตนเอง
  • ควรมี fallback config ภายในตัว service กรณี config server ล่ม

บทสรุป

Spring Cloud Config ช่วยยกระดับการจัดการ configuration จากแบบ hardcoded หรือ local file ไปสู่ระบบ centralized, versioned และ auditable ซึ่งเหมาะกับระบบ microservices ที่ต้องบริหาร config หลาย ๆ ตัวพร้อมกัน

หากคุณใช้ Spring Boot อยู่แล้ว การรวม Spring Cloud Config เข้ามาจะช่วยให้ระบบยืดหยุ่นและพร้อม scale ได้อย่างมืออาชีพ

Leave a Reply

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *