Building and Running Microservices with Quarkus: A Practical Guide

@Harsh
6 min read4 days ago

--

Microservices have revolutionized the way modern applications are developed, with their ability to divide complex systems into smaller, manageable, and independent units. However, one of the crucial challenges in microservices architecture is ensuring that these services communicate effectively with clients and with each other. Additionally, frameworks like Quarkus have become essential to streamline microservice development. In this blog, we’ll delve deep into how microservices communicate, the role of REST APIs, and how Quarkus simplifies the development process. We’ll also walk through setting up a Quarkus lab and running a sample project.

How Microservices Communicate with Clients and Each Other

In a microservices-based application, services need to interact with clients (often through HTTP requests) and with each other (to exchange data or trigger processes). Microservices communicate over the network, typically using HTTP, which is where APIs (Application Programming Interfaces) and REST APIs (Representational State Transfer) come into play.

  • Client Communication: A client can be a browser, mobile app, or any service that sends an HTTP request (like a GET or POST request) to a microservice. The service must know how to interpret the request and provide the appropriate response, which could be data in the form of JSON, XML, or plain text.
  • Inter-Service Communication: When microservices interact, they typically exchange data using HTTP-based APIs, with JSON being the most common data format. They must know how to return data in a way that other services can understand, ensuring consistency and reliability.

However, just writing the business logic in code doesn’t automatically make it accessible over the network. This is where the need for APIs arises. To make your microservices function accessible over the network, you need to convert your code into endpoints that can be called via HTTP. This is done through REST APIs, which allow services to communicate with standard HTTP methods like GET, POST, PUT, and DELETE.

The Need for a Framework: Why Quarkus?

As microservices grow in complexity, the need for a robust, lightweight framework to manage communication, deployment, and scalability becomes essential. This is where Quarkus comes in.

Quarkus is a Kubernetes-native Java framework designed specifically for creating and running applications, including microservices, in containers. It optimizes Java for cloud-native environments by significantly reducing memory usage and startup times, making it ideal for microservice architectures.

Key benefits of Quarkus:

  1. Developer-Friendly: Quarkus is designed with a developer-first approach, offering hot-reload and fast development cycles.
  2. Optimized for Containers: Quarkus produces small, fast Java applications that run efficiently in containers, perfect for Kubernetes and cloud-native environments.
  3. Built-in RESTEasy: Quarkus uses RESTEasy, a JAX-RS (Java API for RESTful Web Services) implementation, to help developers quickly implement REST APIs.
  4. Polyglot Support: It also supports multiple languages (like Kotlin, Scala) and integrates well with both imperative and reactive programming models.

REST API with Quarkus

In microservices, REST APIs enable services to communicate with clients. Quarkus simplifies the process of building these APIs. Using annotations like @GET, @POST, and @Produces, developers can easily expose their business logic to the network.

Example: Setting Up a REST API in Quarkus

To make a function available via HTTP:

  • Use the @GET annotation for HTTP GET requests.
  • Apply the @Produces annotation to specify the response type, e.g., @Produces("text/plain") for plain text or @Produces("application/json") for JSON data.

Quarkus, behind the scenes, uses the RESTEasy library to implement REST APIs. This abstracts much of the complexity, allowing developers to focus more on writing business logic and less on the intricacies of HTTP communication.

Setting Up Quarkus Lab: A Practical Guide

Now, let’s walk through setting up a Quarkus project and running it on your local environment.

Prerequisites

  1. Install Amazon Corretto JDK: Quarkus requires JDK 11 or higher to run. You can use Amazon Corretto, a no-cost, multiplatform, production-ready distribution of the OpenJDK.

2. Install Maven: Maven is essential for managing dependencies and building your Java project.

3. Install IntelliJ IDEA: IntelliJ IDEA is a popular Java IDE that will help you write and manage your Quarkus project.

  • Download community edition from JetBrains. 🔗

Installing Quarkus and Setting Up the Project

Once you have installed the required software, you can create your first Quarkus project using Maven.

  1. Quarkus Installation via Maven:
  • Use the following Maven command to create a Quarkus project:
mvn io.quarkus:quarkus-maven-plugin:create \
-DprojectGroupId=com.example.tech \
-DprojectArtifactId=myworldapp \
-DclassName="com.example.tech.MyWorld" \
-Dpath="/class"

This will generate a basic Quarkus project with a class named MyWorld and a root path /class.

Once the project is generated, you can run the application in development mode using:

mvn quarkus:dev

Writing a REST API in Quarkus

Now let’s look at the code that implements REST endpoints in Quarkus. Here is the MyWorld class:

package com.example.tech;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/class")
public class MyWorld {

@GET
@Path("/hello")
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "My Hello World";
}

@GET
@Path("/json")
@Produces(MediaType.APPLICATION_JSON)
public String jsonString() {
return "Hey I am JSON Type";
}

@GET
@Path("/Sum")
@Produces(MediaType.TEXT_PLAIN)
public int Sum() {
return 10 + 10;
}
}

Breaking Down the Code

  1. Class-Level Path: The @Path("/class") annotation defines the base path for all the methods in the MyWorld class. This means that every endpoint in this class will be accessed starting from /class.
  2. Exposing the “Hello World” Endpoint:
@GET
@Path("/hello")
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "My Hello World";
}
  • @GET: This annotation specifies that this method will handle HTTP GET requests.
  • @Path("/hello"): This defines the URL path where the method can be accessed, making this method available at /class/hello.
  • @Produces(MediaType.TEXT_PLAIN): Specifies that the response will be returned as plain text.

3. Returning JSON Data:

@GET
@Path("/json")
@Produces(MediaType.APPLICATION_JSON)
public String jsonString() {
return "Hey I am JSON Type";
}

Here, the @Produces(MediaType.APPLICATION_JSON) annotation specifies that the response will be in JSON format. JSON is a standard format used for data exchange in microservices.

4. A Simple Sum Calculation:

@GET
@Path("/Sum")
@Produces(MediaType.TEXT_PLAIN)
public int Sum() {
return 10 + 10;
}
  • This endpoint calculates the sum of two numbers and returns it as plain text. The method can be accessed via /class/Sum.

Running the Quarkus Project

Once the Quarkus project is running in dev mode, you can access these endpoints:

Handling HTTP Routes

Quarkus requires a proper route mapping to ensure that clients are correctly connected to the desired functions. The URL structure consists of:

  • http://localhost:8080: The local server address running Quarkus.
  • /class: The base path defined at the class level using @Path("/class").
  • /hello, /json, or /Sum: The specific paths for each method as defined using @Path("/hello"), etc.

Conclusion

Building microservices with Quarkus allows developers to efficiently create and manage scalable, high-performance applications. Through the use of REST APIs, microservices can communicate with clients and other services. Quarkus, with its built-in support for RESTEasy, enables developers to quickly expose functions to the network via HTTP.

We walked through setting up a Quarkus project, creating REST endpoints using annotations like @GET and @Produces, and running the application locally. The next step for you is to experiment with more complex business logic, such as handling POST requests, interacting with databases, and using Quarkus to build production-ready microservices.

--

--