Building Native Images with Spring Native and Liberica tools with a Speed Comparison – 用Spring Native和Liberica工具构建本地图像,并进行速度比较

最后修改: 2022年 5月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

As microservice architecture gains popularity, giant monolithic applications are becoming a thing of the past. Java isn’t staying stagnant and instead is adapting to modern needs. For example, Oracle, Red Hat, BellSoft, and other contributors are actively developing the GraalVM project. In addition, the microservices-specific framework Quarkus was released a year ago. As far as Spring Boot is concerned, VMware has been working on the Spring Native project for two years.

随着微服务架构的普及,巨大的单体应用正在成为过去。Java并没有停滞不前,而是在适应现代需求。例如,Oracle、Red Hat、BellSoft和其他贡献者正在积极开发GraalVM项目。此外,专门针对微服务的框架Quarkus已于一年前发布。就Spring Boot而言,VMware已经在Spring Native项目上工作了两年。

As a result, thanks to the collaboration between VMware and BellSoft, Spring Native became an end-to-end native image solution, which includes the Liberica Native Image Kit, a tool based on the GraalVM source code. Spring Native and Liberica NIK allow developers to create native executables of Spring Boot applications that optimize resource consumption and minimize startup time.

因此,由于VMware和BellSoft之间的合作,Spring Native成为一个端到端的本地映像解决方案,其中包括Liberica Native Image Kit,这是一个基于GeralVM源代码的工具。Spring Native和Liberica NIK允许开发人员创建Spring Boot应用程序的本地可执行文件,以优化资源消耗并最大限度地减少启动时间。

In this tutorial, we’ll discover how to use the native image technology with Spring Boot applications by building and running the same app in three ways—as a classic JAR file; as a native image container with Liberica JDK and Spring Native; and as a native image with Liberica Native Image Kit. Then we’ll compare their startup speeds. In every case, we’ll use the petclinic JDBC application from the Spring Native project as an example.

在本教程中,我们将通过以三种方式构建和运行相同的应用,发现如何在Spring Boot应用中使用本地镜像技术–作为经典的JAR文件;作为使用Liberica JDK和Spring Native的本地镜像容器;以及作为使用Liberica本地镜像套件的本地镜像。然后我们将比较它们的启动速度。在每种情况下,我们都将使用Spring Native项目中的petclinic JDBC应用程序作为例子。

2. Installation of Liberica JDK

2.安装Liberica JDK

First, let’s install the Java runtime for your system. We can visit the Liberica JDK download page and select the version for our platform. Let’s use JDK 11, the x86 Linux Standard JDK package.

首先,让我们为您的系统安装Java运行时。我们可以访问Liberica JDK下载页面,选择适合我们平台的版本。让我们使用JDK 11,即x86 Linux标准JDK包。

There are two ways to install Liberica JDK. One is through a package manager or by downloading the .tar.gz package (or .zip package for Windows).

有两种方法来安装Liberica JDK。一种是通过软件包管理器或下载.tar.gz包(或Windows的.zip包)。

The latter is a more advanced method, but don’t worry, it takes four steps only. We first need to change to the directory in which we want to install:

后者是一个更高级的方法,但不要担心,它只需要四个步骤。我们首先需要改变到我们要安装的目录。

cd directory_path_name

Without leaving the directory, we can run:

不离开这个目录,我们就可以运行。

wget https://download.bell-sw.com/java/11.0.14.1+1/bellsoft-jdk11.0.14.1+1-linux-amd64.tar.gz

If we don’t have a wget command, we can install it through brew install wget (for Linux and Mac).

如果我们没有wget命令,我们可以通过brew install wget(用于Linux和Mac)来安装它。

This way, we’ll unpack the runtime into the directory we’re in:

这样,我们就会把运行时解压到我们所在的目录中。

tar -zxvf bellsoft-jdk11.0.14.1+1-linux-amd64.tar.gz

After the installation has been completed, we can delete the .tar.gz file if we want to save the disc space.

安装完成后,如果我们想节省磁盘空间,我们可以删除.tar.gz文件。

Finally, we need to set up the JAVA_HOME variable by pointing at the Liberica JDK directory:

最后,我们需要设置JAVA_HOME变量,指向Liberica JDK目录。

export JAVA_HOME=$(pwd)/jdk-11.0.14.1

Please note: macOS and Windows users can refer to the Liberica JDK Installation Guide for instructions.

请注意:macOS和Windows用户可以参考Liberica JDK安装指南以获得指导。

3. Getting the Spring Native Project

3.获得Spring Native项目

We can get the Spring Native project with petclinic app samples by running:

我们可以通过运行Spring Native项目和petclinic应用样本获得。

git clone https://github.com/spring-projects-experimental/spring-native.git

4. Building the JAR File

4.构建JAR文件

We want to work with one sample from the whole Spring Native project, so go to the directory with spring petclinic JDBC by running:

我们想用整个Spring Native项目中的一个样本来工作,所以通过运行spring petclinic JDBC进入目录。

export PROJECT_DIR=$(pwd)/spring-native/samples/petclinic-jdbc && cd $PROJECT_DIR

To build the JAR file, we can apply this command:

为了构建JAR文件,我们可以应用这个命令。

./mvnw clean install

This will get us a 24 MB target/petclinic-jdbc-0.0.1-SNAPSHOT.jar. We’ll test it by running:

这将得到一个24MB的target/petclinic-jdbc-0.0.1-SNAPSHOT.jar。我们将通过运行来测试它。

java -jar target/petclinic-jdbc-0.0.1-SNAPSHOT.jar

5. Build a Native Image Container with Liberica JDK

5.用Liberica JDK构建一个本地镜像容器

Now let’s containerize our app.

现在让我们把我们的应用程序容器化。

Make sure that our Docker daemon is running. Note that we need to allocate at least 8 GB of memory to Docker if we use Windows or macOS x86. From the Spring petclinic JDBC application directory, we need to input the command:

确保我们的Docker守护进程正在运行。注意,如果我们使用Windows或macOS x86,我们需要为Docker分配至少8GB的内存。从Spring petclinic JDBC应用程序目录中,我们需要输入以下命令。

./mvnw spring-boot:build-image

This will build the native image container with Spring Boot that we can launch with:

这将用Spring Boot构建本地镜像容器,我们可以用它来启动。

docker run -it docker.io/library/petclinic-jdbc:0.0.1-SNAPSHOT

If we work with Apple M1, this step will be unavailable to us due to the absence of a necessary buildpack for Docker. However, the latest version of Liberica Native Image Kit is fully compatible with Apple Silicon, so we can move to the next step and build a native image with NIK.

如果我们使用苹果M1,由于没有Docker的必要构建包,这一步我们将无法使用。然而,最新版本的Liberica Native Image Kit与Apple Silicon完全兼容,所以我们可以进入下一个步骤,用NIK构建一个本地镜像。

6. Build a Native Image with Liberica NIK

6.用Liberica NIK建立一个本地图像

We’ll use Liberica Native Image Kit to build another version of the petclinic native image. Below we can find the steps for installing NIK for Linux. For macOS or Windows, let’s refer to the Liberica NIK Installation Guide.

我们将使用Liberica Native Image Kit来构建另一个版本的petclinic native image。下面我们可以找到Linux下安装NIK的步骤。对于macOS或Windows,让我们参考Liberica NIK安装指南

We first need to change to the directory in which we want to install:

我们首先需要改变到我们要安装的目录。

cd directory_path_name

Then we download Liberica NIK Core for our platform. It contains Liberica VM and GraalVM-based native image toolkit without additional languages, and so is a great tool to build Java native images.

然后我们下载适合我们平台的Liberica NIK Core。它包含了基于Liberica VM和GraalVM的原生图像工具包,不需要额外的语言,因此是一个构建Java原生图像的伟大工具。

In our case, we’ll get the version of NIK for Java 11 for Linux:

在我们的例子中,我们将得到NIK for Java 11 for Linux的版本。

wget https://download.bell-sw.com/vm/22.0.0.2/bellsoft-liberica-vm-openjdk11-22.0.0.2-linux-amd64.tar.gz

We then unpack the file by running:

然后,我们通过运行该文件进行解包。

tar -xzf bellsoft-liberica-vm-openjdk11-22.0.0.2-linux-amd64.tar.gz

Define the $JAVA_HOME variable by pointing at Liberica NIK:

定义$JAVA_HOME变量,指向Liberica NIK。

export JAVA_HOME=$(pwd)/bellsoft-liberica-vm-openjdk11-22.0.0.2

Now, we go to the petclinic JDBC application directory:

现在,我们进入petclinic JDBC应用程序目录。

cd $PROJECT_DIR

And we can create a native image by running the following command:

而我们可以通过运行以下命令创建一个本地镜像。

./mvnw -Pnative install

It involves the “native” profile for the build and results in the target/petclinic-jdbc binary file 102.3 MB in size.

它涉及构建的 “本地 “配置文件,导致target/petclinic-jdbc二进制文件大小为102.3 MB。

7. Compare Startup Time

7.比较启动时间

Now let’s test the speed of our application and images. We used an Intel(R) Core(TM) i7-8750H CPU PC with SSD for running them:

现在我们来测试一下我们的应用程序和图像的速度。我们使用英特尔(R)酷睿(TM)i7-8750H CPU电脑,并使用SSD来运行它们。

  • the JAR file starts in about 3.3 seconds
  • the first container we built starts in about 0.07 seconds
  • the native image made with NIK Core starts in 0.068 seconds.

8. Conclusion

8.结语

The Spring native images are built and work great even when the project is still in Beta. The startup time reduction is massive.

即使项目还在Beta阶段,Spring的原生图像也已经建立并运行良好。启动时间的减少是巨大的。

We can expect even better results when the Spring Native is released with the Liberica Native Image Kit used as the end-to-end solution for building native images.

当Spring Native发布时,我们可以期待更好的结果,Liberica Native Image Kit被用作构建本地图像的端到端解决方案。