Build Spring Boot 3.0 native executables with GraalVM

Introduction to GraalVM

Since it is a VM, it must also be a virtual machine, so does it have anything to do with the JVM? There is a certain relationship, GraalVM can completely replace the virtual machines mentioned above, such as HotSpot. Translate your code that was running on HotSpot to GraalVM directly, without making any changes, and you don’t even feel it, and the project can run perfectly. But GraalVM has broader uses, supporting not only the Java language, but other languages ​​as well. These other languages ​​include not only direct JVM languages, such as Kotlin and Scala, but also JavaScript, Nodejs, Ruby, Python, etc., as shown in the figure.
insert image description here

Introduction to GraalVM Native Image

GraalVM Native Image is a technology provided by GraalVM that can package Spring Boot programs into cloud-native executable files. It occupies less memory and has a faster startup speed than JVM. It is very suitable for container deployment and use on the Faas platform.
Unlike applications running on the JVM, GraalVM Native Image needs to compile the code in advance to create an executable file. The operation of GraalVM Native Image does not need to provide a JVM virtual machine.

GraalVM document address: https://www.graalvm.org/latest/docs/getting-started/
GraalVM Native Image document address: https://www.graalvm.org/latest/reference-manual/native-image/

Create your first GraalVM cloud-native application

There are two ways to create native applications:

  • Use Cloud Native Buildpacks to generate a lightweight container containing an executable application
  • Generate an executable using the GraalVM Native build tools

The example below is built using GraalVM Native.

Environmental preparation

Install the GraalVM SDK

Compression package installation

Download the corresponding version software:
https://github.com/graalvm/graalvm-ce-builds/releases

  • Windows
  1. Unzip the ZIP package to the installation directory
  2. Configure paththe path to the GraalVM bindirectory

setx /M PATH “C:\Progra~1\Java<graalvm>\bin;%PATH%”

  1. Configured JAVA_HOMEto the installation directory of GraalVM

setx /M JAVA_HOME “C:\Progra~1\Java<graalvm>”

  1. reboot, test

insert image description here

  • Linux
  1. Unzip the ZIP package to the specified directory

tar -xzf graalvm-ce-java-linux--.tar.gz

  1. configuration PATHpath

export PATH=/path/to//bin:$PATH

  1. configuration JAVA_HOMEpath

export JAVA_HOME=/path/to/

  1. test
  • MAC
  1. Extract the ZIP package

tar -xzf graalvm-ce-java-darwin-amd64-.tar.gz

If you are using a later version of macOS Catalina, you may need to execute the following command:
sudo xattr -r -d com.apple.quarantine /path/to/graalvm

  1. Move the unpacked package to/Library/Java/JavaVirtualMachines

sudo mv graalvm-ce-java- /Library/Java/JavaVirtualMachines

Verify success: /usr/libexec/java_home -V will get an installed JDK directory

  1. configuration PATHpath

export PATH=/Library/Java/JavaVirtualMachines//Contents/Home/bin:$PATH

  1. configuration JAVA_HOMEpath

export JAVA_HOME=/Library/Java/JavaVirtualMachines//Contents/Home

IDEA installation

Just use the built-in functions of IDEA, the download is a bit slow, here IDEA only has a version based on Java 19

insert image description here

After downloading with IDEA, you can only run the application inside IDEA. If you want to use maven to package, you also need configuration PATHand JAVA_HOMEpath, which is the same as the compressed package installation method.

Install the Native Image tool

If the tool is not installed, maven will automatically download it when packaging, but it is recommended to install the packaging tool in advance

gu install native-image

insert image description here

Install the local environment that Native Image depends on

Because it needs to be compiled into a specified local executable file, such as exe, Microsoft Visual C++ ( MSVC) needs to be installed on Windows,
xcode needs to be installed on MAC, through xcode-select --installLinux
Ubuntu sudo yum install gcc glibc-devel zlib-devel
other sudo apt-get install build-essential libz-dev zlib1g-dev
Linuxsudo dnf install gcc glibc-devel zlib-devel libstdc++-static

Taking Windows as an example here, install Visual Studio 2017 or later build tools andWindows 10 SDK

Create a Spring Boot 3.0 application using start.spring.io

1. Select Java 17 version
2. Select GraalVM Native Support, Spring Web

insert image description here

Created pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>graalvm-native-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>graalvm-native-application</name>
    <description>graalvm-native-application</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3. Write a simple interface

package com.example.graalvmnativeapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class GraalvmNativeApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(GraalvmNativeApplication.class, args);
    }

    @RequestMapping("/")
    String home() {
    
    
        return "Hello World!";
    }

}

4. Package the executable file
Find the x64 Native Tools Command Prompt in the VS installation and execute the following command

mvn -Pnative native:compile

There are a total of 7 steps, and it took about 2 minutes to package the package. The generated executable file is in the targetdirectory

insert image description here
5. Run the executable file
Double-click the exe file, and the Spring Boot application will start almost instantly. The file size is 68M, which is really too big for a demo with no business code.

insert image description here

The access address http://localhost:8080/can be accessed normally.

insert image description here
6. Comparison with Java 17

VM packet size Start Time
GraalVM Native Image 68M 0.15s
Java 17 18M 2.15s
Java 8 16.5M 3.5s

It can be seen from this DEMO that the startup time of using GraalVM Spring Boot is indeed much happier, but at the same time the package is also much larger, which means that space is exchanged for time. If you want to package native executable files, the environment configuration is also cumbersome. However, it is worth trying to use GraalVM to replace JVM to run Java programs.

References:

  1. https://www.graalvm.org/latest/docs/getting-started/windows/ (the use of GraalVM on Windows)
  2. https://blog.csdn.net/q412086027/article/details/113878426 (inspired me)
  3. https://medium.com/graalvm/using-graalvm-and-native-image-on-windows-10-9954dc071311 (Clearer Windows configuration steps)

Other articles by the author:
Grafana series articles, version: OOS v9.3.1 (updating)

  1. Introduction and installation of Grafana
  2. Introduction to configuration parameters of Grafana monitoring large screen (1)
  3. Introduction to configuration parameters of Grafana monitoring large screen (2)
  4. Grafana monitors large-screen visualization charts

Spring Boot Admin 2 series of articles:

  1. Spring Boot Admin Reference Guide
  2. The problem that the SpringBoot Admin service is offline and does not display health information
  3. Loading of Spring Boot Admin2 @EnableAdminServer
  4. Detailed Explanation of Spring Boot Admin2 AdminServerAutoConfiguration
  5. Detailed Explanation of Spring Boot Admin2 Instance Status Monitoring
  6. Spring Boot Admin2 custom JVM monitoring notification
  7. Spring Boot Admin2 custom exception monitoring
  8. Spring Boot Admin monitoring indicators connected to Grafana visualization

Guess you like

Origin blog.csdn.net/weixin_40972073/article/details/128350601