40 - Build และ Deploy
เป้าหมายของบท
Section titled “เป้าหมายของบท”บทสุดท้ายจะสรุปขั้นตอน build และเตรียม deploy โปรเจกต์ Backend API
หลังจบบทนี้ผู้อ่านควรเข้าใจ:
- build jar อย่างไร
- run jar ด้วย profile production อย่างไร
- checklist ก่อน deploy มีอะไรบ้าง
- health check สำคัญอย่างไร
- roadmap หลังจบเล่มควรไปต่อเรื่องไหน
Build application
Section titled “Build application”ใช้คำสั่ง:
.\gradlew.bat clean buildถ้าใช้ macOS/Linux:
./gradlew clean buildartifact จะอยู่ที่:
build/libs/ตัวอย่าง:
backend-api-0.0.1-SNAPSHOT.jarRun jar
Section titled “Run jar”PowerShell:
$env:SPRING_PROFILES_ACTIVE="prod"$env:DB_URL="jdbc:postgresql://localhost:5432/secure_admin"$env:DB_USERNAME="postgres"$env:DB_PASSWORD="admin123"$env:JWT_SECRET="0123456789012345678901234567890123456789012345678901234567890123"
java -jar build/libs/backend-api-0.0.1-SNAPSHOT.jarถ้า deploy ด้วย platform เช่น Render, Railway, Fly.io, AWS, Azure หรือ GCP ให้ตั้งค่า environment variables ใน dashboard ของ platform นั้น
เพิ่ม health endpoint
Section titled “เพิ่ม health endpoint”เพิ่ม dependency:
implementation 'org.springframework.boot:spring-boot-starter-actuator'ตั้งค่า:
management.endpoints.web.exposure.include=health,infomanagement.endpoint.health.probes.enabled=trueตรวจ:
GET /actuator/healthresponse:
{ "status": "UP"}health endpoint ช่วยให้ load balancer หรือ platform รู้ว่า application พร้อมรับ traffic หรือไม่
Checklist ก่อน deploy
Section titled “Checklist ก่อน deploy”ตรวจสิ่งเหล่านี้:
./gradlew clean buildผ่าน- test ผ่าน
- profile production ใช้ env vars
- ไม่มี password/secret ใน Git
- Flyway migration ผ่าน
ddl-auto=validate- JWT secret ยาวพอและเปลี่ยนจากค่า demo
- log ไม่มี password/token เต็ม
/actuator/healthใช้งานได้- database backup/restore มีแผน
- admin endpoint ต้องใช้ role
ADMIN - Swagger UI ถูกเปิดหรือปิดตาม policy
Checklist feature ก่อนจบเล่ม
Section titled “Checklist feature ก่อนจบเล่ม”ระบบควรทำได้:
- register user
- hash password
- login และออก JWT
- protect endpoint ด้วย JWT
/meอ่าน current userUSERเข้า admin endpoint ไม่ได้ADMINดู user list ได้ADMINเปลี่ยน role/status ได้- audit log ถูกบันทึก
- validation error อ่านง่าย
- exception response สม่ำเสมอ
- pagination ทำงาน
- build และ run ด้วย Docker Compose ได้
Deploy flow โดยรวม
Section titled “Deploy flow โดยรวม”1. Push code to GitHub2. CI run test/build3. Build Docker image หรือ jar4. Deploy ไป platform5. Set environment variables6. Run Flyway migration7. Health check ผ่าน8. เปิด trafficช่วงแรกอาจ deploy manual ได้ แต่เมื่อโปรเจกต์จริงจังควรใช้ CI/CD
Roadmap หลังจบหนังสือ
Section titled “Roadmap หลังจบหนังสือ”หัวข้อที่ควรเรียนต่อ:
- Refresh token
- Logout และ token revocation
- OAuth2/OIDC กับ provider จริง
- Redis caching
- Rate limiting
- CI/CD
- Observability ด้วย metrics/tracing
- Testcontainers
- Blue/green deployment
- Microservices
ปิดเล่ม
Section titled “ปิดเล่ม”ถ้าทำตามหนังสือจนจบ ผู้อ่านจะผ่าน flow หลักของ backend งานจริงแล้ว:
REST API -> Database -> Better API -> Auth/JWT -> Admin -> Production Readyจากจุดนี้สิ่งสำคัญคือสร้างโปรเจกต์ซ้ำด้วยตัวเองอีกครั้งโดยไม่ดูเฉลยทั้งหมด เพื่อให้เข้าใจ structure และ decision ของ Spring Boot มากขึ้น