本节介绍如何使用 graalvm 的本机映像生成器从 spring boot 应用程序创建本机映像,以及如何在 docker 容器中运行此本机映像。
在软件架构和微服务架构设计中,我们必须考虑应用程序的可扩展性、性能。每当应用程序中的请求数量增加时,我们的应用程序应该开始快速扩展并有效地利用资源。
我正在考虑使用 spring boot 提前 (aot) 编译与 graalvm 以及 java 虚拟线程(在 jdk 21 及更高版本中可用)在容器中运行可执行文件。
在这个架构设计决策中,我们获得了好处,但也必须考虑以下实施挑战和设计注意事项:
spring boot 应用程序
为了测试这个用例,我正在构建一个 spring boot 应用程序,该应用程序在“/hello”处公开 rest 端点。我正在使用以下配置、库和工具:
我们需要在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>com.developerhelperhub.tutorial.springboot.tutorial.tutorialstartupperformanceapplication</mainclass> </configuration> <executions> <execution> <id>add-reachability-metadata</id> <goals> <goal>add-reachability-metadata</goal> </goals> </execution> </executions> </plugin>
我们需要为特定的操作系统主机和cpu架构构建原生镜像,原生镜像将在容器中运行。
我们使用 alpine linux 来在容器中运行我们的应用程序,因为它体积小、简单且安全。为了实现这一点,我们需要使用适当的 graalvm 配置来构建我们的应用程序。 alpine 的系统要求是操作系统和 cpu 架构。
以下命令我们可以用来检查“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