Windows GraalVM builds Spring Boot 3.0 native executables

graalvmofficial documentation

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/

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, GraalVMwhich can completely replace the above-mentioned virtual machines, for example HotSpot. Translate the code you previously HotSpotran GraalVMon to directly without making any changes, and you don’t even feel it, and the project can run perfectly. But GraalVMthere are broader uses as well, supporting not only Javathe language but other languages ​​as well. These other languages ​​include not only direct JVMlineage languages, such as Kotlin, Scala, but also such JavaScript、Nodejs、Ruby、Python as shown in the figure.
Insert image here
Image from: https://www.graalvm.org/latest/docs/introduction/
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.

Create your first GraalVM cloud-native application

There are two ways to create native applications:

useGraalVM NativeThe build tool generates an executable
usingCloud Native Buildpacksto generate a lightweight container containing an executable application

The following example usesGraalVM Nativeto build,The java version is 17

windows install GraalVM SDK

Download address: https://github.com/graalvm/graalvm-ce-builds/releases/tag/vm-22.3.1

insert image description here
After the download is complete, unzip it.

Configure environment variables

insert image description here
The variable value is the installation directory of graalvm
insert image description here
. Verify that the installation is successful.

java -version
D:\IdeaProjects\mytools\chatgpt>java -version
openjdk version "17.0.6" 2023-01-17
OpenJDK Runtime Environment GraalVM CE 22.3.1 (build 17.0.6+10-jvmci-22.3-b13)
OpenJDK 64-Bit Server VM GraalVM CE 22.3.1 (build 17.0.6+10-jvmci-22.3-b13, mixed mode, sharing)

D:\IdeaProjects\mytools\chatgpt>

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

Since online installation is slow, offline installation can be used.

  1. First go to github to download native-image, download address: https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-22.3.1/native-image-installable-svm-java17-windows-amd64 -22.3.1.jar

insert image description here
2. Install command

gu install -L  C:\Users\zhubayi\Downloads\native-image-installable-svm-java17-linux-amd64-22.3.1.jar

注意:-L is followed by native-imagethe path of the file

3. Check whether the installation is successful

gu list 
C:\Users\zhubayi>gu list
ComponentId              Version             Component name                Stability                     Origin
---------------------------------------------------------------------------------------------------------------------------------
graalvm                  22.3.1              GraalVM Core                  Supported
native-image             22.3.1              Native Image                  Early adopter                 github.com

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

Take Windows as an example here. To install Visual Studio 2022 or later build tools and Windows 10 SDK,
please refer to my blog: https://blog.csdn.net/qq_44732146/article/details/128877510

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
insert image description here
pom.xml file

<?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.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhubayi</groupId>
    <artifactId>graalvm-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>graalvm-demo</name>
    <description>graalvm-demo</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <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 controller

@RestController
public class TestController {
    
    
    @RequestMapping("/")
    String home() {
    
    
        return "Hello World!";
    }
}

4. Package the executable file
and find it in the installation of VSx64 Native Tools Command PromptorterminalExecute the following command

mvn -Pnative native:compile

insert image description here
insert image description here
There are a total of 7 steps, and it took about 2 minutes to package the package. The generated executable file is intargetTable of contents
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 about 68M. For a demo with no business code, it is indeed too big, but the startup speed is very fast! .
insert image description here

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

insert image description here
From this example, it can be seen that GraalVM Spring Bootthe startup time of using is indeed much faster, 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.

Guess you like

Origin blog.csdn.net/qq_44732146/article/details/129631186