ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Spring Boot 如何构建并运行 GraalVM Native Image 应用?

Spring Boot 如何构建并运行 GraalVM Native Image 应用? Spring Boot 如何构建并运行 GraalVM Native Image 应用【免费下载链接】spring-bootSpring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.项目地址: https://gitcode.com/gh_mirrors/sp/spring-boot如果你有一个 Spring Boot Web 应用希望把它部署成 GraalVM Native Image 形式——一个不需要携带 JVM 的独立可执行文件或一个不含 JVM 的轻量容器镜像——这篇文章给出 Spring Boot 官方文档中的两条完整构建路径一条通过 Cloud Native Buildpacks 生成 Docker 镜像本地不需要安装 GraalVM另一条通过 GraalVM Native Build Tools 在本机生成原生可执行文件。两条路径都从同一个“Hello World”示例应用出发覆盖构建命令、运行方式和结果验证。准备示例应用与构建器要求文档使用的示例应用是一个最简单的 Spring MVC Web 应用它同时使用了 Spring MVC 和嵌入式 Tomcat这两者都已测试并确认可以在 GraalVM native image 中工作。主应用类如下来自 示例源码package org.springframework.boot.docs.howto.nativeimage.developingyourfirstapplication.sampleapplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; RestController SpringBootApplication public class MyApplication { RequestMapping(/) String home() { return Hello World!; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }文档同时建议新建 native 项目最简单的办法是到 start.spring.io添加GraalVM Native Support依赖后生成项目项目自带的HELP.md会给出入门提示。路径一用 Cloud Native Buildpacks 构建 native 容器镜像这是文档给出的主路径。Spring Boot 通过 Maven 和 Gradle 的 Cloud Native BuildpacksCNB集成配合 Paketo Java Native Image buildpack可以一条命令把应用构建成包含 native 可执行文件的镜像。镜像中不包含 JVMnative image 是静态编译的因此体积更小。两个硬性前提构建应用的 Java 版本至少为 JDK 25因为 Buildpacks 使用的 GraalVM native-image 版本与编译所用的 Java 版本一致本机安装并运行着 Docker 守护进程。系统与 Docker 检查在 Linux 上建议配置 Docker 允许非 root 用户访问。可以用下面的命令确认 Docker daemon 可正常访问不带sudodocker run hello-world文档给出的平台建议macOS 上建议将 Docker 分配的内存增加到至少8GB必要时增加 CPU 核数Windows 上建议启用 Docker 的 WSL 2 后端以获得更好性能。Maven 构建pom.xml需要满足两点使用spring-boot-starter-parent并声明org.graalvm.buildtools:native-maven-plugin。parent部分如下文档中该位置为{version-spring-boot}模板变量即你使用的 Spring Boot 版本本仓库 gradle.properties 中当前版本为4.2.0-SNAPSHOTparent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version4.2.0-SNAPSHOT/version /parentbuildplugins部分加入plugin groupIdorg.graalvm.buildtools/groupId artifactIdnative-maven-plugin/artifactId /pluginspring-boot-starter-parent声明了一个nativeprofile其中配置了生成 native image 所需的执行项executions用命令行-P标志激活。文档同时提示如果不使用spring-boot-starter-parent需要自行配置 Spring Boot 插件的process-aotgoal 和 Native Build Tools 插件的add-reachability-metadatagoal 的执行。构建镜像只需运行mvn -Pnative spring-boot:build-imageGradle 构建替代Gradle 侧的机制是Spring Boot Gradle 插件在应用了org.graalvm.buildtools.native插件后会自动配置 AOT 任务。确认你的构建脚本plugins块包含该插件后bootBuildImage任务会生成 native image 而不是 JVM 镜像gradle bootBuildImage运行容器并验证构建完成后本地 Docker 中应有一个可用镜像用docker run启动下面镜像名docker.io/library/myproject:0.0.1-SNAPSHOT是文档示例实际取决于你的项目坐标docker run --rm -p 8080:8080 docker.io/library/myproject:0.0.1-SNAPSHOT你应该看到与下面类似的输出文档示例启动时间随机器不同而变化但应远快于运行在 JVM 上的 Spring Boot 应用. ____ _ __ _ _ /\\ / ____ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | _ | _| | _ \/ _ | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) |____| .__|_| |_|_| |_\__, | / / / / |_||___//_/_/_/ :: Spring Boot :: (v4.2.0-SNAPSHOT) ....... . . . ....... . . . (log output here) ....... . . . ........ Started MyApplication in 0.08 seconds (process running for 0.095)再打开浏览器访问http://localhost:8080应看到Hello World!按ctrl-c可以优雅退出应用。补充说明 CNB 构建器文档指出默认使用的 builder 是paketobuildpacks/builder-noble-java-tiny:latest它体积小、攻击面小但不含 shell 且系统库精简。如果你需要在运行镜像中带更多工具可以把paketobuildpacks/ubuntu-noble-run:latest作为run镜像。路径二用 Native Build Tools 生成本地原生可执行文件如果不想经过 Docker而是直接得到本机可运行的原生可执行文件使用 GraalVM 官方为 Maven 和 Gradle 提供的 Native Build Tools 插件。与路径一不同这条路径要求本机安装 GraalVM 发行版含 native-image 编译器。安装 JDKLinux 和 macOS 上文档推荐使用 SDKMAN! 安装 Liberica GraalVM 发行版sdk install java 25.r25-nik sdk use java 25.r25-nik用java -version的输出确认版本已生效文档示例输出如下openjdk version 25 2025-09-16 LTS OpenJDK Runtime Environment Liberica-NIK-25.0.0-1 (build 2537-LTS) OpenJDK 64-Bit Server VM Liberica-NIK-25.0.0-1 (build 2537-LTS, mixed mode, sharing)Windows 上需要安装对应版本{version-graal}在本仓库解析为 25的 GraalVM 或 Liberica Native Image Kit、Visual Studio Build Tools 和 Windows SDK并且由于 Windows 命令行长度限制运行 Maven 或 Gradle 插件时必须使用 x64 Native Tools Command Prompt而不是普通命令行。构建Maven 项目的前提与路径一相同使用spring-boot-starter-parent以继承nativeprofile并声明org.graalvm.buildtools:native-maven-plugin。激活nativeprofile 后调用native:compilegoal 触发native-image编译mvn -Pnative native:compile生成的 native image 可执行文件位于target目录。Gradle 项目应用 Native Build Tools Gradle 插件后Spring Boot Gradle 插件会自动触发 Spring AOT 引擎任务依赖也已自动配置直接运行标准任务即可gradle nativeCompile生成的 native image 可执行文件位于build/native/nativeCompile目录。运行并验证直接运行生成的可执行文件Maven 项目为target/myprojectGradle 项目为build/native/nativeCompile/myproject文件名即你的应用名# Maven 构建产物 target/myproject # Gradle 构建产物 build/native/nativeCompile/myproject验证方式与路径一相同启动日志中应出现Started MyApplication in 0.08 seconds ...文档示例实际时间随机器变化浏览器访问http://localhost:8080应返回Hello World!按ctrl-c优雅退出。排查构建或运行问题native image 采用构建期静态分析文档明确了几个会直接影响部署的机制排查时应对照检查AOT 处理生成的产物。Spring AOT 引擎在构建期生成 Java 源码、字节码以及 GraalVM JSON 提示文件resource-config.json、reflect-config.json、serialization-config.json、proxy-config.json、jni-config.json位于META-INF/native-image/{groupId}/{artifactId}/。Maven 的生成源码在target/spring-aot/main/sourcesGradle 在build/generated/aotSources提示文件分别在target/spring-aot/main/resources和build/generated/aotResources。文档提示生成源码可读性不错调试应用时可以参考。嵌套配置属性必须加注解。反射提示通常由 AOT 引擎自动为配置属性生成但非内部类的嵌套配置属性必须标注NestedConfigurationProperty否则在 native image 中不可绑定。构造函数绑定时注解加在字段上record 和 Kotlin data class 则加在参数上所有情况下都要使用 public getter/setter否则属性不可绑定。缺少提示时用 Tracing Agent。Spring 会自动生成大部分反射、资源、代理提示但某个库或模式没被识别时GraalVM 的 native image tracing agent 可以快速定位缺失项。文档给出的做法是带 agent 直接启动应用并实际触发代码路径java -Dspring.aot.enabledtrue \ -agentlib:native-image-agentconfig-output-dir/path/to/config-dir/ \ -jar target/myproject-0.0.1-SNAPSHOT.jar其中/path/to/config-dir/需要替换为你指定的提示输出目录。触发完目标代码路径后按ctrl-c停止应用agent 会在关闭时把提示文件写到该目录。你可以手动检查这些文件也可以把它们复制到src/main/resources/META-INF/native-image/目录作为下次 native image 构建的输入。另一种做法是运行应用测试来触发但默认生成的提示会包含测试框架自身所需的条目agent 支持 access-filter 文件把这部分排除。已知限制。并非所有第三方库都支持 native imageSpring 本身不为第三方库携带提示而是依赖 GraalVM 社区的 reachability metadata 项目。文档同时列出 AOT 的闭世界限制应用的 Bean 不能随运行环境变化Profile与按 profile 区分的配置有限制依赖“某 Bean 是否创建”来改变行为的属性例如ConditionalOnProperty、.enabled属性不受支持。类路径在构建期固定、不可变更且没有懒加载可执行文件中的一切都会在启动时载入内存。可选分支把已构建的可执行 jar 转换成 native image如果你的 CI/CD 沿用 JVM 流水线或者需要一份 OS 中立的部署产物native-image不支持交叉编译可以在 AOT 处理过的 Spring Boot 可执行 jar 基础上做转换。前提是jar 中必须包含 AOT 生成的资产生成的类、JSON 提示文件等。用pack转换时无需本地 GraalVM。文档示例假设 AOT 处理后的 jarmyproject-0.0.1-SNAPSHOT.jar位于target目录pack build --builder paketobuildpacks/builder-noble-java-tiny \ --path target/myproject-0.0.1-SNAPSHOT.jar \ --env BP_NATIVE_IMAGEtrue \ my-application:0.0.1-SNAPSHOT完成后同样用docker run --rm -p 8080:8080 ...启动验证。用 GraalVM 自带的native-image工具转换这些命令适用于 Linux 或 macOSWindows 需要自行调整第一条命令会删除target/native目录请确认该目录内没有需要保留的内容并在项目根目录执行rm -rf target/native mkdir -p target/native cd target/native jar -xvf ../myproject-0.0.1-SNAPSHOT.jar native-image -H:Namemyproject META-INF/native-image/argfile -cp .:BOOT-INF/classes:find BOOT-INF/lib | tr \n : mv myproject ../两个注意事项META-INF/native-image/argfile只有在确实需要 reachability metadata 覆盖时才会打进 jar可能不存在native-image的-cp参数不接受通配符所以上面的命令用find和tr把所有 jar 逐一列出来。进一步阅读Developing Your First GraalVM Native Application本文两条构建路径的完整 how-to 原文。GraalVM Native Images 参考 与 Introducing GraalVM Native Imagesnative image 与 JVM 部署的关键差异、AOT 处理原理。Advanced Native Images TopicsTracing Agent、自定义提示RuntimeHintsRegistrar、静态 JSON 提示文件与已知限制的详细说明。【免费下载链接】spring-bootSpring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.项目地址: https://gitcode.com/gh_mirrors/sp/spring-boot创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表