What is Quarkus and how to create it

Tram Ho

Traditional Java stacks such as Monolithic architecture

We usually write all Authorization, presentation, business,… in the same project and like the example above we have

  • Checkout feature
  • Order Management System (OMS – Order Management System)
  • Inventory management feature (Inventory)
  • Product information management feature (Merchandise)

And finally we give this project 4 Cores CPU, 12GB Ram, 10TB hard drive .

Screen Shot 2023-01-31 at 11.48.38.png

With this model only stable for small stores, low request volume, but if applied to larger scale with increasing number of requests, it is necessary to scale duplicate an instance (horizontal scale). .

Assuming the Checkout feature is to handle the most requests and requires doubling the resources to process it thoroughly, otherwise it will die or affect other features in a peak hour frame. Now we have to duplicate 4 more CPU Cores, 12GB Ram, 10TB hard drive .

Inferred now it takes up to 8 CPU Cores, 24GB Ram, 20TB hard drive for the Checkout feature to work stably.

We should only need to allocate resources in the right place for the checkout feature, so such additional allocation will cause redundancy and cost.

Therefore, the Microservice model was born to separate and avoid affecting other features.

If we apply Microservices to this project, we will have

  • Checkout Service
  • OMS Service (Order Management System)
  • Inventory Service (Inventory)
  • Merchandise Service (Merchandise)

We will also give these services 4 Cores CPU, 12GB Ram, 10TB hard drive . evenly divided.

Screen Shot 2023-01-31 at 11.51.56.png

Also, the Checkout service has a much larger request than all other services, now we just need to duplicate the instance of the Checkout service, separate, easily monitor and optimize the cost a lot compared to the traditional architecture. .

And Quarkus a Framework for Java language was born to develop serving the Microservice model.

Screen Shot 2023-01-31 at 11.58.32.png

Quarkus is a new Java framework, first released in March 2019, created for Java developers creating cloud-native applications. It is optimized for GraalVM and HotSpot, allowing for fast boot times and low memory footprint.

Quarkus is designed to run on Kubernetes , a popular platform for deploying and managing applications while also providing metrics and tracking.

Quarkus is also an Open Source licensed Apache License version 2.0. Most importantly it is an open community where all developers can contribute, ideas, openly discuss,…

So why don’t we use Spring with a larger community? Here I will not compare these two frameworks with each other, I just want to introduce to everyone a new framework to have more options for projects.

But if you want to refer, you can read more about the comparison of these two men here .

Create a project in Quarkus

First, I will show you how to create a Java project with Quarkus and interact with the API.

To create a Quarkus project, go here .

Screen Shot 2023-01-31 at 13.57.27.png

The CONFIGURE YOUR APPLICATION section will be available

  • Group : Group name.
  • Artifact : The name of the artifact.
  • Build Tool : There are 2 options, Maven or Gradle. Here I use Maven.
  • Version : Version of the project.
  • Java Version : Java version. Here I use Java 17.
  • Starter Code : Yes
  • Filter & Search : Below the Filter & Search bar is a list of libraries that Quarkus suggests for you. Here I will search and check into the RESTEasy Classic Jackson library, this library will convert to json form to respond to the Client.

When done, click the Generate your application button and click DOWNLOAD THE ZIP project to download it to your computer.

Screen Shot 2023-01-31 at 14.16.26.png

After extracting the .zip file, open your favorite Text Editor or Idle. Here I use IntelliJ IDEA

Screen Shot 2023-01-31 at 14.25.23.png

This is the directory structure of the project. Usually you only need to care about the src folder and the pom.xml file

In the folder src -> main -> java -> org.duynd.pratice (the folder you named Group before) we will mainly code in this.

The resources directory we will use the application.properties file to configure things that are needed during the development phase.

Interacting with APIs

Click on the file GreetingResource

This is the default file Quarkus created for me when I added the RESTEasy Classic Jackson library before.

@Path : Use this Anotation when you want to define Class that is Controller.

So when you run on the browser with the url http://localhost:8080/hello , the program will execute the hello() function and return it to bowser. Why ? Since you have defined @Path in the class, all functions in the class will have a preflix of /hello .

For example if you add

on the hello() method you will have the route http://localhost:8080/hello/hi

@GET : This annotation defines the method of the HTTP protocol (GET, POST, PUT, DELETE).

@Produces : This annotation defines the method’s response method.

You can remove and not use this Anotation, but if you want to re-scope it will be very good. For more details, I will talk more about this guy in the next post.

Here is defined as MediaType.TEXT_PLAIN then this method says it will return a plain text to the Client.

Ok, now run the program as follows:

And now visit http://localhost:8080/hello you will see the output.

Screen Shot 2023-01-31 at 15.36.40.png

Now I will add another method and add @Path("hi") to define a route for it to try.

And now try accessing the link http://localhost:8080/hello/hi

Screen Shot 2023-01-31 at 15.41.15.png

So we have briefly learned how to create a Quarkus project and interact with the API in the most basic way.

I will try to write a series about Quarkus so that newcomers can approach and easily understand it.

Hope you guys support and comment

Share the news now

Source : Viblo