- Spring Cloud คืออะไร
- สถาปัตยกรรมโดยรวม
- องค์ประกอบสำคัญ
- Spring Boot กับ Spring Cloud ต่างกันอย่างไร
- เหมาะกับแต่ละ application เหมาะกับระบบที่ประกอบด้วยหลายapplication
- Workshop Architecture
- ขั้นตอนทดลอง
- การเลือกเวอร์ชัน
- Spring Cloud กับ Kubernetes
- แนวทางสำหรับการเรียน
- สรุป
- แหล่งศึกษาเพิ่มเติม
#Spring Cloud คืออะไร
Spring Cloud คือชุดโครงการใน Spring ecosystem ที่ช่วยแก้ปัญหาพื้นฐานของระบบแบบ Distributed Systems และ Microservices เช่น centralized configuration, service discovery, routing, service-to-service communication, load balancing, circuit breaker และ distributed messaging โดยทำงานร่วมกับ Spring Boot
แนวคิดสำคัญ: Spring Boot ช่วยสร้างแต่ละ service ส่วน Spring Cloud ช่วยให้หลาย service ทำงานร่วมกันได้อย่างเป็นระบบ
#สถาปัตยกรรมโดยรวม
flowchart LR
U[Client / Frontend] --> G[Spring Cloud Gateway]
G --> A[Order Service]
G --> B[Product Service]
G --> C[Payment Service]
A --> B
A --> C
D[Service Discovery] --- G
D --- A
D --- B
D --- C
CFG[Spring Cloud Config] -. Configuration .-> G
CFG -. Configuration .-> A
CFG -. Configuration .-> B
CFG -. Configuration .-> C
#องค์ประกอบสำคัญ
#1. Spring Cloud Config
ใช้จัดการ configuration ของหลาย application จากศูนย์กลาง
แทนการกระจายค่าคอนฟิกไว้ในแต่ละ service เหมาะกับการแยก environment เช่น dev,
test, staging และ production
ตัวอย่างแนวคิด:
Git Repository
|
Config Server
|
+-----+------+---------+
| | |
Order Product Payment
Service Service Service
#2. Service Discovery
ในระบบ Microservices ตำแหน่งของ service อาจเปลี่ยนตามการ scale หรือ deployment จึงสามารถใช้ service registry เช่น Eureka, Consul หรือกลไก service discovery ของ Kubernetes เพื่อค้นหา service จากชื่อแทนการ hard-code IP address
#3. Spring Cloud Gateway
ทำหน้าที่เป็น API Gateway ด้านหน้าระบบ เช่น
- Routing
- Path/URL rewriting
- Authentication integration
- Header manipulation
- Rate limiting
- Circuit breaker integration
ตัวอย่าง route:
spring:
cloud:
gateway:
routes:
- id: product-service
uri: lb://PRODUCT-SERVICE
predicates:
- Path=/products/**
lb:// แสดงแนวคิดว่าปลายทางจะถูกเลือกผ่าน load balancing/service discovery
#4. Spring Cloud LoadBalancer
ช่วยกระจาย request ไปยังหลาย instance ของ service
Order Service
|
Load Balancer
/ | \
v v v
P1 P2 P3
เมื่อ Product Service มีหลาย instance ระบบสามารถเลือก instance
ที่เหมาะสมแทนการผูก client กับ server เพียงเครื่องเดียว
#5. Spring Cloud OpenFeign
ช่วยสร้าง Declarative REST Client โดยประกาศ Java interface แทนการเขียน HTTP client ซ้ำ ๆ
@FeignClient(name = "product-service")
public interface ProductClient {
@GetMapping("/products/{id}")
Product getProduct(@PathVariable Long id);
}
จากนั้น service สามารถ inject ProductClient แล้วเรียก method ได้เหมือน Java
object
#6. Circuit Breaker
Distributed system ต้องออกแบบให้รองรับ service ที่ช้า ล่ม หรือเรียกไม่ได้ Spring Cloud CircuitBreaker เป็น abstraction สำหรับใช้ circuit breaker implementation เช่น Resilience4j
สถานะหลัก:
CLOSED --> OPEN --> HALF_OPEN --> CLOSED
แนวคิดคือหยุดส่ง request ซ้ำไปยัง dependency ที่กำลังมีปัญหา และเปิดโอกาสให้ระบบฟื้นตัว
#7. Spring Cloud Stream
เหมาะกับ Event-Driven Architecture และ asynchronous communication โดยเชื่อม application กับ message broker
Order Service
|
OrderCreated
v
Kafka / RabbitMQ
| |
v v
Payment Notification
Service Service
#Spring Boot กับ Spring Cloud ต่างกันอย่างไร
Spring Boot Spring Cloud
สร้าง standalone application/service เชื่อมหลาย service เป็น distributed system
REST API, DI, Data, Security Gateway, Config, Discovery, Load Balancing
Embedded server Distributed-system patterns
#เหมาะกับแต่ละ application เหมาะกับระบบที่ประกอบด้วยหลาย application
ทั้งสองไม่ได้แข่งขันกัน แต่ใช้งานร่วมกัน
#Workshop Architecture
ตัวอย่างระบบร้านค้า:
+----------------------+
| Config Server |
+----------+-----------+
|
+--------+ +--------v---------+
| Client |----------->| API Gateway |
+--------+ +---+----------+---+
| |
+--------v--+ +--v-----------+
| Product | | Order |
| Service |<---| Service |
+-----------+ +------+-------+
|
+------v-------+
| Payment |
| Service |
+--------------+
แนะนำแบ่งเป็น:
spring-cloud-workshop/
├── config-server/
├── discovery-server/
├── api-gateway/
├── product-service/
├── order-service/
└── payment-service/
#ขั้นตอนทดลอง
#ขั้นที่ 1: สร้าง Microservices
สร้าง product-service, order-service และ payment-service ด้วย Spring
Boot และแยก port ของแต่ละ application
server:
port: 8081
#ขั้นที่ 2: เพิ่ม API Gateway
กำหนด Gateway เป็น entry point ของระบบ เช่น port 8080 และ route
/products/**, /orders/**, /payments/** ไปยัง service ที่เกี่ยวข้อง
#ขั้นที่ 3: เพิ่ม Service Discovery
register แต่ละ service กับ service registry แล้วเปลี่ยน routing/service calls จาก fixed URL เป็น logical service name
#ขั้นที่ 4: Service-to-Service Communication
ให้ order-service เรียก product-service ด้วย OpenFeign เพื่ออ่านข้อมูลสินค้า
#ขั้นที่ 5: เพิ่ม Circuit Breaker
จำลอง product-service หยุดทำงาน แล้วสังเกต timeout/error จากนั้นเพิ่ม circuit
breaker และ fallback เพื่อเปรียบเทียบพฤติกรรม
#ขั้นที่ 6: Centralized Configuration
ย้าย configuration ที่ต้องการควบคุมจากส่วนกลางไปไว้ใน configuration repository และให้ service อ่านผ่าน Config Server
#การเลือกเวอร์ชัน
Spring Cloud ใช้แนวคิด Release Train และต้องเลือกให้เข้ากับ Spring Boot ที่ใช้งาน
ไม่ควรกำหนดเวอร์ชัน dependency ย่อยแบบสุ่ม ควรใช้ spring-cloud-dependencies
BOM และตรวจ compatibility matrix จากเอกสาร Spring Cloud ก่อนเริ่มโครงการ
ณ ตุลาคม 2026 เอกสาร Spring ระบุว่า Spring Cloud 2025.1.x (Oakwood) จับคู่กับ
Spring Boot 4.0.x และตั้งแต่ 2025.1.2 รองรับ 4.1.x ด้วย ส่วน 2025.0.x
จับคู่กับ Spring Boot 3.5.x แต่ release train ดังกล่าวสิ้นสุด open-source
support แล้ว
#Spring Cloud กับ Kubernetes
เมื่อ deploy บน Kubernetes ความรับผิดชอบบางส่วนสามารถย้ายไปยัง platform ได้ เช่น:
Requirement Spring Cloud approach Kubernetes approach
Discovery Eureka/Consul Service + DNS Load balancing Spring Cloud LoadBalancer Kubernetes Service Configuration Config Server ConfigMap/Secret Gateway Spring Cloud Gateway Gateway/Ingress/API Gateway Scaling Application/platform integration Deployment + HPA
ดังนั้น production architecture ไม่จำเป็นต้องใช้ทุก Spring Cloud module ควรเลือกตาม infrastructure และ operational requirements
#แนวทางสำหรับการเรียน
ลำดับที่เหมาะสมคือ:
- Spring Boot REST API
- Microservices fundamentals
- Spring Cloud Gateway
- Service Discovery
- OpenFeign + LoadBalancer
- Circuit Breaker/Resilience4j
- Config Server
- Event-driven communication
- Observability
- Docker และ Kubernetes
#สรุป
Spring Cloud เป็น ecosystem สำหรับเพิ่ม distributed-system capabilities ให้ Spring Boot applications จุดเด่นคือสามารถประกอบความสามารถที่ต้องการ เช่น Gateway, Config, Discovery, Load Balancing, declarative HTTP clients, Circuit Breaker และ Messaging โดยไม่จำเป็นต้องนำทุก module มาใช้พร้อมกัน
สำหรับ workshop ควรเริ่มจากระบบ 3 microservices แล้วเพิ่ม infrastructure ทีละชั้น เพื่อให้เห็นปัญหาก่อนใช้เครื่องมือแก้ปัญหา และเมื่อย้ายไป Kubernetes ควรประเมินว่าความสามารถใดควรอยู่ใน application และความสามารถใดควรมอบให้ platform จัดการ
#แหล่งศึกษาเพิ่มเติม
- Spring Cloud: https://spring.io/projects/spring-cloud/
- Spring Cloud Reference: https://docs.spring.io/spring-cloud-release/reference/
- Spring Initializr: https://start.spring.io/