Spring boot 3 && GraalVM Native Image
什么是 GraalVM?
GraalVM is a high-performance JDK designed to accelerate the execution of applications written in Java and other JVM languages while also providing runtimes for JavaScript, Python, and a number of other popular languages. GraalVM offers two ways to run Java applications: on the HotSpot JVM with Graal just-in-time (JIT) compiler or as an ahead-of-time (AOT) compiled native executable. GraalVM’s polyglot capabilities make it possible to mix multiple programming languages in a single application while eliminating foreign language call costs.
GraalVM是一个高性能的JDK,旨在加速
用Java和其他JVM语言编写的应用程序的执行
,同时也为JavaScript、Python和许多其他流行语言提供运行时。GraalVM提供了两种运行Java应用程序的方法:在HotSpot JVM上使用Graal实时(JIT)编译器
或作为提前(AOT)编译的本地可执行文件
。GraalVM的多语言功能使得在一个应用程序中混合多种编程语言成为可能,同时消除了外部调用成本。
GraalVM 架构
什么是 GraalVM Native Image?
GraalVM Native Images是独立的可执行文件
,可以通过提前处理编译的Java应用程序来生成。本机映像通常比JVM映像占用的内存更少,启动速度更快。真的很快。
Spring boot 3 & GraalVM Native Image
Windows 版本
Windows 10 22H2
下载需要的包,否则后面 java maven 编译会报错
通过微软WDK网址下载相关的软件
-
visual studio
visual studio 2019 版本 (估计2022也可以)
工作负荷-通用 Windows 平台开发 -
安装 WDK
适用于 Windows 10 版本 2004 的 WD
以上2个耗时较长。
- 安装docker desktop
环境变量 一定要设置,否则使用mvn native:compile
编译时可能出现 cl.exe
找不到, 也会报 fatal error C1083: 无法打开包括文件: “stdio.h ”: No such file or directory]
MSVC_HOME=C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133
WK10_BIN_HOME=C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0
WK10_INCLUDE_HOME=C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0
WK10_LIB_HOME=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0
INCLUDE=%WK10_INCLUDE_HOME%\ucrt;%WK10_INCLUDE_HOME%\um;%WK10_INCLUDE_HOME%\shared;%MSVC_HOME%\include;
LIB=%WK10_LIB_HOME%\um\x64;%WK10_LIB_HOME%\ucrt\x64;%MSVC_HOME%\lib\x64;
#此处追加
Path=%MSVC_HOME%\bin\HostX64\x64;%WK10_BIN_HOME%\x64
创建一个springboot 工程
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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>pr.iceworld.fernando</groupId>
<artifactId>spring-boot3-graavm-image</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- repository -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
<profiles>
<profile>
<id>native</id>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>pr.iceworld.fernando.springboot3.image.MainApplication</mainClass>
<image>
<!-- 如果不指定,默认builder 是 tiny 版本,无法使用 docker run 运行 -->
<builder>paketobuildpacks/builder:base</builder>
</image>
</configuration>
</plugin>
</plugins>
</build>
</project>
@RestController
//@SpringBootApplication
//@SpringBootConfiguration
@EnableAutoConfiguration
//@ComponentScan
public class MainApplication {
@RequestMapping("/")
String home() {
return "Hello It's me!";
}
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
编译工程并打包
mvn -Pnative native:compile
spring-boot3-graavm-image.exe
可以直接执行,且加载很快,很快
···
2022-12-12T23:51:15.726+08:00 INFO 1224 — [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 69 ms
2022-12-12T23:51:15.750+08:00 INFO 1224 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘’
2022-12-12T23:51:15.750+08:00 INFO 1224 — [ main] p.i.f.springboot3.image.MainApplication : Started MainApplication in 0.105 seconds (process running for 0.111)
···
C:\Users\ferna>curl localhost:8080
Hello It's me!
打包一个docker 镜像
mvn -Pnative spring-boot:build-image
REPOSITORY TAG IMAGE ID CREATED SIZE
paketobuildpacks/run base-cnb 3e3acccbaa17 4 days ago 88.8MB
paketobuildpacks/run tiny-cnb 6cfe3de75423 4 days ago 17.3MB
spring-boot3-graavm-image 3.0.0 9b7b85e90f52 42 years ago 167MB
paketobuildpacks/builder base 0593f8ae856c 42 years ago 1.35GB
paketobuildpacks/builder <none> 215b9051b4b1 42 years ago 597MB
paketobuildpacks/builder tiny 7841a1c811a3 42 years ago 590MB
docker run --rm -p 8080:8080 spring-boot3-graavm-image:3.0.0
C:\Users\ferna>curl localhost:8080
Hello It's me!
Linux 版本
TODO