Get started with serverless Java with the lightweight framework Micronaut!

Serverless is very exciting. Some people said that Java is not suitable for serverless due to the so-called cold start problem, but the situation has changed due to the appearance of micro frameworks such as Micronaut introduced this time and GraalVM's native image creation function (Substrate VM). It has changed a lot.

As for the flow around here, there are wonderful lecture materials at "JJUG CCC 2019 Fall" by @_kensh and @jyukutyo, so let's be excited to see them.

-About Java in the Serverless era -Future illuminated by multilingual virtual machine GraalVM

Now, let's actually make serverless Java with Micronaut!

Why Micronaut?

Micronaut claims to be a "framework for serverless" in its tagline. We also held a session about Micronaut and Lambda at "re: Invent 2019". Compared to other frameworks, I focused on serverless, and I personally found it very attractive, so I chose this time.

Is there any other framework?

I think the following three are the major micro-frameworks that can be used by Java (JVM language including Java).

All of them are frameworks that I'm looking forward to in the future. I compared the catchphrases, so please refer to it.

catch copy

Framework catch copy Free translation
Micronaut A modern, JVM-based, full-stack framework for building modular, easily testable microservice and serverless applications. It's a modern full stack framework for microservices and serverless
Quarkus A Kubernetes Native Java stack tailored for OpenJDK HotSpot and GraalVM. It's a Kubernetes native framework optimized for OpenJDK HotSpot and GraalVM.
Helidon Lightweigt. Fast. Crafted for Microservices. It's a lightweight, fast framework made for microservices.

Google Trends (USA, last year)

Screen Shot 2019-12-04 at 23.01.22.png

GitHub Stars

Screen Shot 2019-12-04 at 22.59.19.png https://www.githubcompare.com/

Other

--In IntelliJ IDEA 2019.3 released on November 28, 2019, Micronaut, Quarkus, and Helidon are supported as "Microservices frameworks support". You can see the degree of attention! --Spring Framework also seems to support GraalVM in the next version 5.3. I'm looking forward to this too!

Micronaut setup

Now let's set up Micronaut. The Official Site recommends installing with SDKMAN, so let's install it according to that procedure.

Installing SDKMAN

$ curl -s https://get.sdkman.io | bash

Restart the terminal or execute the following shell

$ source "$HOME/.sdkman/bin/sdkman-init.sh"

OK if the sdk command passes

$ sdk version
SDKMAN 5.7.4+362

Installing Micronaut

$ sdk install micronaut

OK if the mn command passes

$ mn -V
| Micronaut Version: 1.2.7
| JVM Version: 11.0.5

Creating Lambda functions in Micronaut

The project template is created via the mn command. You can start it in interactive mode by entering only mn. It is convenient because you can also complete commands. You can also trace the command history.

$ mn
| Starting interactive mode...
| Enter a command name to run. Use TAB for completion:
mn> 

You can check how to use the command with mn> help.

mn> help
Usage: mn [-hnvVx] [COMMAND]
Micronaut CLI command line interface for generating projects and services.
Commonly used commands are:
  create-app NAME
  create-cli-app NAME
  create-federation NAME --services SERVICE_NAME[,SERVICE_NAME]...
  create-function NAME
・ ・ ・

This time I want to create a Lambda function, so let's use create-function.

mn> create-function hello-world
| Generating Java project...
| Function created at /Users/cojohnny/micronaut/hello-world
| Initializing application. Please wait...

Exit the interactive mode once and check the generated project.

mn> exit
$ cd hello-world/
$ tree
.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── micronaut-cli.yml
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── hello
    │   │       └── world
    │   │           ├── Application.java
    │   │           ├── HelloWorld.java
    │   │           └── HelloWorldFunction.java
    │   └── resources
    │       ├── application.yml
    │       └── logback.xml
    └── test
        └── java
            └── hello
                └── world
                    ├── HelloWorldClient.java
                    └── HelloWorldFunctionTest.java

Let's take a look at the generated source. The apply method in HelloWorldFunction.java is a Lambda function. The current implementation is just returning the input JSON setting of name.

HelloWorldFunction.java


package hello.world;

import io.micronaut.function.executor.FunctionInitializer;
import io.micronaut.function.FunctionBean;
import javax.inject.*;
import java.io.IOException;
import java.util.function.Function;

@FunctionBean("hello-world")
public class HelloWorldFunction extends FunctionInitializer implements Function<HelloWorld, HelloWorld> {

    @Override
    public HelloWorld apply(HelloWorld msg) {
         return msg;
    }

    /**
     * This main method allows running the function as a CLI application using: echo '{}' | java -jar function.jar 
     * where the argument to echo is the JSON to be parsed.
     */
    public static void main(String...args) throws IOException {
        HelloWorldFunction function = new HelloWorldFunction();
        function.run(args, (context)-> function.apply(context.get(HelloWorld.class)));
    }    
}

Let's build with gradlew.

$ ./gradlew assemble

Two types of Jar are completed.

$ tree build/libs/
build/libs/
├── hello-world-0.1-all.jar
└── hello-world-0.1.jar

The one with -all at the end of the file name is" Fat Jar "which contains the dependent libraries, so upload that to Lambda.

Lambda function setup

Let's upload the created "hello-world-0.1-all.jar" to AWS. Set "hello.world.HelloWorldFunction :: apply" in Handler.

image.png

Execution of Lambda function

Now let's execute the Lambda function. Set the JSON of the request from the "Test" button at the top of the Lambda console. Specify the following for JSON.

{
  "name": "Lambda"
}

image.png

Execute "Test"! It worked!

image.png

Summary

This time it was just a simple process execution, but you can easily create and execute a Lambda function that includes the framework. By using a lightweight framework such as Micronaut, you will be able to build Lambda while using the rich features of the framework, so it seems that the application of serverless architecture to large-scale applications will continue to expand.

Regarding the cold start, which was the biggest concern, the hurdle for application to the production environment is getting lower and lower because a powerful solution called "provisioned concurrency" announced at re: Invent 2019 was presented the other day.

I'm really looking forward to the serverless world, which is expected to expand further in 2020! In particular, even those that did not have too familiar to less server have been involved in large-scale system of Java, I think that it is in advance at the start is now. Let's get started with serverless Java together!

reference

Try the latest Java frameworks, Helidon, Micronaut, Quarkus together up to native-image Micronaut User Guide AWS Lambda announces Provisioned Concurrency

Recommended Posts

Get started with serverless Java with the lightweight framework Micronaut!
[Java] [Play Framework] Until the project is started with Gradle
[Java] Get the date with the LocalDateTime class
Understanding the MVC framework with server-side Java 1/4 View
Understanding the MVC framework with server-side Java 3/4 Controller
Understanding the MVC framework with server-side Java 2/4 Model
Get started with Gradle
How to deploy Java to AWS Lambda with Serverless Framework
Experienced Java users get started with Android application development
[Java] Get the date 10 days later with the Calendar class
Get started with Spring boot
Get started with DynamoDB with docker
Getting Started with Java Basics
First year Java developers on udemy get started with PHP
Now is the time to get started with the Stream API
Let's get started with parallel programming
Returning to the beginning, getting started with Java ② Control statements, loop statements
[Java1.8 +] Get the date of the next x day of the week with LocalDate
Add Bean Validation with Micronaut (Java)
Consideration on the 2017 Java Persistence Framework (1)
I tried the Java framework "Quarkus"
Follow the link with Selenium (Java)
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
Get the result of POST in Java
[Java] Get List / Map elements with Iterator
Guess about the 2017 Java Persistence Framework (3) Reladomo
Getting Started with Ruby for Java Engineers
[Java] Get the day of the specific day of the week
[Note] How to get started with Rspec
Getting Started with Java Starting from 0 Part 1
[Java] How to get the current directory
Getting started with the JVM's GC mechanism
How to get the date in java
[Java8] Search the directory and get the file
Try using the Wii remote with Java
java framework
Let's get started with Java-Create a development environment ②
Let's get started with Java-Create a development environment ①
Links & memos for getting started with Java (for myself)
[LeJOS] Let's control the EV3 motor with Java
[Java] Set the time from the browser with jsoup
[Java] Get the length of the surrogate pair string
How to use Java framework with AWS Lambda! ??
[JAVA] Get only the file name, excluding the extension
Getting Started with Doma-Using Subqueries with the Criteria API
Calculate the similarity score of strings with JAVA
[Java] Get images with Google Custom Search API
How to get started with Eclipse Micro Profile
Maybe it works! Let's get started with Docker!
Getting Started with Doma-Using Joins with the Criteira API
Find the Fibonacci number with the Fork / Join Framework
Rails beginners tried to get started with RSpec
[Java] How to get the redirected final URL
Getting Started with Doma-Introduction to the Criteria API
[Java] How to get the authority of the folder
Java starting with JShell-A peek into the Java world
[LeJOS] Get EV3 sensor value remotely with Java
Get along with Java containers in Cloud Run
Going back to the beginning and getting started with Java ① Data types and access modifiers
[DDD] What is the most accessible architecture to get started with Domain Driven Design?