Micronaut Framework – Complete Guide with Sample Example

Micronaut Framework is one of the recently introduced JVM-based framework for building lightweight, modular applications and microservices.  In this article we will briefly take you through some of the Micronaut features. This framework is introduced at a NFJS conference in 2019. Why would software developers need yet another a microservice framework as there are already lots of options available with them.

What could be so new about Micronaut ? How its different than Spring, Let’s find out the more about it…..

Is another Microservice Framework required?

We already have Spring and Spring Boot for building microservices? Right, Spring and Spring Boot both are used to build microservices. To Spring’s credit, it had to evolve and adapt with all features to continue support for monolith application development while the microservices adoption occurred.

There are many new frameworks able to address trouble areas in older frameworks that had to build-in support for non-microservice application development. Three such areas that Micronaut address are:

  • Service Startup
  • Memory Footprint
  • Processing Throughput

Micronaut is the tool that addresses common challenges and increases developer productivity and satisfaction which is going to make java development easier. Micronaut is an open source framework which is created by Graeme Rocher and his team. The team which built Grails Frameworks

Micronaut is the reactive and lightweight modern JVM framework designed with microservices and cloud computing. With the productive features of GRAIL Micronaut supports Java, Kotlin and Groovy development. As a built tool it supports both Maven and Gradle. It has similar feature like Spring.

Features that makes Micronaut standout differently

  • Micronaut is Full-stack polyglot framework which supports Java, Kotlin, & Groovy language and it can use both Apache Maven and Gradle as build tools. Micronaut include all the microservices patterns that developers rely on. For example, Micronaut contain native support for service discovery, distributed configuration, client-side load balancing and authentication.
  • Most importantly developers can access various data stores in both blocking and nonblocking ways for client and servers, connect services vai REST HTTP call.
  • Micronaut demonstrates a notable improvement in memory usage through reduced use of runtime reflection and reduces external dependencies injection data at compile time. As a result it alleviate excessive startup times and reduces the memory consumption that can hinder application response.

With above all features micronaut becomes a very promising framework which can be used to build the microservice.

Installing Micronaut on Windows

  1. Download the Micronaut CLI binary from the Micronaut download page. Make sure you select the latest release available for better support.
  2. Unzip the downloaded binary file into a folder on your system
  3. Create the MICRONAUT_HOME environment variable with above directory path.
  4. Update your windows PATH environment variable. You can add the path like : %MICRONAUT_HOME%\bin

After following above steps, open command prompt and issue command as below-

If you see somewhat above screen that means you have successfully installed Micronaut on your machine and it can be used for project creation.

Below commands can help you to create a working project-

create-app — This command creates an application
create-cli-app — This command creates a command line application
create-federation — This command creates a federation of services
create-function — This command creates a serverless function application
create-profile — This command creates a profile

Micronaut Sample Example : Micro World

We will create a Simple CRUD application. We are creating an app using Micronaut Command line interface (cli). By default it uses gradle, but we will use maven. Run below command –

mn create-app micro-world --build maven

The above command creates a micronaut app with the default package micro-world

Now you can see the folder by name microworld with structure

In order to create a microservice that responds to your command you first need a controller.

Let’s create couple of controllers-

mn create-controller HomeController

mn create-controller PersonController

Our app will be simple CRUD application, we will use a Map to keep our data for demo purpose.

Here are the content of HomeController.java

The class is defined as a controller with the @Controller annotation mapped to the path home.

The @Get annotation is used to map the index method to all the requests that use an HTTP GET

HttpStatus.ok is returned as result.

Lets define our domain object Person.

After this, we will define a dummy service around this domain object.

Now the important part, we write our PersonController.

The controller exposes and endpoint /person/add and /person/get. The end points are pretty much self explanatory.

To start the application, run below command

./mvnw compile exec:exec

You can test the application in your browser like Micronaut Endpoint Test

Similarly, you can check the API in Postman client –

Micronaut - Add Endpoint

Micronaut - Get Endpoint

From this small demo application you can see on your own that how simple and fast it is to develop a application using Micronaut.

Conclusion:

Micronaut processing speed is direct impact of fast data access as well as reactive asynchronous invocations. It offers a similar programming model as Spring Boot or, Grails do. Developers coming from those frameworks, who are looking for a framework to use in a distributed architecture, now will enjoy a fast and familiar learning curve.

This will enable you to easily transition from existing application to Micronaut. We have seen the basic of the micronaut features but there are lots to explore. You can also visit the official guide to learn more.

Add a Comment

Your email address will not be published. Required fields are marked *