[JAVA] I tried QUARKUS immediately

Who is QUARKUS?

A new Java framework with GraalVM as the execution VM. It is developed by Red Hat.

The goal of Quarkus is to make Java a leading platform in Kubernetes and serverless environments while offering developers a unified reactive and imperative programming model to optimally address a wider range of distributed application architectures.

Quarkus' goal is to advance Java by providing developers with an integrated reactive and imperative programming model to accommodate a wide range of distributed application architectures in environments such as Kubernetes and serverless. Platform.

As stated in the official blog, it seems that it corresponds to the execution environment such as serverless where Spring and Play are slightly delayed.

Also, how to write with the conventional impeller model One of the great attractions is that it supports both reactive writing styles.

RxJava should allow you to achieve more flexible and less latency-sensitive application behavior.

It also supports existing libraries flexibly, so isn't it more than enough to create something quickly?

Besides using Hibernate ORM with default specifications It seems to support MariaDB and PostgreSQL.

By the way, you can also write in Kotlin.

For more information is here. ↓↓↓ https://quarkus.io/get-started/

Execution environment

macOS Mojave 10.14.3

What you need to run QUARKUS

--JJ1.8 with JAVA_HOME and above --Maven 3.5.3 and above

Maven setup

Since it is a Mac, install it with homebrew

brew update && brew install Maven

Check if it is installed

mvn --version

Success if it returns like this

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T03:41:47+09:00)
Maven home: /usr/local/Cellar/maven/3.6.0/libexec
Java version: 1.8.0_162, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.3", arch: "x86_64", family: "mac"

GraalVM setup

Install GraalVM (graalvm-ce-1.0.0-rc13-macos-amd64.tar.gz) ↓↓↓ https://github.com/oracle/graal/releases

Set GraalVM environment variable (GRAALVM_HOME) Please pass the path according to your shell. bash -> bashprofile zsh -> zshrc

GRAALVM_HOME=/Library/Java/JavaVirtualMachines/graalvm-ce-1.0.0-rc13-macos-amd64/Contents/Home

Launch a new project

Go to the directory where you want to put the project

$ mvn io.quarkus:quarkus-maven-plugin:0.11.0:create \
    -DprojectGroupId=jp.acme \
    -DprojectArtifactId=getting-started \
    -DclassName="org.acme.quickstart.GreetingResource" \
    -Dpath="/hello"

This will launch an application built with Maven based on QUARKUS. The directory structure looks like this

image.png

Dockerfile for / docker Feeling to put the necessary Java code in / java Here there is GreetingResource.java and the contents are ↓↓

GreetingResource.java


package org.acme.quickstart;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello";
    }
}

Contains a simple code that returns "hello" when GET with / hello.

Build

#Start in development mode
$ mvn compile quarkus:dev

Hot reload works when launched in development mode. (Hot reload is a function that compiles and reflects the difference each time a change is made)

Confirm if startup is successful

It will boot on port 8080 with the default settings, so hit curl to see if it returns hello

curl http://localhost:8080/hello

Native binary generation

$ mvn package -Pnative -Dnative-image.docker-build=true

What is a native binary in the first place? It is a machine language that gives instructions necessary for execution, which is prepared for each execution machine. In other words, by using GraalVM, Linux will spit out machine language optimized for Linux.

By the way, this process takes some time. The original document also says that it gives me 15 minutes.

Docker build

Let's build Docker when we get here. Click here for the command

$ docker build -f src/main/docker/Dockerfile -t <Image name> .

This will build the image using the Dockerfile included in the early application you just created. The inside of the Dockerfile looks like this

####
# Before building the docker image run:
#
# mvn package -Pnative -Dnative-image.docker-build=true
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile -t quarkus/getting-started .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/getting-started
#
###
FROM registry.fedoraproject.org/fedora-minimal
WORKDIR /work/
COPY target/*-runner /work/application
RUN chmod 775 /work
EXPOSE 8080
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]

COPY target/*-runner /work/application As you can see, it says that it will use the native binary under target.

Start Docker container

$ docker run -i --rm -p 8080:8080 <Image name>

You can start the previous image by hitting this command. Port number 8080 At this time, the execution speed is displayed, but it is extremely fast.

It started in 0.005 seconds with me.

Summary

By using QUARKUS, it seems possible to develop applications that overcome the weakness of the JVM that supports Docker by default, which was the startup speed. Also, it can be said that it is suitable for large-scale development because a common development environment can be created based on the created Docker image.

I hope this will be an opportunity to create an environment where you can enjoy Java even more. I'm so interested in GraalVM that I'll write another article about GraalVM.

Recommended Posts

I tried QUARKUS immediately
I tried the Java framework "Quarkus"
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried FizzBuzz.
I tried JHipster 5.1
[I tried] Spring tutorial
I tried running Autoware
I tried using Gson
I tried using TestNG
I tried Spring Batch
I tried using Galasa
I tried node-jt400 (Programs)
I tried node-jt400 (execute)
I tried node-jt400 (Transactions)
I tried node-jt400 (Environment construction)
I tried DI with Ruby
I tried node-jt400 (IFS write)
I tried node-jt400 (SQL Update)
I tried using azure cloud-init
I tried Spring State machine
I tried Drools (Java, InputStream)
I tried Rails beginner [Chapter 1]
I tried the Docker tutorial!
I tried using Apache Wicket
I tried the VueJS tutorial!
I tried node-jt400 (SQL query)
I tried using Java REPL
I tried source code analysis
I tried the FizzBuzz problem
I tried node-jt400 (SQL stream)
I tried node-jt400 (IFS read)
I tried putting XcodeGen + SwiftPM
I tried Rails beginner [Chapter 2]
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
I tried to verify yum-cron
I tried Jets (ruby serverless)
I tried metaprogramming in Java
I tried using anakia + Jing now
I tried Angular tutorial + SpringBoot + PostgreSQL
I tried something called recursive search
I tried using Spring + Mybatis + DbUnit
I tried using JOOQ with Gradle
[K8s] I tried communication between pods!
I tried morphological analysis with MeCab
I tried a little digdag docker.run_options
I tried to summarize iOS 14 support
I tried to interact with Java
I tried UDP communication with Java
I tried to explain the method
I tried putting Domino11 in CentOS7
[Rails] I tried deleting the application
I tried using Java8 Stream API
I tried Java's micro-benchmark tool JMH
I tried using JWT in Java
I tried GraphQL with Spring Boot
I tried to summarize Java learning (1)
I tried to understand nil guard