Linux installe les dépendances sous-jacentes liées aux outils
yum install -y gcc glibc-devel zlib-devel
Installer le JDK GraalVM
"Téléchargement du site officiel de GraalVM"
pour trouver le plus procheGraalVM Community Edition XXXCliquez sur Assets (parce que le mien est un projet SpringBoot3, le JDK initial en nécessite 17, donc j'en télécharge 17) Si la vitesse de téléchargement est lente, vous pouvez utiliser un outil de téléchargement tiers, tel que : Thunder, etc. Sélectionnez les 2 contenus suivants télécharger
_
graalvm-ce-java17-linux-amd64-XXXtar.gz
native-image-installable-svm-java17-linux-amd64-XXXjar
Configurez la variable d'environnement JDK comme variable d'environnement de GraalVM, car GraalVM est JDK
S'il existe déjà une variable d'environnement Java auparavant, vous devez la remplacer par GraalVM
et télécharger le fichierAllez dans le répertoire /usr/local/java/ pour décompresser 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
Configurer les variables d'environnement
vim /etc/profile
Trouvez export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL, écrivez ci-dessous :
#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 n'a pas de répertoire jre/bin, mais pour faciliter le passage à la version 8, conservez-le. Si vous souhaitez passer à java, il vous suffit de modifier la variable d'environnement JAVA_HOME pour qu'elle soit votre répertoire d'installation java8.
:wq enregistrer et quitter pour que la variable d'environnement prenne effet
source /etc/profile && java -version
sortir
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)
Installer GraalVM Native-Image
Paquetimage-native-installable-svm-java17-linux-amd64-XXXjarTéléchargez dans le répertoire /usr/local/java/ pour l'installation
gu install --file native-image-installable-svm-java17-linux-amd64-22.3.1.jar
Entrez native-image --help pour voir si la console affiche les commandes d'aide pertinentes. Si c'est le cas, l'installation a réussi.
native-image --help
SpringBoot3
Ajoutez le projet Spring Boot3 et sélectionnez JDK comme 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>
Construire une classe de démarrage HelloController.java et SpringBootApplication
@RestController
public class HelloController {
@GetMapping("hello")
public String hello() {
return "Hello World.";
}
}
Mettez SpringBoot3 dans un package jar et téléchargez-le sur le serveur Linux
native-image -jar springboot3-native-demo.jar
Il peut y avoir des avertissements ou des erreurs pendant le processus en cours d'exécution, mais le programme peut s'exécuter normalement, car ce qui est tapé en binaire (assemblage) doit être un type défini et immuable, tandis que la réflexion, les génériques, etc. sont des types dynamiques. run Si le type change pendant le processus, le programme générera une erreur et un avertissement sera émis.
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).
L'empaquetage générera 2 fichiers, le premier est le fichier exécutable binaire, le second est l'artefact de construction (peut être supprimé) et le troisième est le fichier jar (ne peut pas être supprimé)。
Démarrez le fichier exécutable binaire springboot3-native-demo sous /data/graalvm/
PS : parce que j'ai téléchargé le package jar dans le répertoire /data/graalvm/
/data/graalvm/springboot3-native-demo
sortir
[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
Accédez à l'ip:8080/hello du serveur Linux et produisez avec succès Hello World.
Commande de démarrage en arrière-plan
nohup /data/graalvm/springboot3-native-demo >/dev/null 2>&1 &
Vérifiez si le projet est démarré en arrière-plan 8080 est le numéro de port du projet
netstat -ntlp | grep 8080