概述
本节介绍如何使用 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>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>
- “mainClass”:配置Spring Boot应用程序的邮件类
- "imageName": 配置原生镜像名称
- “buildArgs”:配置 —libc=”msul”,我们正在配置 GraalVM 以使用“libc musl”兼容库构建本机映像,因为我们将在 Alpine Linux 机器上运行此映像。与其他标准库相比,Musl 的设计更小,使用的内存更少,非常适合资源受限的环境。
构建二进制文件并创建 Docker 镜像
我们需要为特定操作系统主机和 CPU 架构构建本机映像,本机映像将在容器中运行。
我们使用 Alpine Linux 来在容器中运行我们的应用程序,因为它体积小、简单且安全。为了实现这一点,我们需要使用适当的 GraalVM 配置来构建我们的应用程序。 Alpine 的系统要求是操作系统和 CPU 架构。
- “架构”:“amd64”
- “操作系统”:“linux”
- C 通用库:“libc musl”
以下命令我们可以用来检查“amd64/alpine”图像
docker pull amd64/alpine # pull the image docker image inspect amd64/alpine # inspect the image
We can use docker container to build the native image instead of setup the GraalVM and Java related configuration in our locally. I am using “ghcr.io/graalvm/native-image-community:22-muslib” docker image to build the native.
Following command we can use to inspect the “ghcr.io/graalvm/native-image-community:22-muslib” image
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
I am creating a build image to test and debug the container, ensuring that all configurations and services are installed correctly. This approach will help us quickly identify and resolve any issues.
Following steps are added in the docker file, the file name “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"]
I am using the UPX compression tool in the build process to reduce the image size, UPX will typically reduce the file size of programs and DLLs by around 50%-70%, thus reducing disk space, network load times, download times and other distribution and storage costs.
Use the following command to build the Docker image.
docker build --no-cache -f DockerfileBuild -t alpine-graalvm-build .
After the build is complete, the image size will be 1.85 GB.
REPOSITORY TAG IMAGE ID CREATED SIZE alpine-graalvm-build latest 81d23bc1bc99 36 seconds ago 1.85GB
We can verify the configuration and installation within the container before creating a smaller container inside the Alpine Linux box. The following command will allow us to enter the container:
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
We know that this native image includes all the dependencies necessary to run the binary standalone, without requiring any build-related tools such as GraalVM, Maven, UPX, or source code. We can use a Docker multi-stage build approach to copy the build file into our application image. By using multiple stages, you can separate the build environment from the runtime environment. This means only the necessary artifacts are included in the final image, significantly reducing its size.
Following steps are added in the docker file, the file name “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"]
Use the following command to build the Docker image.
docker build -f DockerfileBuildAndCreateAlpineContainer -t alpine-graalvm .
After the build is complete, the image size of container will be 32.8MB.
REPOSITORY TAG IMAGE ID CREATED SIZE alpine-graalvm latest 79676c696920 11 seconds ago 32.8MB
We can verify the container.
docker run --rm -it --entrypoint sh alpine-graalvm ls /app #verify the binary available /app/app-native-binary #run the executable
The application startup time is just 0.074 seconds, whereas a typical Spring Boot application running on the JVM has a startup time of approximately 1.665 seconds.
Started TutorialStartupPerformanceApplication in 0.074 seconds (process running for 0.075)
Following command can be use to run the docker container for running the application
docker run -d --name test-app -p 8080:8080 alpine-graalvm #run the container curl http://localhost:8080/hello # checking the endpoints
Spring boot and GraalVM references
- Spring Boot Introduction GraalVM Native Images
- GraalVM documentation build Spring Boot Native Executable
- GraalVM Maven Plugin Documentation
- Sample Spring Boot Application Docker Image Setup with GraalVM
- Sample Native Images with Spring Boot and GraalVM
- Spring Boot 3.2.8 GraalVM Native Images Documentation
- Spring Boot GraalVM UPX Tutorial Video
- Spring Boot Alpine Linux Docker Native Image Example ## Docker and GraalVM References
- GraalVM Containers Images
- Docker Environment Variables
- Maven Download
- UPX Documentation
- UPX Releases
- Docker Stop Container
Source Code
- Spring Boot Github Repo
- Kubernetes Related Repo
以上是使用 GraalVM 构建器从 Spring Boot 应用程序构建本机映像的详细内容。更多信息请关注PHP中文网其他相关文章!

Java如何缓解平台特定的问题?Java通过JVM和标准库来实现平台无关性。1)使用字节码和JVM抽象操作系统差异;2)标准库提供跨平台API,如Paths类处理文件路径,Charset类处理字符编码;3)实际项目中使用配置文件和多平台测试来优化和调试。

java'splatformentenceenhancesenhancesmicroservicesharchitecture byferingDeploymentFlexible,一致性,可伸缩性和便携性。1)DeploymentFlexibilityAllowsibilityAllowsOllowsOllowSorlowsOllowsOllowsOllowSeStorunonAnyPlatformwithajvM.2)penterencyCrossServAccAcrossServAcrossServiCessImplifififiesDeevelopmentandeDe

GraalVM通过三种方式增强了Java的平台独立性:1.跨语言互操作,允许Java与其他语言无缝互操作;2.独立的运行时环境,通过GraalVMNativeImage将Java程序编译成本地可执行文件;3.性能优化,Graal编译器生成高效的机器码,提升Java程序的性能和一致性。

效率testjavaapplicationsforplatformcompatibility oftheSesteps:1)setUpautomatedTestingTestingActingAcrossMultPlatFormSusingCitoolSlikeSlikeJenkinSorgithUbactions.2)contuctualtemualtemalualTesteTESTENRETESTINGINREALHARTWARETOLEALHARDOELHARDOLEATOCATCHISSUSESUSEUSENINCIENVIRENTMENTS.3)schictcross.3)schoscross.3)

Java编译器通过将源代码转换为平台无关的字节码,实现了Java的平台独立性,使得Java程序可以在任何安装了JVM的操作系统上运行。

ByteCodeachievesPlatFormIndenceByByByByByByExecutedBoviratualMachine(VM),允许CodetorunonanyplatformwithTheApprepreprepvm.Forexample,Javabytecodecodecodecodecanrunonanydevicewithajvm

Java不能做到100%的平台独立性,但其平台独立性通过JVM和字节码实现,确保代码在不同平台上运行。具体实现包括:1.编译成字节码;2.JVM的解释执行;3.标准库的一致性。然而,JVM实现差异、操作系统和硬件差异以及第三方库的兼容性可能影响其平台独立性。

Java通过“一次编写,到处运行”实现平台独立性,提升代码可维护性:1.代码重用性高,减少重复开发;2.维护成本低,只需一处修改;3.团队协作效率高,方便知识共享。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3汉化版
中文版,非常好用

记事本++7.3.1
好用且免费的代码编辑器

Dreamweaver Mac版
视觉化网页开发工具