DEV Community

Pramoth Suwanpech
Pramoth Suwanpech

Posted on

เริ่มต้น Quarkus 3 part 1

blog นี้จะพาเรียนรู้ quarkus step by step เพราะว่าการเริ่มต้น Quarkus มันมีหลาย plugin มากทำให้คนที่เริ่มเรียนจับต้นชนปลายยาก ผมจึงจะจะเอามาเรียบเรียง เริ่มทำตั้งแต่เริ่มต้น โดยเราจะทำ web app ที่ render ด้วย server side โดยใช้ Qute template engine หน้าบ้านใช้ HTMX,AlpineJS

  1. Rest
  2. web (static,server side rendering)
    • Qute
    • web bundler
    • Renarde
  3. hibernate orm

ทีมผมใช้ Spring boot เป็นหลัก แต่ในปีนี้เรามีบางส่วนจะย้ายมา Quarkus เพราะว่าเราต้องการ dev ที่เร็วซึ่ง quarkus มำ livereload ได้ดีมากๆอารมร์เหมือนเขียนพวก dynamic lang เลย
อีกประเด็นที่เราเลือก Quarkus ก็คือ เราต้องการใช้ server side renderer เราจึงต้องการ template engine แต่เราต้องการ template engine ที่ typesafety เมื่อหาอยู่นาน java template engine ที่ typesafe ก็จะมีแค่ jsp และ qute เท่านั้น
ส่วนเรื่อง performance ของ Quarkus ที่ดีกว่า boot นั้นไม่ได้เป็นจุดที่เราเลือก เพราะว่ามันไม่ได้สำคัญในแง่มากใน enterprise app ที่เราทำไป optimize หลังบ้านให้ดีๆ ดีกว่า แต่ก็ถือว่าดีที่ได้ความเร็วเพิ่มมา

Intro

Quarkus จะต่างจาก boot ตรงที่มันจะทำการbuildหรือประกอบร่าง bean ต่างๆตอน build เช่น แทนที่จะ scan หา annotation @Entity ตอน start app มันก็หาตอน build แล้วเตียม code ที่ setup EntityManager ให้เลยตอนรันก็พร้อมใช้เลย ข้อดีทำให้ start เร็วและสามารถขจัดโค๊ดที่ไม่ได้ใช้ และยังเป็นการเตรียมพร้อมกับการทำ native image ได้ด้วย(เพราะ build image ต้องการรู้ว่าจะใช้ควาสไหนตอน build เช่นกัน)

Part1 Rest

หลังจากติดตั้ง quarkus CLI แล้ว ก็เริ่มใช้สร้าง project เลยครับโดยใช้คำสั่ง
quarkus create app th.co.geniustree.quarkus:demo1 -P 3.11.0

quarkus create จะใช้สร้าง project โดยโปรเจคจะมี 3 แบบ app,extension,cli เราจะสร้างเวบแอพ เราเลือก app (ส่วน extension สำหรับคนที่จะทำปลั๊กอิน quarkus เด๋วเราจะมีทำในขั้น advance) สำหรับ cli ก็ตามชื่อเลย ส่วน -P 3.11.0 จะบกว่าเราจะใช้ quarkus platform version อะไร (เครื่องผม quarkus cli เจน version เก่าให้ตลอด)

เราจะได้ project maven ที่มี Rest controller มาให้ Quarkus จะใช้ microprofile api ซึ่งเป็น subset ของ JakataEE แทน spring

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello from Quarkus REST";
    }
}

Enter fullscreen mode Exit fullscreen mode

เราสารถรันใน dev โหมดโดย quarkus dev แล้วลองเข้า http://localhost:8080 จะเห็น devUI ของ Quarkus ซึ่งมี rest endpoint /hello อยู่

Image description

ให้ทดลองแก้โค๊ด แล้วยิง rest ใหม่ มันจะ auto reload ให้ความรู้สึกฟินมากๆ

อ้างอิง
https://quarkus.io/get-started/

Part2 https://dev.to/pramoth/erimtn-quarkus-3-part-2-web-4bkm

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay