สร้าง Reactive RESTful Web Service ด้วย Vert.x
Vert.x เป็น polyglot event-driven framework ที่ทำงานบน JVM ที่สามารถเขียนได้หลายภาษาทั้งภาษาของ JVM อย่างเช่น Java, Groovy, Groovy, Scala หรือที่ไม่ใช้ เช่น JavaScript, Ruby, Python, Clojure และ Ceylon
What is Vert.X?
Vert.x พัฒนาโดย Tim Fox ในปี 2011 ตอนที่ทำงานอยู่ที่ VMware ตอนแรกชื่อว่า Node.x เป็นการเอาชื่อ Node.js มาเปลี่ยนเป็น “x” ที่สื่อความหมายว่าเป็น polyglot ที่ใช้ได้กับหลายภาษา ไม่จำกัดเฉพาะบางภาษาเหมือน Node.js แต่เปลี่ยนชื่อเป็น Vert.x เพื่อหลีกเลี่ยงปัญหาทางกฎหมายกับ “Node” ที่เป็นเครื่องหมายการค้าของ Joynent Inc. แต่ชื่อใหม่ก็มีความหมายในเชิงเดียวกันกับ Node ซึ่ง “vertex” มีความหมายว่า “node” ในทางคณิตศาสตร์ ในปี 2013 โครงการ Vert.x ได้ไปอยู่ภายใต้การดูแลของ Eclipse Foundation
Architecture
Vert.x ใช้ Netty เป็น low level IO library, framework มี feature ดังนี้
- Polyglot, ส่วนของ application สามารถเขียนได้ด้วย Java, JavaScript, Groovy, Ruby, Scala, Kotlin และ Ceylon
- Simple concurrency model, โค้ดทั้งหมดเป็น single thread เหมือน Node.js โดยไม่ต้องยุ่งยากกับการเขียนโปรแกรมแบบ multi-thread
- Simple, ใช้รูปแบบการเขียนโปรแกรมแบบ asynchronous programming ทำให้มีความสามารถในการ scale แบบ non-blocking application
- Distributed event bus, event bus
- Actor model และ public repository, ใช้การ re-use และ shared component
Vert.x Core
Vert.x Core มี functionality พื้นฐานของ Vert.x รวมทั้ง HTTP, TCP, File System และ feature อื่นๆ โดยจะใช้ใน component อื่นๆ ของ Vert.x ซึ่ง component เป็น JAR file ธรรมดา ที่สามารถอยู่ใน application
เพิ่ม Vert.x Core dependency ใน Maven project
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.7.0</version>
</dependency>
หรือ Gradle project
dependencies {
compile 'io.vertx:vertx-core:3.7.0'
}
Vert.x Project
วิธีการง่ายๆ สำหรับสร้าง Vert.x project คือใช้การ git clone
starter project จาก Vert.x repository
vertx-maven-starter สำหรับสร้าง Maven project และ vertx-grale-starter สำหรับสร้าง Gradle project ซึ่งเป็น template อย่างง่ายสำหรับเริ่ม project
ในบทความนี้ใช้ Maven ดังนั้นใช้ Vert.x Maven Starter สร้าง project ใช้คำสั่ง
git clone https://github.com/vert-x3/vertx-maven-starter.git vertx-restful-api
จะได้ template project ใน folder vertx-restful-api
(ซึ่งเป็นชื่อของ project)
เปิดไฟล์ pom.xml
ด้วย IntelliJ IDEA (หรือ IDE อื่นๆ) ครั้งแรกจะใช้เวลาในการโหลด dependency นานหน่อย เหมือนกับ maven project ทั่วไป
เมื่อเปิดไฟล์ MainVerticle
จะเจอ template code ของ Vert.x จะเห็นได้ว่า class MainVerticle
extends มาจาก class AbstractVerticle
ซึ่งเป็น Application class ของ Vert.x มี method start()
สำหรับเริ่มต้นการทำงานของ Vert.x app
สามารถรัน Vert.x application ด้วยคำสั่ง mvn test exec:java
และเข้าถึงได้ด้วย http://localhost:8080
จะแสดงข้อความดังนี้ แสดงว่า Vert.x app ทำงานได้แล้ว
Hello Vert.x!
RESTful Web
Templete code ไม่เพียงพอสำหรับสร้าง Web API ดังนั้นจะต้องเพิ่ม vertx-web
dependency ดังนี้
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
เพิ่ม Router เพื่อ handle request ซึ่งในบทความนี้จะสร้าง CRUD API ดังนั้นจะมี endpoint สำหรับจัดการกับ GET/POST/PUT/DELETE HTTP method
ใน Vert.x จะใช้ class Router
เพื่อ route HTTP request ไปยัง handle method เพื่อจัดการกับ request นั้นๆ หลังจากที่ตั้งค่าให้กับแต่ละ endpoint แล้ว ก็ implement แต่ละ handle method
Test API
ใช้ Postman เพื่อทดสอบ Vert.x app ว่าจัดการกับ HTTP request ตามที่ route แต่ละ endpoint กับ handle method หรือไม่
จะเห็นได้วา CRUD API ทำงานได้อย่างที่ต้องการในการ สร้าง/อ่าน/แก้ไข/ลบ custermers
resource และเนื่องด้วย Vert.x ทำงานแบบ Reactive โดยพื้นฐานทำให้ Web Service API ทำงานแบบ Reactive ไปได้โดยปริยาย
ตัวอย่างโค้ด
สรุป
ในบทความนี้ได้ทดลองใช้ Vert.x สร้าง RESTful Web Service แบบ CRUD ง่ายๆ ซึ่งโดยพื้นฐานของ Vert.x เป็น application แบบ Reactive และ non-blocking ทำให้ API ที่เราสร้างขึ้นเป็น Reactive ไปโดยปริยาย แต่ในบทความนี้เป็นการสร้าง Web Service อย่างง่ายๆ ที่ไม่ได้ต่อกับฐานข้อมูลใดๆ แต่ใช้ in-memory data store เพื่อเก็บข้อมูลเท่านั้น แต่ Vert.x framework เป็น framework ที่เขียนได้หลายภาษาอย่างที่กล่าวข้างต้น และมีความสามารถด้าน scale ได้ ซึ่งบทความนี้ไม่ได้ครอบคลุม แต่สามารถอ่าน doc หรือตัวอย่างของ Vert.x ได้ซึ่งมีตัวอย่างให้หลากหลายที่จะเอามาปรับใช้ได้ตามต้องการ และ Vert.x ก็มีความคล้ายกับ Node.js พอสมควร