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!
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.
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.
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. |
https://www.githubcompare.com/
--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!
Now let's set up Micronaut. The Official Site recommends installing with SDKMAN, so let's install it according to that procedure.
$ 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
$ sdk install micronaut
OK if the mn command passes
$ mn -V
| Micronaut Version: 1.2.7
| JVM Version: 11.0.5
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.
Let's upload the created "hello-world-0.1-all.jar" to AWS. Set "hello.world.HelloWorldFunction :: apply" in Handler.
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"
}
Execute "Test"! It worked!
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!
Try the latest Java frameworks, Helidon, Micronaut, Quarkus together up to native-image Micronaut User Guide AWS Lambda announces Provisioned Concurrency
Recommended Posts