概述
本节介绍如何使用 graalvm 的本机映像生成器从 spring boot 应用程序创建本机映像,以及如何在 docker 容器中运行此本机映像。
客观的
在软件架构和微服务架构设计中,我们必须考虑应用程序的可扩展性、性能。每当应用程序中的请求数量增加时,我们的应用程序应该开始快速扩展并有效地利用资源。
我正在考虑使用 spring boot 提前 (aot) 编译与 graalvm 以及 java 虚拟线程(在 jdk 21 及更高版本中可用)在容器中运行可执行文件。
aot 编译对于快速启动时间和可预测性能很重要的场景是有利的,但代价是运行时适应性较差。
与虚拟机 (vm) 相比,容器是轻量级的,使用的资源更少,因为它们共享主机操作系统内核。容器的启动和停止速度比虚拟机快得多,从而实现更快的扩展和部署。
虚拟线程可以提高处理大量并发任务的应用程序的性能。这对于 web 服务器、数据库和其他 i/o 密集型系统等应用程序尤其有利。虚拟线程比传统线程使用更少的资源。它们由运行时以最小化内存使用和 cpu 开销的方式进行管理。
在这个架构设计决策中,我们获得了好处,但也必须考虑以下实施挑战和设计注意事项:
点击下载“电脑DLL/驱动修复工具”;
虚拟线程:如果我们的业务逻辑是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 属性配置
220.12.1登录后复制
spring boot aot 插件配置
org.springframework.bootspring-boot-maven-pluginprocess-aotprocess-aot登录后复制
graalvm 插件配置
org.graalvm.buildtoolsnative-maven-pluginapp-native-binarytrue--static --libc=musl-h:+reportexceptionstacktracescom.developerhelperhub.tutorial.springboot.tutorial.tutorialstartupperformanceapplicationadd-reachability-metadataadd-reachability-metadata登录后复制
“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 image
docker image inspect amd64/alpine # inspect the image
登录后复制
我们可以使用 docker 容器来构建原生镜像,而不用在本地设置 graalvm 和 java 相关配置。我正在使用 “ghcr.io/graalvm/native-image-community:22-muslib” docker 映像来构建本机。
以下命令我们可以用来检查“ghcr.io/graalvm/native-image-community:22-muslib”图像
docker pull ghcr.io/graalvm/native-image-community:22-muslib # pull the image
docker image inspect ghcr.io/graalvm/native-image-community:22-muslib # inspect the image
登录后复制
我正在创建一个构建映像来测试和调试容器,确保所有配置和服务都正确安装。这种方法将帮助我们快速识别并解决任何问题。
在docker文件中添加以下步骤,文件名为“dockerfilebuild”
from ghcr.io/graalvm/native-image-community:22-muslib as build
# install necessary tools
run microdnf install wget
run microdnf install xz
# install maven for build the spring boot application
run wget https://dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gz
run tar xvf apache-maven-3.9.8-bin.tar.gz
# set up the environment variables needed to run the maven command.
env m2_home=/app/apache-maven-3.9.8
env m2=$m2_home/bin
env path=$m2:$path
# install upx (ultimate packer for executables) to compress the executable binary and reduce its size.
run wget https://github.com/upx/upx/releases/download/v4.2.4/upx-4.2.4-amd64_linux.tar.xz
run tar xvf upx-4.2.4-amd64_linux.tar.xz
# set up the environment variables required to run the upx command.
env upx_home=/app/upx-4.2.4-amd64_linux
env path=$upx_home:$path
#copy the spring boot source code into container
run mkdir -p /app/spring-boot-rest-api-app
copy spring-boot-rest-api-app /app/spring-boot-rest-api-app
#compile the native image
run cd /app/spring-boot-rest-api-app && mvn -pnative native:compile
#compressed binary file
run upx -7 -k /app/spring-boot-rest-api-app/target/app-native-binary
workdir /app
entrypoint ["/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 size
alpine-graalvm-build latest 81d23bc1bc99 36 seconds ago 1.85gb
登录后复制
我们可以在 alpine linux 盒子内创建较小的容器之前验证容器内的配置和安装。以下命令将允许我们进入容器:
docker run --rm -it --entrypoint /bin/bash alpine-graalvm-build
java --version #verify the java version
mvn --version #verify the maven version
upx --version #verify the upx version
ls /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-community:22-muslib as build
# install necessary tools
run microdnf install wget
run microdnf install xz
# install maven for build the spring boot application
run wget https://dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gz
run tar xvf apache-maven-3.9.8-bin.tar.gz
# set up the environment variables needed to run the maven command.
env m2_home=/app/apache-maven-3.9.8
env m2=$m2_home/bin
env path=$m2:$path
# install upx (ultimate packer for executables) to compress the executable binary and reduce its size.
run wget https://github.com/upx/upx/releases/download/v4.2.4/upx-4.2.4-amd64_linux.tar.xz
run tar xvf upx-4.2.4-amd64_linux.tar.xz
# set up the environment variables required to run the upx command.
env upx_home=/app/upx-4.2.4-amd64_linux
env path=$upx_home:$path
#copy the spring boot source code into container
run mkdir -p /app/spring-boot-rest-api-app
copy spring-boot-rest-api-app /app/spring-boot-rest-api-app
#compile the native image
run cd /app/spring-boot-rest-api-app && mvn -pnative native:compile
#compressed binary file
run upx -7 -k /app/spring-boot-rest-api-app/target/app-native-binary
workdir /app
#second stage: create the runtime image
from amd64/alpine
#set the working directory
workdir /app
#copy the built application from the first stage
copy --from=build /app/spring-boot-rest-api-app/target/app-native-binary .
#expose port which our spring boot application is running
expose 8080
#command to run the application
entrypoint ["/app/app-native-binary"]
登录后复制
使用以下命令构建 docker 镜像。
docker build -f dockerfilebuildandcreatealpinecontainer -t alpine-graalvm .
登录后复制
构建完成后,容器镜像大小为32.8mb。
repository tag image id created size
alpine-graalvm latest 79676c696920 11 seconds ago 32.8mb
登录后复制
我们可以验证容器。
docker run --rm -it --entrypoint sh alpine-graalvm
ls /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 container
curl http://localhost:8080/hello # checking the endpoints
登录后复制
spring boot 和 graalvm 参考
spring boot 介绍 graalvm 原生镜像
graalvm 文档构建 spring boot native executable
graalvm 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 应用程序构建本机映像的详细内容,更多请关注php中文网其它相关文章!
Vegazone Casino15 天前
发表在:thinkphp怎么做小程序接口Just wrapped up a de...
Vegazone Casino16 天前
发表在:thinkphp怎么做小程序接口Just had a ripper ar...
AndrewRep17 天前
发表在:03日05日,星期四,在这里每天60秒读懂世界!Інколи хочеться віді...
Barrypaymn17 天前
发表在:03日05日,星期四,在这里每天60秒读懂世界!Читання стало для ме...
AndrewRep17 天前
发表在:11日20日,星期四,在这里每天60秒读懂世界!У сучасному світі ін...
Barrypaymn17 天前
发表在:11日20日,星期四,在这里每天60秒读懂世界!Книжкові блоги стали...
Kevinvorma27 天前
发表在:03日05日,星期四,在这里每天60秒读懂世界!Під час вибору проду...
Kevinvorma27 天前
发表在:11日20日,星期四,在这里每天60秒读懂世界!Ринок пропонує широк...
Jamesfourl1 个月前
发表在:11日20日,星期四,在这里每天60秒读懂世界!Багато людей не посп...
LincolnWap1 个月前
发表在:03日05日,星期四,在这里每天60秒读懂世界!Сучасні жінки прагну...