The JRush web conference for Java engineers is coming soon! Register now and evolve your Java apps. Rush with us!

Integrating Micronaut with Native Image Kit

Create a microservices constellation with Micronaut and Liberica Native Image Kit


Published September 29, 2021


The microservices are taking over the IT infrastructure; this is a well-established fact. Gigantic monolithic applications are vestiges of the past. With the dawn of new technologies, i.e. frameworks, companies can now create a whole constellation of microservices. The most popular framework is Spring Boot ー a renowned and reliable framework that makes it easy to create Spring-based applications. However, there are a number of alternatives available on the market. In this article we are going to focus on one of them: Micronaut.

If you don’t feel confident enough at this point about microservices, contact us. Our engineers will be happy to consult with you on the matter, or help you with the migration from Oracle JDK to Liberica JDK.

Introducing Micronaut

Micronaut is a JVM-based framework which was developed by Object Computing, Inc. The company stood behind the powerful Groovy-based framework, Grails. Micronaut can be seen as the successor to Grails. It supports three popular languages, Java, Kotlin, and Groovy (with Scala on the roadmap), and was created to simplify the development of microservices. Therefore, it includes all of the tools and features necessary for building a cloud-native microservice application:

  • Compile time dependency injection and inversion of control
  • Support for reactive programming
  • Service discovery
  • AOT compilation
  • HTTP routing
  • HTTP Client with client-side load-balancing
  • Seamless API visibility

Micronaut strives to overcome the issues of other frameworks by providing a fast startup time, a smaller memory footprint, minimal use of proxies and reflection, and fast and easy unit testing. In addition, Micronaut supports the creation of AWS Lambda functions, which is very useful for building serverless applications.

If you have been using Spring up until now, you will find it easy to learn Micronaut.

Now, let us try to build a microservice using this framework.

Creating a microservice

First of all, you need to install Micronaut. You can download a binary file from the official Micronaut site or use any package manager you like: Homebrew, Chocolatey, or SDKMAN!. For example, if you are using SDKMAN!, simply run the following command in your terminal:

sdk install micronaut

This will install all the binary files you need to create, test, and deploy Micronaut applications, together with Micronaut CLI.

Next, create an application (Java will be used by default if you don’t specify the --lang argument):

mn create-app --features=graalvm example.micronaut.micronautguide --build=maven

Note: in case you are using Intellij IDEA, you have to enable annotation processing!

Let us create a basic application with a list of hospitals. The completed example of this application can be found on the official Micronaut website. However, we recommend you to go through the step-by-step process of creating it.

  1. Create a Hospital class and annotate it with @Introspected:

    alt_text

  2. Create a Service and annotate it with @Singleton. Write a method that returns a random hospital:

    alt_text

  3. Next, create a Controller, which will return a Hospital. Micronaut will automatically convert it into JSON format in the response:

    alt_text

    Now your application is all set and ready. You can launch it as it is, but in the following section, we will show you how to do that using Liberica Native Image Kit.

Integrating Liberica NIK

Now that we have created our application, we need to generate a Micronaut native image with Liberica Native Image Kit (NIK). Liberica NIK is a GraalVM-based technology, which allows you to create seamless polyglot projects in different programming languages. NIK is a perfect choice if you wish to minimize resource consumption, achieve record startup times (up to 1/10 of a second), and save on memory. It is based on LTS Liberica JDK with 8 years of access to bug fixes, security updates, and other fixes as needed.

Start with downloading the appropriate version of Liberica NIK (the package already contains Liberica VM, native image and language installables). Alternatively, you can download NIK Core version, which contains Liberica VM and native image without additional languages. After the download is finished, check the file by verifying the checksum in the command line (the checksum should match the one next to the link on the downloads page).

You can also download Liberica NIK using SDKMAN!. Note that this package contains the full version of NIK, not the Core one. Run:

sdk install java 21.2-nik
  1. Configure the runtime environment. For Linux and macOs, in case you have .tar.gz / zip archive, set JAVA_HOME environment variable to the NIK installation directory:

     export JAVA_HOME=$HOME/Development/bellsoft-liberica-vm-openjdk11-21.2.0/
    

    If the installation is performed using the package (deb, pkg/dmg), the installation path for macOS will be the conventional one.

    On Windows, you will have to go through the Control Panel to set your environment variables.

  2. Install the native-image tool using gu install:

     $JAVA_HOME/bin/gu install native-image
    

    As a result, the native executable for your application will contain the application code, necessary libraries, Java APIs, and reduced version of a virtual machine.

  3. After you have installed NIK, you can generate a native image by running the following command:

     ./mvnw package -Dpackaging=native-image
    

    You can create a Docker image by running

     ./mvnw package -Dpackaging=docker-native
    
  4. After that, you can start the application either by running the native executable or using Docker.

    alt_text

    As you see, the application starts in 1.3 seconds, which is an impressive result.

Conclusion

Micronaut is the framework that makes building services easy. Remember that it also supports Kotlin and Groovy, so you can have several microservices written in different languages and united under the same roof of Consul Discovery Service. Thus, your constellation can grow into a microservice galaxy!

As we have discovered, building native images with Liberica Native Image Kit is fast and convenient, and in the end you will enjoy the incredibly rapid startup of your app. If you want to learn more about the functionality of Liberica NIK or the benefits of small containers for your business, feel free to get in touch with our engineers. Together we will spike the performance of your application to the clouds and beyond!

Author image

Peter Zhelezniakov

Software Engineer at BellSoft