Linux installs underlying tool-related dependencies
yum install -y gcc glibc-devel zlib-devel
Install GraalVM JDK
"GraalVM official website download"
to find the nearestGraalVM Community Edition X.X.XClick Assets (because mine is a SpringBoot3 project, the initial JDK requires 17, so I download 17) If the download speed is slow, you can use a third-party download tool, such as: Thunder, etc. Select the following 2 content to
download
graalvm-ce-java17-linux-amd64-X.X.X.tar.gz
native-image-installable-svm-java17-linux-amd64-X.X.X.jar
Configure the JDK environment variable as the environment variable of GraalVM, because GraalVM is JDK
If there is already a Java environment variable before, you need to replace it with GraalVM
and upload the fileGo to the /usr/local/java/ directory to decompress graalvm-ce-java17-linux-amd64-XXXtar.gz(GraalVM JDK)
mkdir -p /usr/local/java
cd /usr/local/java
tar -zxvf graalvm-ce-java17-linux-amd64-22.3.1.tar.gz
Configure environment variables
vim /etc/profile
Find export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL, write below:
#java
export JAVA_HOME=/usr/local/java/graalvm-ce-java17-22.3.1
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
Java17 does not have a jre/bin directory, but for the convenience of switching to version 8, keep it. If you want to switch to java, you only need to modify the environment variable JAVA_HOME to be your java8 installation directory.
:wq save and exit to make the environment variable take effect
source /etc/profile && java -version
output
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)
Install GraalVM Native-Image
Bundlenative-image-installable-svm-java17-linux-amd64-X.X.X.jarUpload to the /usr/local/java/ directory for installation
gu install --file native-image-installable-svm-java17-linux-amd64-22.3.1.jar
Enter native-image --help to see if the console outputs relevant help commands. If so, the installation is successful.
native-image --help
SpringBoot3
Add spring boot3 project and select JDK as GraalVM JDK17
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fu</groupId>
<artifactId>spring-boot3-aot-graalvm-native-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 没有跟demo-maven作为父子项目,而是以SpringBoot作为父项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- AOT Graalvm native maven 插件 -->
<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>
Build a HelloController.java and SpringBootApplication startup class
@RestController
public class HelloController {
@GetMapping("hello")
public String hello() {
return "Hello World.";
}
}
Put SpringBoot3 into a jar package and upload it to the linux server
native-image -jar springboot3-native-demo.jar
There may be some warnings or errors during the running process, but the program can run normally, because the binary (assembly) requires a definite and immutable type, while reflection, generics, etc. are dynamic types. If you run If the type changes during the process, the program will error out, so a warning will be issued.
GraalVM Native Image: Generating 'springboot3-native-demo' (executable)...
========================================================================================================================
[1/7] Initializing... (4.1s @ 0.18GB)
Version info: 'GraalVM 22.3.1 Java 17 CE'
Java version info: '17.0.6+10-jvmci-22.3-b13'
C compiler: gcc (redhat, x86_64, 4.8.5)
Garbage collector: Serial GC
[2/7] Performing analysis... [******] (19.3s @ 0.61GB)
3,587 (76.86%) of 4,667 classes reachable
4,509 (52.77%) of 8,545 fields reachable
16,135 (45.95%) of 35,113 methods reachable
160 classes, 0 fields, and 539 methods registered for reflection
59 classes, 59 fields, and 52 methods registered for JNI access
4 native libraries: dl, pthread, rt, z
[3/7] Building universe... (2.3s @ 0.49GB)
Warning: Reflection method java.lang.Class.forName invoked at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
Warning: Reflection method java.lang.Class.getDeclaredMethod invoked at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
Warning: Reflection method java.lang.Class.getDeclaredConstructor invoked at org.springframework.boot.loader.jar.Handler.getFallbackHandler(Handler.java:200)
Warning: Aborting stand-alone image build due to reflection use without configuration.
Warning: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
------------------------------------------------------------------------------------------------------------------------
0.6s (2.4% of total time) in 23 GCs | Peak RSS: 1.07GB | CPU load: 3.41
========================================================================================================================
Failed generating 'springboot3-native-demo' after 25.8s.
Generating fallback image...
Warning: Image 'springboot3-native-demo' is a fallback image that requires a JDK for execution (use --no-fallback to suppress fallback image generatformation why a fallback image was necessary).
Packaging will generate 2 files, the first is the binary executable file, the second is the build artifact (can be deleted), and the third is the jar file (cannot be deleted)。
Start the springboot3-native-demo binary executable file under /data/graalvm/
PS: Because I uploaded the jar package to the /data/graalvm/ directory
/data/graalvm/springboot3-native-demo
output
[root@node1 graalvm]# ./spring-boot3-aot-graalvm-native-demo-1.0-SNAPSHOT
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.5)
2023-07-02T11:18:20.990-04:00 INFO 2500 --- [ main] c.f.s.SpringBoot3AotGraalvmNativeDemo : Starting SpringBoot3AotGraalvmNativeDemo v1.0-SNAPSHOT using Java 17.0.6 with PID 2500 (/data/graalvm/spring-boot3-aot-graalvm-native-demo-1.0-SNAPSHOT.jar started by root in /data/graalvm)
2023-07-02T11:18:20.993-04:00 INFO 2500 --- [ main] c.f.s.SpringBoot3AotGraalvmNativeDemo : No active profile set, falling back to 1 default profile: "default"
2023-07-02T11:18:21.869-04:00 INFO 2500 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-07-02T11:18:21.879-04:00 INFO 2500 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-07-02T11:18:21.879-04:00 INFO 2500 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.7]
2023-07-02T11:18:21.951-04:00 INFO 2500 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-07-02T11:18:21.953-04:00 INFO 2500 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 889 ms
2023-07-02T11:18:22.306-04:00 INFO 2500 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-07-02T11:18:22.320-04:00 INFO 2500 --- [ main] c.f.s.SpringBoot3AotGraalvmNativeDemo : Started SpringBoot3AotGraalvmNativeDemo in 1.803 seconds (process running for 2.287)
2023-07-02T11:18:43.548-04:00 INFO 2500 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-07-02T11:18:43.549-04:00 INFO 2500 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-07-02T11:18:43.550-04:00 INFO 2500 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Access the ip:8080/hello of the linux server and successfully output Hello World.
Background start command
nohup /data/graalvm/springboot3-native-demo >/dev/null 2>&1 &
Check whether the project is started in the background 8080 is the port number of the project
netstat -ntlp | grep 8080