本站资源收集于互联网,不提供软件存储服务,每天免费更新优质的软件以及学习资源!

使用GraalVM构建器从SpringBoot应用程序构建本机映像

网络教程 app 1℃

使用GraalVM构建器从SpringBoot应用程序构建本机映像

概述

本节介绍如何使用 graalvm 的本机映像生成器从 spring boot 应用程序创建本机映像,以及如何在 docker 容器中运行此本机映像。

客观的

在软件架构和微服务架构设计中,我们必须考虑应用程序的可扩展性、性能。每当应用程序中的请求数量增加时,我们的应用程序应该开始快速扩展并有效地利用资源。

我正在考虑使用 spring boot 提前 (aot) 编译与 graalvm 以及 java 虚拟线程(在 jdk 21 及更高版本中可用)在容器中运行可执行文件。

aot 编译对于快速启动时间和可预测性能很重要的场景是有利的,但代价是运行时适应性较差。与虚拟机 (vm) 相比,容器是轻量级的,使用的资源更少,因为它们共享主机操作系统内核。容器的启动和停止速度比虚拟机快得多,从而实现更快的扩展和部署。虚拟线程可以提高处理大量并发任务的应用程序的性能。这对于 web 服务器、数据库和其他 i/o 密集型系统等应用程序尤其有利。虚拟线程比传统线程使用更少的资源。它们由运行时以最小化内存使用和 cpu 开销的方式进行管理。

在这个架构设计决策中,我们获得了好处,但也必须考虑以下实施挑战和设计注意事项:

虚拟线程:如果我们的业务逻辑是cpu密集型的,比如需要大量内存计算的场景,我们应该避免使用虚拟线程。提前 (aot) 编译:aot 编译器可能无法正确处理反射、代理编码或序列化。此外,graalvm 是一项相对较新的技术,给从 spring boot 应用程序创建本机映像带来了挑战,并导致构建时间增加。容器:容器提供了许多好处,但也带来了一些与安全、网络、性能、ci/cd 等领域相关的挑战。一些示例是 容器可能包含来自基础镜像或依赖项的漏洞。 将容器集成到现有的 ci/cd 管道中可能具有挑战性,需要更改构建、测试和部署流程。管理 kubernetes 等容器编排平台可能很复杂,并且需要专业知识。有效地扩展和缩小容器以处理不同的负载,而不会过度配置或配置不足的资源。

spring boot 应用程序
为了测试这个用例,我正在构建一个 spring boot 应用程序,该应用程序在“/hello”处公开 rest 端点。我正在使用以下配置、库和工具:

带有 rest 的 spring boot 3.2.8 spring boot aot 编译spring boot graalvm 原生镜像maven 3.9.8 构建工具java 22

我们需要在pom xml文件中添加以下配置。

spring boot 属性配置

<properties> <java.version>22</java.version> <spring-native.version>0.12.1</spring-native.version></properties>

spring boot aot 插件配置

<plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <executions> <execution> <id>process-aot</id> <goals> <goal>process-aot</goal> </goals> </execution> </executions></plugin>

graalvm 插件配置

<plugin> <groupid>org.graalvm.buildtools</groupid> <artifactid>native-maven-plugin</artifactid> <configuration> <imagename>app-native-binary</imagename> <metadatarepository> <enabled>true</enabled> </metadatarepository> <buildargs> <buildarg>–static –libc=musl</buildarg> <buildarg>-h:+reportexceptionstacktraces</buildarg> </buildargs> <mainclass>.developerhelperhub.tutorial.springboot.tutorial.tutorialstartupperformanceapplication</mainclass> </configuration> <executions> <execution> <id>add-reachability-metadata</id> <goals> <goal>add-reachability-metadata</goal> </goals> </execution> </executions></plugin>

“mainclass”:配置spring boot应用程序的邮件类”imagename”: 配置原生镜像名称 “buildargs”:配置 —libc=”msul”,我们正在配置 graalvm 以使用“libc musl”兼容库构建本机映像,因为我们将在 alpine linux 机器上运行此映像。与其他标准库相比,musl 的设计更小,使用的内存更少,非常适合资源受限的环境。构建二进制文件并创建 docker 镜像

我们需要为特定的操作系统主机和cpu架构构建原生镜像,原生镜像将在容器中运行。

我们使用 alpine linux 来在容器中运行我们的应用程序,因为它体积小、简单且安全。为了实现这一点,我们需要使用适当的 graalvm 配置来构建我们的应用程序。 alpine 的系统要求是操作系统和 cpu 架构。

“架构”:“amd64”“os”:“linux”c 通用库:“libc musl”

以下命令我们可以用来检查“amd64/alpine”图像

docker pull amd64/alpine # pull the imagedocker image inspect amd64/alpine # inspect the image

我们可以使用 docker 容器来构建原生镜像,而不用在本地设置 graalvm 和 java 相关配置。我正在使用 “ghcr.io/graalvm/native-image-munity:22-muslib” docker 映像来构建本机。

以下命令我们可以用来检查“ghcr.io/graalvm/native-image-munity:22-muslib”图像

docker pull ghcr.io/graalvm/native-image-munity:22-muslib # pull the imagedocker image inspect ghcr.io/graalvm/native-image-munity:22-muslib # inspect the image

我正在创建一个构建映像来测试和调试容器,确保所有配置和服务都正确安装。这种方法将帮助我们快速识别并解决任何问题。

在docker文件中添加以下步骤,文件名为“dockerfilebuild”

from ghcr.io/graalvm/native-image-munity:22-muslib as build# install necessary toolsrun microdnf install wget run microdnf install xz# install maven for build the spring boot applicationrun wget dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gzrun tar xvf apache-maven-3.9.8-bin.tar.gz# set up the environment variables needed to run the maven mand.env m2_home=/app/apache-maven-3.9.8env m2=$m2_home/binenv path=$m2:$path# install upx (ultimate packer for executables) to press the executable binary and reduce its size.run wget github./upx/upx/releases/download/v4.2.4/upx-4.2.4-amd64_linux.tar.xzrun tar xvf upx-4.2.4-amd64_linux.tar.xz# set up the environment variables required to run the upx mand.env upx_home=/app/upx-4.2.4-amd64_linuxenv path=$upx_home:$path#copy the spring boot source code into containerrun mkdir -p /app/spring-boot-rest-api-appcopy spring-boot-rest-api-app /app/spring-boot-rest-api-app#pile the native imagerun cd /app/spring-boot-rest-api-app && mvn -pnative native:pile#pressed binary filerun upx -7 -k /app/spring-boot-rest-api-app/target/app-native-binaryworkdir /appentrypoint ["/bin/bash"]

我在构建过程中使用 upx 压缩工具来减小图像大小,upx 通常会将程序和 dll 的文件大小减小约 50%-70%,从而减少磁盘空间、网络加载时间、下载时间等配送和存储成本。

使用以下命令构建 docker 镜像。

docker build –no-cache -f dockerfilebuild -t alpine-graalvm-build .

构建完成后,镜像大小为1.85 gb。

repository tag image id created sizealpine-graalvm-build latest81d23bc1bc99 36 seconds ago 1.85gb

我们可以在 alpine linux 盒子内创建较小的容器之前验证容器内的配置和安装。以下命令将允许我们进入容器:

docker run –rm -it –entrypoint /bin/bash alpine-graalvm-buildjava –version #verify the java versionmvn –version #verify the maven versionupx –version #verify the upx versionls /app/spring-boot-rest-api-app/target/app-native-binary #verify the binary available/app/spring-boot-rest-api-app/target/app-native-binary #run the executable

我们知道这个原生镜像包含独立运行二进制文件所需的所有依赖项,而不需要任何与构建相关的工具,例如 graalvm、maven、upx 或源代码。我们可以使用 docker 多阶段构建方法将构建文件复制到我们的应用程序映像中。通过使用多个阶段,您可以将构建环境与运行时环境分开。这意味着最终图像中仅包含必要的工件,从而显着减小其尺寸。

在docker文件中添加以下步骤,文件名为“dockerfilebuildandcreatealpinecontainer”

from ghcr.io/graalvm/native-image-munity:22-muslib as build# install necessary toolsrun microdnf install wget run microdnf install xz# install maven for build the spring boot applicationrun wget dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gzrun tar xvf apache-maven-3.9.8-bin.tar.gz# set up the environment variables needed to run the maven mand.env m2_home=/app/apache-maven-3.9.8env m2=$m2_home/binenv path=$m2:$path# install upx (ultimate packer for executables) to press the executable binary and reduce its size.run wget github./upx/upx/releases/download/v4.2.4/upx-4.2.4-amd64_linux.tar.xzrun tar xvf upx-4.2.4-amd64_linux.tar.xz# set up the environment variables required to run the upx mand.env upx_home=/app/upx-4.2.4-amd64_linuxenv path=$upx_home:$path#copy the spring boot source code into containerrun mkdir -p /app/spring-boot-rest-api-appcopy spring-boot-rest-api-app /app/spring-boot-rest-api-app#pile the native imagerun cd /app/spring-boot-rest-api-app && mvn -pnative native:pile#pressed binary filerun upx -7 -k /app/spring-boot-rest-api-app/target/app-native-binaryworkdir /app#second stage: create the runtime imagefrom amd64/alpine#set the working directoryworkdir /app#copy the built application from the first stagecopy –from=build /app/spring-boot-rest-api-app/target/app-native-binary .#expose port which our spring boot application is runningexpose 8080 #mand to run the applicationentrypoint ["/app/app-native-binary"]

使用以下命令构建 docker 镜像。

docker build -f dockerfilebuildandcreatealpinecontainer -t alpine-graalvm .

构建完成后,容器镜像大小为32.8mb。

repository tag image id created sizealpine-graalvm latest79676c696920 11 seconds ago32.8mb

我们可以验证容器。

docker run –rm -it –entrypoint sh alpine-graalvmls /app #verify the binary available/app/app-native-binary #run the executable

应用程序启动时间仅为 0.074 秒,而在 jvm 上运行的典型 spring boot 应用程序的启动时间约为 1.665 秒。

started tutorialstartupperformanceapplication in 0.074 seconds (process running for 0.075)

以下命令可用于运行 docker 容器来运行应用程序

docker run -d –name test-app -p 8080:8080 alpine-graalvm #run the containercurl localhost:8080/hello # checking the endpoints

spring boot 和 graalvm 参考spring boot 介绍 graalvm 原生镜像graalvm 文档构建 spring boot native executablegraalvm maven 插件文档使用 graalvm 设置 spring boot 应用程序 docker 镜像示例使用 spring boot 和 graalvm 的示例本机映像spring boot 3.2.8 graalvm 原生镜像文档spring boot graalvm upx 教程视频 spring boot alpine linux docker 本机镜像示例## docker 和 graalvm 参考资料graalvm 容器镜像docker 环境变量下载 upx 文档upx 发布docker stop 容器源代码spring boot github 存储库kubernetes 相关仓库

以上就是使用 GraalVM 构建器从 Spring Boot 应用程序构建本机映像的详细内容,更多请关注范的资源库其它相关文章!

转载请注明:范的资源库 » 使用GraalVM构建器从SpringBoot应用程序构建本机映像

喜欢 (0)