qinfengge

qinfengge

醉后不知天在水,满船清梦压星河
github
email
telegram

spring boot 3 + graalVM native first experience

spring boot 3 has been out for a while, and the official website has also made GraalVM native a major feature of sp3.

So what is GraalVM?#

From the name, you can see that GraalVM is a virtual machine. Its main goal is to improve the performance of Java applications and consume fewer resources.

It adds JIT compiler and AOT to the Java HotSpot JVM to compile applications into native executable files. In addition to Java, GraalVM also supports multiple programming languages such as JavaScript, Ruby, Python, etc.

In simple terms, GraalVM allows Java or other languages to be compiled into binary programs using its interpreter. After being converted by the Spring Boot 3 framework, Java programs have faster startup speed, smaller memory footprint, and shorter time to achieve optimal performance.

Why do we need GraalVM?#

Why does Java attach so much importance to GraalVM?

As we all know, Java, as an "old" language, is gradually falling behind in terms of new technologies and trends. Although the JDK has been updated, it is still limited by historical reasons and cannot make a big turn.

Among them, cloud-native, serverless, and containerization are challenging Java. Although it has not reached the point of being completely defeated, this is the future trend and cannot be stopped.

Thoughts on serverless#

For serverless providers, smaller memory footprint and faster startup speed are undoubtedly the inevitable choices to reduce costs.

For consumers, it saves money on purchasing servers and maintenance, and supports pay-as-you-go billing, which is very attractive.

On the other hand, AI's large models are reshuffling the industry, but only large companies have the ability to train, deploy, and iterate, and only cloud service providers can provide large model capabilities. Therefore, serverless is almost the best choice for small and medium-sized companies to connect with AI.

Installing GraalVM#

Download and unzip it from the official website, and then configure the environment variables after completion.

You can use the official configuration method

# Note: Modify the address below to the address you unzipped locally
setx /M PATH "C:\Program Files\Java\graalvm-ce-java11-20.3.0\bin;%PATH%"
setx /M JAVA_HOME "C:\Program Files\Java\graalvm-ce-java11-20.3.0"

You can also configure the environment variables directly

Press win + i to enter settings---advanced system settings---environment variables, and add the following configurations

GRAALVM_HOME: The path you unzipped
JAVA_HOME: The path you unzipped

image

Then configure in the path

%GRAALVM_HOME%\bin
%JAVA_HOME%\bin

Use the java -version command in cmd to check the version. If there is the word GraalVM, it means successful.

image

Installing the compilation environment#

To compile Java into binaries through GraalVM, you need to install the C++ environment.

The following is the method of installing the relevant environment through Visual Studio, you can also refer to the official tutorial

If you are using Windows 11, do not use Visual Studio 2019 mentioned in the official tutorial, but use the latest version.

Install the following components

image

Creating a spring boot 3 project#

Create using IntelliJ IDEA

image

Choose GraalVM for JDK, and choose Java 17 or above for Java version

image

Choose a version of spring boot 3.0 or above, and add GraalVM Native Support and Web dependencies.

After creation, the Maven file should have the following plugins

image

After that, create a controller and write a simple interface

@RestController
@RequestMapping("user")
public class UserController {

    @GetMapping("test")
    public String test(){
        return "hello world";
    }

Compilation and building#

Now that the code is written, it's time to compile.

Open the Visual Studio Build Tools x64 Native Tools Command Prompt for VS 2022

image

Both x64 and x86 are fine. Then go to the project directory.

Use mvn -PnativeTest test to compile and test.

After there are no errors, use mvn native:build to build. After completion, an exe file will be generated in the project directory.

image

image

As you can see, the built file is still quite large. This is because some dependencies are packaged during the build process.

image

About 50M is the size of the code, which also includes a java.base.

30M is the size of the image heap, and the rest is other data.

When you start the exe file, you will find that the speed is extremely fast.

image

Current shortcomings#

GraalVM can be said to be a very interesting thing. It is a brave attempt by Java to cloud-native. It solves the major drawbacks of Java, but at the same time brings fatal problems.

In Java, dynamic invocation supports introducing various dependencies and pointing to them during invocation. However, GraalVM Native uses AOT (Ahead-of-Time Compilation) based on static code reachability analysis. In simple terms, Java does not know what you are doing during compilation, but it can find out when you call it. However, GraalVM needs to know who you are and what you are doing during compilation.

GraalVM sacrifices compilation time and optimizes startup time. This is correct, but it cannot perform dynamic invocations.

Although it provides methods to solve this problem using reflection, it is still very complex at the moment.

I tried to introduce the dependency of MyBatis, but the test compilation did not pass.

GraalVm Native Image Official Documentation

GraalVM Compiling Spring 3.0 (Cloud-Native Era)

Using Native Image in Spring Boot 3

Installing GraalVM and Building Executable EXE Files

[cl.exe missing when building native app using GraalVM]

Maven GraalVm native tests (mvn -PnativeTest test)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.