Book Image

Building Microservices with Micronaut®

By : Nirmal Singh, Zack Dawood
Book Image

Building Microservices with Micronaut®

By: Nirmal Singh, Zack Dawood

Overview of this book

The open source Micronaut® framework is a JVM-based toolkit designed to create microservices quickly and easily. This book will help full-stack and Java developers build modular, high-performing, and reactive microservice-based apps using the Micronaut framework. You'll start by building microservices and learning about the core components, such as ahead-of-time compilation, reflection-less dependency injection, and reactive baked-in HTTP clients and servers. Next, you will work on a real-time microservice application and learn how to integrate Micronaut projects with different kinds of relational and non-relational databases. You'll also learn how to employ different security mechanisms to safeguard your microservices and integrate microservices using event-driven architecture in the Apache Kafka ecosystem. As you advance, you'll get to grips with automated testing and popular testing tools. The book will help you understand how you can easily handle microservice concerns in Micronaut projects, such as service discovery, API documentation, distributed configuration management, fallbacks, and circuit breakers. Finally, you'll explore the deployment and maintenance aspects of microservices and get up to speed with the Internet of Things (IoT) using the Framework. By the end of this book, you'll be able to build, test, deploy, and maintain your own microservice apps using the framework.
Table of Contents (20 chapters)
Section 1: Core Concepts and Basics
Section 2: Microservices Development
Section 3: Microservices Testing
Section 4: Microservices Deployment
Section 5: Microservices Maintenance
Section 6: IoT with Micronaut and Closure

Working on a hello world project in the Micronaut framework

To understand the practical aspects of using the Micronaut framework for developing a microservice, we will work with a hello world project. This will help you quickly get started with the Micronaut framework and also give you first-hand experience of how easy it is to do microservices development.

Micronaut works seamlessly with the Maven and Gradle packaging managers. We will cover one example for each using the Micronaut CLI as well as Micronaut Launch (web interface) for generating barebones projects.

Creating a hello world project using the Micronaut CLI

Please take the following steps to create a hello world application using the Micronaut CLI:

  1. Open the terminal (or Command Prompt).
  2. Change the directory to your desired directory where you want to create the hello world project.
  3. Type the following command:
    mn create-app hello-world-maven --build maven
  4. Wait for the Micronaut CLI to finish and it will create a hello-world-maven project. The create-app command will create a boilerplate project for you with a Maven build and your system-installed Java version. It will create as well as a sample test class called
  5. To explore your freshly created hello-world-maven project, open this project in your preferred IDE.
  6. To run your project, run the following command in a Bash terminal:
    ./mvnw compile exec:exec

    Please note that if you are copying the project from somewhere else, then it's required to regenerate mvnw by typing the following command:

    mvn -N io.takari:maven:wrapper
  7. The Maven wrapper will build and run your project on http://localhost:8080 by default.

Adding HelloWorldController

To create a simple endpoint, let's add a simple controller to the hello-world-maven project:

  1. Add a web package to our hello-world-maven project.
  2. Add a HelloWorldController Java class. It will contain a simple hello endpoint:
    public class HelloController {
        public String helloMicronaut() {
            return "Hello, Micronaut!";

    HelloController is accessible on the …/hello path. helloMicronaut() will generate a plain text "Hello, Micronaut!" message.

  3. Rerun your application and hit http://localhost:8080/hello/ in a browser window. The server will return the following response:
Figure 1.9 – Hello, Micronaut!

Figure 1.9 – Hello, Micronaut!

By default, the application will be accessible on port 8080, and this port can be changed in the application properties.

So far, we have worked on a hello world project using the Micronaut CLI. Next, we will explore Micronaut Launch, which is a web interface, to generate a boilerplate project.

Creating a hello world project using Micronaut Launch

Micronaut Launch ( is an intuitive web interface that came into existence with Micronaut 2.0.1. We can use this interface to quickly generate boilerplate for different kinds of Micronaut applications (such as server applications, the CLI, serverless functions, a messaging application, and so on). Let's quickly use this to generate a hello world application for us.

Please follow these instructions to generate the hello world project using the Micronaut Launch web interface:

  1. Open Micronaut Launch in a browser window:
  2. Under Application Type, choose Application.
  3. Under Micronaut Version, choose 2.0.1.
  4. For the Java version, choose Java 14.
  5. For Language, choose Java.
  6. Give a base package name such as com.packtpub.micronaut.
  7. Choose Gradle as the build option.
  8. Give a name to the application, such as hello-world-gradle.
  9. Choose JUnit as the testing framework
  10. After you've finished choosing all the options, click on GENERATE PROJECT.

After choosing the preceding options and providing various inputs, the Micronaut Launch interface should look as follows:

Figure 1.10 – Using Micronaut Launch to generate a boilerplate project

Figure 1.10 – Using Micronaut Launch to generate a boilerplate project

Your project boilerplate source code will be generated into a zipped file. You can unarchive this zipped file into your desired directory and open it in your preferred IDE. Just like the previous example (hello-world-maven), we can add a basic HelloWorldController instance.

To run your project, run the following command in a Bash terminal:

gradlew.bat run

When the project is running, go to http://localhost:8080/hello and you should see the Hello, Micronaut! message in the browser tab.

In this section, we explored how to get started with the Micronaut framework by developing small hello world projects using the Micronaut CLI as well as the Micronaut Launch user interface. This small exercise will be a good preface for what we will cover in the next chapter.