Building Microservices with Quarkus and Kotlin

Kenneth Kogi
6 min readDec 12, 2022

--

Hello there 👋, Its Kogi here😊

Quarkus

So today I am going to take you through building microservices with quarkus, a sub atomic java framework. In this guide, we will cover the following topics:

  • Building REST-based application.
  • Connecting to a PostgreSQL Database
  • Exposing Open API/Swagger documentation
  • Running applications on the local machine with Quarkus plugin
  • Testing with JUnit and RestAssured. (Not yet ready)
  • Deploying and running Quarkus applications on Kubernetes. (Not yet ready)

Lets buckle up, and start the journey… So whats microservices architecture to start with and why are they essential in the modern day software development.

Traditional software programs are built as in all-in-one architecture, wherein all aspects of the software operate as a single unit E.g Authorization, Business Logic, Database Layer. This what is referred to as a monolithic architecture. Monoliths can be convenient early on in a project’s life for ease of code management, cognitive overhead, and deployment. This allows everything in the monolith to be released at once. Microservices is an approach to application development in which a large application is built as a suite of modular services (i.e. loosely coupled modules/components). Each module supports a specific business goal and uses a simple, well-defined interface to communicate with other sets of services. Instead of sharing a single database as in Monolithic application, each microservices has its own database. Having a database per service is essential if you want to benefit from microservices, because it ensures loose coupling. Each of the services has its own database. Moreover, a service can use a type of database or a programming language that is best suited to its needs. You can learn more about microservices and monoliths from here.

So whats Quarkus, Its a java framework tailored specifically for building modern microservices and deployment to Kubernetes. It allows java developers to create applications for a modern, cloud-native world. If you compare it with other frameworks like Spring Boot / Spring Cloud or Micronaut, the first difference is native support for running on Kubernetes or Openshift platforms. It is built on top of well-known Java standards like CDI, JAX-RS, and Eclipse MicroProfile which also distinguishes it from Spring Boot or Micronaut. You can check out this article which goes in depth comparing the different frameworks.

Getting started with Quarkus

Prerequisites: Java JDK Installed, Gradle or Maven, Intellij or an IDE of your choice.

Bootstrapping the project

You can create a new Quarkus project using the quarkus CLI or through bootstrapping the project from quarkus.io.

For this tutorial, I am going with the latter. Also I will be using Kotlin instead of Java. (I’m not scared of Java😂, its just preference and simplicity).

Next, configure your application, by setting up the build tool for my case I am using Gradle, then add extensions needed for the application to run. Think of Quarkus extensions as your project dependencies. Extensions configure, boot and integrate a framework or technology into your Quarkus application. They also do all of the heavy lifting of providing the right information to GraalVM for your application to compile natively.

Quarkus Extensions Example:

  • ReastEasy Jax-RS — REST framework implementing JAX-RS and more.
  • RESTEasy Jackson — Jackson serialization support for RESTEasy.
  • SmallRye Open API — document your REST APIs with Open API — comes with Swagger UI.
  • Hibernate ORM with Panache — define your persistent model in Hibernate ORM with Panache.
  • JDBC Driver — PostgreSQL database connector.

Check here for a full list of available extensions.

Running the application

Once generated, Extract the files and open the project with your favorite IDE. Now we are ready to run our application.

./gradlew quarkusDev

If you get this message, you project is now up and running.

Configure PostgreSQL Database

Set a postgres database, then in your application properties, add the credentials to for the application to access the database service.

Set Up the Project Folder Structure

For the folder structure, I prefer following the N-tier architecture approach, where the processing, data management, and presentation functions are logically separated. With this not only does your software gain from being able to get services at the best possible rate, but it’s also easier to manage. This is because when you work on one section, the changes you make will not affect the other functions. And if there is a problem, you can easily pinpoint where it originates.

  • Controllers/Resources — The top most layer where all the endpoints are defined.
  • Services — This layer coordinates the application, makes logical decisions, evaluations and performs calculations. It also processes data between the two surrounding layers.
  • Models — Define our application entities and the relationships between them.
  • Repositories — Here information is stored or retrieved from the the database. The information is then passed back to the service layer for processing. Instead of writing native SQL queries, we use Hibernate with Panache as the ORM tool.
  • Data — define all the DTO’s .
  • Enums — define the enums.
Project Folder Structure

Build a REST based Application.

To demonstrate how to Rest API’s are implemented in quarkus, we are going to build a simple book management system where we expose all the CRUD endpoints.

model

JPA allows Definition of all the database columns and relationships, also enforcing constraints using Hibenache validator.

controller

REST controller looks very typical for those who are familiar with SpringBoot or Java EE:

service

Holds the logic of the application, acts as an intermediary between the controller and repository layer.

repository

If need be, you can also define custom queries inside the repository.

Open API/Swagger UI documentation

Finally, you can document and test all the created endpoints using swagger UI and Open API at http://localhost:8080/q/swagger-ui/

Thanks for reading and please give me a like or comment if there is something incorrectly written or it can be improved in any way!

Here is the link to the GitHub Repository.

--

--