写点什么

应“云”而生的 Java 框架,带你手撸红黑树

用户头像
极客good
关注
发布于: 刚刚

export GRAALVM_HOME=sdk home java 21.0.0.2.r11-grl


使用 gu 安装 native-image :


${GRAALVM_HOME}/bin/gu install native-image


构建本机可执行文件


=========


在源码的 pom.xml 中,我们可以看到如下的 profile :


<profiles>


<profile>


<id>native</id>


<properties>


<quarkus.package.type>native</quarkus.package.type>


</properties>


</profile>


</profiles>


我们使用这个 profile 进行本机可执行文件的构建,整个构建耗时 几分钟


./mvnw package -Pnative


部分构建日志:


[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ quarkus-getting-started ---


[INFO]


[INFO] --- quarkus-maven-plugin:1.13.0.Final:build (default) @ quarkus-getting-started ---


[INFO] [org.jboss.threads] JBoss Threads version 3.2.0.Final


[INFO] [io.quarkus.deployment.pkg.steps.JarResultBuildStep] Building native image source jar: /Users/addo/Workspaces/private_w/quarkus-getting-started/target/quarkus-getting-started-1.0.0-SNAPSHOT-native-image-source-jar/quarkus-getting-started-1.0.0-SNAPSHOT-runner.jar


[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildStep] Building native image from /Users/addo/Workspaces/private_w/quarkus-getting-started/target/quarkus-getting-started-1.0.0-SNAPSHOT-native-image-source-jar/quarkus-getting-started-1.0.0-SNAPSHOT-runner.jar


[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildContainerRunner] Using docker to run the native image builder


[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildContainerRunner] Checking image status quay.io/quarkus/ubi-quarkus-native-image:21.0.0-java11


21.0.0-java11: Pulling from quarkus/ubi-quarkus-native-image


Digest: sha256:becf08de869e707beaa5e57444b533ef93ebef15aad90c92ac660ddf7cea2b11


Status: Image is up to date for quay.io/quarkus/ubi-quarkus-native-image:21.0.0-java11


quay.io/quarkus/ubi-quarkus-native-image:21.0.0-java11


[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildStep] Running Quarkus native-image plugin on GraalVM Version 21.0.0 (Java Version 11.0.10+8-jvmci-21.0-b06)


[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildRunner] docker run --env LANG=C --rm -v /Users/addo/Workspaces/private_w/quarkus-getting-started/target/quarkus-getting-started-1.0.0-SNAPSHOT-native-image-source-jar:/project:z quay.io/quarkus/ubi-quarkus-native-image:21.0.0-java11 -J-Dsun.nio.ch.maxUpdateArraySize=100 -J-Djava.util.logging.manager=org.jboss.logmanager.LogManager -J-Dvertx.logger-delegate-factory-class-name=io.quarkus.vertx.core.runtime.VertxLogDelegateFactory -J-Dvertx.disableDnsResolver=true -J-Dio.netty.leakDetection.level=DISABLED -J-Dio.netty.allocator.maxOrder=1 -J-Duser.language=en -J-Duser.country=CN -J-Dfile.encoding=UTF-8 --initialize-at-build-time= -H:InitialCollectionPolicy=com.oracle.svm.core.genscavenge.CollectionPolicy$BySpaceAndTime -H:+JNI -H:+AllowFoldMethods -jar quarkus-getting-started-1.0.0-SNAPSHOT-runner.jar -H:FallbackThreshold=0 -H:+ReportExceptionStackTraces -J-Xmx5g -H:-AddAllCharsets -H:EnableURLProtocols=http --no-server -H:-UseServiceLoaderFeature -H:+StackTrace quarkus-getting-started-1.0.0-SNAPSHOT-runner


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] classlist: 5,859.24 ms, 0.96 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] (cap): 633.34 ms, 0.94 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] setup: 2,468.19 ms, 0.94 GB


00:06:00,437 INFO [org.jbo.threads] JBoss Threads version 3.2.0.Final


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] (clinit): 516.65 ms, 2.23 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] (typeflow): 12,642.02 ms, 2.23 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] (objects): 11,340.37 ms, 2.23 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] (features): 525.87 ms, 2.23 GB


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] analysis: 26,032.67 ms, 2.23 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] universe: 1,394.06 ms, 2.16 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] (parse): 2,690.38 ms, 2.16 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] (inline): 4,336.77 ms, 2.73 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] (compile): 17,580.03 ms, 2.71 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] compile: 26,152.06 ms, 2.71 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] image: 3,288.43 ms, 2.70 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] write: 1,904.64 ms, 2.70 GB


[quarkus-getting-started-1.0.0-SNAPSHOT-runner:25] [total]: 67,414.16 ms, 2.70 GB


[WARNING] [io.quarkus.deployment.pkg.steps.NativeImageBuildStep] objcopy executable not found in PATH. Debug symbols will not be separated from executable.


[WARNING] [io.quarkus.deployment.pkg.steps.NativeImageBuildStep] That will result in a larger native image with debug symbols embedded in it.


[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 74739ms


[INFO] ------------------------------------------------------------------------


[INFO] BUILD SUCCESS


[INFO] ------------------------------------------------------------------------


[INFO] Total time: 01:21 min


[INFO] Finished at: 2021-04-17T08:06:47+08:00


[INFO] ------------------------------------------------------------------------


假如构建时出现类似 Caused by: java.lang.RuntimeException: Image generation failed. Exit code was 137 which indicates an out of memory error. Consider increasing the Xmx value for native image generation by setting the "quarkus.native.native-image-xmx" property 这种报错。需要调整下 Docker 的设置,比如笔者使用的 macOS,打开 Docker Desktop > Preference > Resource > Advanced,将内存从默认的 2GB 调大,比如 8GB。


从构建日志可以看出,构建的过程是在 quay.io/quarkus/ubi-quarkus-native-image 的容器中完成的。虽然异常提示调整 “quarkus.native.native-image-xmx” ,其实是容器内存太小导致的。


构建成功后,可以在 target 中找到 quarkus-getting-started-1.0.0-SNAPSHOT-runner 。这是一个可执行文件,大小为 28MB。


尝试执行该文件,收到 zsh: exec format error: ./target/quarkus-getting-started-1.0.0-SNAPSHOT-runner 错误。因为这是一个 Linux 可执行文件,因此我们需要在容器中运行。


构建本机镜像


======


在源文件的 src/main/docker 目录中,我们可以找到 Dockerfile.native :


FROM registry.access.redhat.com/ubi8/ubi-minimal:8.3


WORKDIR /work/


RUN chown 1001 /work \


&& chmod "g+rwX" /work \


&& chown 1001:root /work


COPY --chown=1001:root target/*-runner /work/application


EXPOSE 8080


USER 1001


CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]


运行镜像


====


本地运行一下,可以看出启动只需要 0.013s 。


docker run --rm -p 8080:8080 quarkus/quarkus-getting-started:latest




--/ __ / / / / _ | / _ / //_/ / / / __/


-/ // / // / __ |/ , / ,< / // /\ \


--___// |//|//||_/_/


2021-04-17 00:22:27,146 INFO [io.quarkus] (main) quarkus-getting-started 1.0.0-SNAPSHOT native (powered by Quarkus 1.13.0.Final) started in 0.013s. Listening on: http://0.0.0.0:8080


2021-04-17 00:22:27,147 INFO [io.quarkus] (main) Profile prod activated.


2021-04-17 00:22:27,147 INFO [io.quarkus] (main) Installed features: [cdi, resteasy]


测试一下端点:


http :8080/hello/greeting/quarkus


HTTP/1.1 200 OK


Content-Length: 14


Content-Type: text/plain;charset=UTF-8


Hello, quarkus


看下镜像的信息,大小为 132MB,其中 base 镜像 ubi-minimal 就占了 103 MB。感觉还是有点大,是否继续精简一下?


docker images


REPOSITORY TAG IMAGE ID CREATED SIZE


quarkus/quarkus-getting-started latest 8f86f5915715 4 minutes ago 132MB


registry.access.redhat.com/ubi8/ubi-minimal 8.3 604ddd554fec 2 weeks ago 103MB


镜像瘦身


====

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
应“云”而生的 Java 框架,带你手撸红黑树