In this blog, we will discuss Micronaut in context of what and why, and we will also see how to create an application from Micronaut launch.
Before taking a deep dive into Micronaut, let us get familiar with some of the associated terminologies. Having this information will help us better understand it.
Ahead of time compilation(AOT) – It is the pre-computation of application code using closed-world static analysis. A fancy way of saying, do more at compile time and less at run time.
Reflection – Reflection in Java is an API (Application Programming Interface) that analyzes or changes classes, methods, and interfaces during runtime. It involves examining or making changes to the run-time behavior of a class.
What is Micronaut?
The Micronaut framework is designed for building lightweight, modular applications, and microservices in the cloud. It’s an open-source JVM-based framework tailored for building and testing low-memory microservices, serverless applications, and message-driven microservices based on cloud architecture.
Micronaut uses ahead-of-time (AOT) compilation to pre-compute the application requirements at compile time. As a result, significantly less memory is consumed, startup times are shortened, and reflection is eliminated from the framework infrastructure.
In contrast to other frameworks, Micronaut does not depend on the size of the application’s codebase for startup time and memory consumption. This is because it analyzes metadata and builds its dependency injection at compilation time instead of runtime, which makes integration testing faster and easier.
A unique characteristic of Micronaut is that it does not modify byte code during compilation time, and it removes all reflection levels. Thus, maximum speed can be achieved at runtime.
Micronaut incorporates existing microservices patterns that developers rely on, helping to eliminate the need to piece together separate tools and frameworks.
Features that make it so well suited for microservices.
- Support for frameworks that implement the Reactive Streams standard.
- Support for messaging systems like Kafka, RabbitMQ, MQTT, JMS, and Nats.io.
- Supports serverless functions for cloud providers like AWS Lambda, Oracle Functions, Google Cloud Functions, and Azure Functions.
- Supports OpenAPI documentation by creating a YAML file at compilation time.
- GraalVM readiness to further reduce startup time.
Pros and Cons of Micronaut
- Cloud Native framework for web applications and microservices
- Compile time reflection
- Minimal cold startup time and memory use
- Reactive stack
- Polyglot framework (Supports Java, Kotlin, Groovy)
- Minimal use of proxies
- No runtime bytecode generation
- Easy unit testing
- Smaller community
How to create a Micronaut application?
There are multiple ways to create an application and the fastest way without installing anything is to go to the micronaut.io homepage.
On the top right corner, there is a button with the text LAUNCH.
Upon clicking this button, a new page opens where the application can be configured.
This page is called Micronaut Launch. It provides a lot of settings for creating an application.
It is possible to define multiple application types like a command line, serverless, gRPC, and messaging-driven application. Select the standard Micronaut application type.
Then select Java version 17.
Next specify name and base package. According to Java package guidelines, the base package name should be the domain name in reverse order.
There is a line of buttons at the bottom.
The leftmost button is called “FEATURES”. On clicking it, a pop-up opens that lists all the modules Micronaut can support.
Now search and select netty-server as the runtime.
Before we generate the project, we can inspect the diff, which is quite handy when adding new features. For now, click “PREVIEW” as we are creating a new project.
Clicking the “PREVIEW” button allows viewing the source structure. The application has a standard Java structure that includes a pom.xml, a maven wrapper, and a source folder.
To generate the project, close this preview and press the “GENERATE PROJECT” button. It is possible to push a project directly to GitHub, download as a zip and use some commands to generate a project. Click on Download Zip to download the project as a zip file. Now unzip the zip file and open it in IntelliJ.
There is a Application class containing the Java main method. This class contains the static Micronaut.run method, which is the starting point for the application.
For configuring the application, there is an application.yml file.
By default, Micronaut includes the logback logging implementation in the logback.xml file.
There is also DemoAppTest class which contains one test method asserting that the application can start up.
Run the test case to see if that works.
The log output indicates the use of a test environment. By the way, Micronaut has built in support for different environments, which is quite nice.
There is also the micronaut-cli.yml file, which uses Micronaut CLI along with it. It’s meta information when using CLI commands.
And as its a Maven project, so there is a pom.xml file. This file contains all the dependencies.
We can also create an application through Micronaut CLI. For more information on creating an application through the CLI, click here.
To know more about Micronaut, you can visit this link.
Click here to learn more about reflection in Java.
To read more tech blogs, feel free to visit Knoldus Blogs.