Windows下使用Graalvm将Springboot应用编译成exe大大提高启动和运行效率
source link: https://www.cnblogs.com/antake/p/17081592.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
GraalVM安装
GraalVM安装
请前往GraalVM官网 下载 GraalVM Community 22.3,注意当前支持的Springboot的GraalVM版本必须是22.3
笔者这里下载的Java版本为17,如下图
安装GraalVM的方法与安装JAVA方法大致相同,GraalVM中包括了JDK(无需再次安装JDK),同时还需要安装native-image工具(后续下载)
配置环境变量
使用命令查看是否安装graal vm成功
native-image工具安装
这里下载的文件(同GraalVM下载位置下方的release文件)为native-image-installable-svm-java17-windows-amd64-22.3.0.jar如下图所示
下载完成之后在文件目录运行以下命令进行安装
Visual Studio安装
编译native image必须依赖VC++的编译环境,这里必须使用微软官方的vc++编译工具Visual Studio,笔者这里使用的最新版本2022Visual Studi官网
To use Native Image on Windows, install Visual Studio and Microsoft Visual C++ (MSVC). There are two installation options: * Install the Visual Studio Build Tools with the Windows 10 SDK * Install Visual Studio with the Windows 10 SDK
You can use Visual Studio 2017 version 15.9 or later.
The `native-image` builder will only work when it is run from the **x64 Native Tools Command Prompt**. The command for initiating an x64 Native Tools command prompt varies according to whether you only have the Visual Studio Build Tools installed or if you have the full Visual Studio 2019 installed.
从上可知,Visual Studio 2017 version 15.9 or later均可
打开Visual Studio Installer,勾选如下图中三个组件
安装完成后,需要配置VC++的编译环境变量PATH,INCLUDE,LIB
LIB=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\ucrt\x64;D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.16.27023\lib\x64
INCLUDE=C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\ucrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.20348.0\shared;D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.16.27023\include
PATH=D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64
Springboot应用初体验
调试运行,正常启动
如果编译都不行,用插件编译之前现在console执行以下命令
jar(JVM)本地exe(native image)文件大小18M69M启动耗时1.183s0.098s内存占用106M46m
根据上述对比结果,尽管是一个最简单的springboot程序,但在启动耗时上native具有明显的优势,达到毫秒级,占用内存少了一半多。
由于是一项新技术,其推广和普及肯定必定需要一段时间(毕竟我们还在JDK1.8,哈哈),但在一些新的小型应用中,完全可以试试手,毕竟尽量native用不了,还有jvm可以兜底,我们并需要为此做额外的工作。
最近,希望java web能通过native的特性,真正的适配云原生环境。
__EOF__
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK