编译为本机图像的 SparkJava 不起作用
SparkJava compiled to native-image does not work
我正在尝试将基于 spark-java 的简单 java 应用程序编译为本机映像。该应用程序公开了一个 GET /healthcheck
端点,它始终 returns text/plain: OK
.
项目中只有两个依赖:
dependencies {
implementation 'com.sparkjava:spark-core:2.9.1'
implementation 'org.slf4j:slf4j-simple:1.7.21'
}
我已经通过 运行 fatjar 通过 GraalVM 创建了本机图像配置:
java -agentlib:native-image-agent=config-output-dir=native-image/ -jar javaspark-native-1.0-SNAPSHOT-fatjar.jar
重要的是我正在对端点进行 REST 调用以确保代理可以检查代码中的必要分支。然后将生成的 *.json 文件放入 META-INF/native-image
,因此由 native-image
.
自动选取
然后我使用 multistage docker build 来编译 fatjar 并将 jar 编译成原生镜像:
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y gcc zlib1g-dev wget
RUN wget https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-19.3.0.2/graalvm-ce-java11-linux-amd64-19.3.0.2.tar.gz
RUN tar -vzxf graalvm-ce-java11-linux-amd64-19.3.0.2.tar.gz
ENV PATH /graalvm-ce-java11-19.3.0.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUN gu install native-image
WORKDIR /graalvm-demo
COPY . /graalvm-demo
RUN ./gradlew clean fatJar
RUN native-image --verbose --enable-http -H:+ReportUnsupportedElementsAtRuntime --no-fallback -jar /graalvm-demo/build/libs/javaspark-native-1.0-SNAPSHOT-fatjar.jar
FROM adoptopenjdk/openjdk11:x86_64-alpine-jdk-11.0.3_7-slim
WORKDIR /graalvm-demo
COPY --from=0 /graalvm-demo/javaspark-native-1.0-SNAPSHOT-fatjar .
RUN apk --update --no-cache add \
curl \
tar \
&& rm -rf /var/cache/apk/*
EXPOSE 8080
CMD ./javaspark-native-1.0-SNAPSHOT-fatjar
当 docker 构建通过以下方式触发时:
docker build . -t app
看起来一切正常 - 图像已创建,启动时控制台未报告任何问题:
[Thread-0] INFO org.eclipse.jetty.util.log - Logging initialized @1ms to org.eclipse.jetty.util.log.Slf4jLog
[Thread-0] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - == Spark has ignited ...
[Thread-0] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - >> Listening on 0.0.0.0:8080
[Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.4.z-SNAPSHOT; built: 2019-04-29T20:42:08.989Z; git: e1bc35120a6617ee3df052294e433f3a25ce7097; jvm 11.0.5
[Thread-0] INFO org.eclipse.jetty.server.session - DefaultSessionIdManager workerName=node0
[Thread-0] INFO org.eclipse.jetty.server.session - No SessionScavenger set, using defaults
[Thread-0] INFO org.eclipse.jetty.server.session - node0 Scavenging every 600000ms
[Thread-0] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@6260ba9e{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
[Thread-0] INFO org.eclipse.jetty.server.Server - Started @4ms
但是,当我尝试访问端点时,它不起作用:
[qtp1802776325-8] WARN org.eclipse.jetty.io.ManagedSelector - java.lang.ExceptionInInitializerError
[qtp1802776325-10] WARN org.eclipse.jetty.io.ManagedSelector - java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jetty.server.HttpOutput
有人知道我做错了什么吗?
存储库中演示问题的提交:https://github.com/xmcax/javaspark-native/tree/f5636ed39c63e35861840998a21a0b79bf4ff996
事实证明,自 GraalVM 19 以来。0.x (https://www.graalvm.org/docs/release-notes/19_0/#1900):
There was a change in how classes are initialized in a native-image. Now, we initialize application classes at run time by default.
这就解释了为什么到目前为止我看到的所有教程都没有提到我的问题中描述的问题——它们都使用早期版本的 GraalVM,其中静态 类 默认在构建时初始化。应用相同的方法解决了这个问题。有:
RUN native-image \
-H:+ReportUnsupportedElementsAtRuntime \
-H:+TraceClassInitialization \
--verbose \
--enable-http \
--static \
--no-fallback \
--initialize-at-build-time=org.eclipse.jetty,org.slf4j,javax.servlet,org.sparkjava \
-jar /sparkjava/build/libs/javaspark-native-1.0-SNAPSHOT-fatjar.jar
一切正常。使用 alpine 基础图像生成的 docker 图像是 26M:
REPOSITORY TAG IMAGE ID CREATED SIZE
ni7 latest dcb94dbfb6ad About a minute ago 26MB
完整的解决方案可以在这里看到:https://github.com/xmcax/javaspark-native/tree/b9235b1f777de661c0087fbfef4ccaf39303ad3e
我正在尝试将基于 spark-java 的简单 java 应用程序编译为本机映像。该应用程序公开了一个 GET /healthcheck
端点,它始终 returns text/plain: OK
.
项目中只有两个依赖:
dependencies {
implementation 'com.sparkjava:spark-core:2.9.1'
implementation 'org.slf4j:slf4j-simple:1.7.21'
}
我已经通过 运行 fatjar 通过 GraalVM 创建了本机图像配置:
java -agentlib:native-image-agent=config-output-dir=native-image/ -jar javaspark-native-1.0-SNAPSHOT-fatjar.jar
重要的是我正在对端点进行 REST 调用以确保代理可以检查代码中的必要分支。然后将生成的 *.json 文件放入 META-INF/native-image
,因此由 native-image
.
然后我使用 multistage docker build 来编译 fatjar 并将 jar 编译成原生镜像:
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y gcc zlib1g-dev wget
RUN wget https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-19.3.0.2/graalvm-ce-java11-linux-amd64-19.3.0.2.tar.gz
RUN tar -vzxf graalvm-ce-java11-linux-amd64-19.3.0.2.tar.gz
ENV PATH /graalvm-ce-java11-19.3.0.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUN gu install native-image
WORKDIR /graalvm-demo
COPY . /graalvm-demo
RUN ./gradlew clean fatJar
RUN native-image --verbose --enable-http -H:+ReportUnsupportedElementsAtRuntime --no-fallback -jar /graalvm-demo/build/libs/javaspark-native-1.0-SNAPSHOT-fatjar.jar
FROM adoptopenjdk/openjdk11:x86_64-alpine-jdk-11.0.3_7-slim
WORKDIR /graalvm-demo
COPY --from=0 /graalvm-demo/javaspark-native-1.0-SNAPSHOT-fatjar .
RUN apk --update --no-cache add \
curl \
tar \
&& rm -rf /var/cache/apk/*
EXPOSE 8080
CMD ./javaspark-native-1.0-SNAPSHOT-fatjar
当 docker 构建通过以下方式触发时:
docker build . -t app
看起来一切正常 - 图像已创建,启动时控制台未报告任何问题:
[Thread-0] INFO org.eclipse.jetty.util.log - Logging initialized @1ms to org.eclipse.jetty.util.log.Slf4jLog
[Thread-0] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - == Spark has ignited ...
[Thread-0] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - >> Listening on 0.0.0.0:8080
[Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.4.z-SNAPSHOT; built: 2019-04-29T20:42:08.989Z; git: e1bc35120a6617ee3df052294e433f3a25ce7097; jvm 11.0.5
[Thread-0] INFO org.eclipse.jetty.server.session - DefaultSessionIdManager workerName=node0
[Thread-0] INFO org.eclipse.jetty.server.session - No SessionScavenger set, using defaults
[Thread-0] INFO org.eclipse.jetty.server.session - node0 Scavenging every 600000ms
[Thread-0] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@6260ba9e{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
[Thread-0] INFO org.eclipse.jetty.server.Server - Started @4ms
但是,当我尝试访问端点时,它不起作用:
[qtp1802776325-8] WARN org.eclipse.jetty.io.ManagedSelector - java.lang.ExceptionInInitializerError
[qtp1802776325-10] WARN org.eclipse.jetty.io.ManagedSelector - java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jetty.server.HttpOutput
有人知道我做错了什么吗?
存储库中演示问题的提交:https://github.com/xmcax/javaspark-native/tree/f5636ed39c63e35861840998a21a0b79bf4ff996
事实证明,自 GraalVM 19 以来。0.x (https://www.graalvm.org/docs/release-notes/19_0/#1900):
There was a change in how classes are initialized in a native-image. Now, we initialize application classes at run time by default.
这就解释了为什么到目前为止我看到的所有教程都没有提到我的问题中描述的问题——它们都使用早期版本的 GraalVM,其中静态 类 默认在构建时初始化。应用相同的方法解决了这个问题。有:
RUN native-image \
-H:+ReportUnsupportedElementsAtRuntime \
-H:+TraceClassInitialization \
--verbose \
--enable-http \
--static \
--no-fallback \
--initialize-at-build-time=org.eclipse.jetty,org.slf4j,javax.servlet,org.sparkjava \
-jar /sparkjava/build/libs/javaspark-native-1.0-SNAPSHOT-fatjar.jar
一切正常。使用 alpine 基础图像生成的 docker 图像是 26M:
REPOSITORY TAG IMAGE ID CREATED SIZE
ni7 latest dcb94dbfb6ad About a minute ago 26MB
完整的解决方案可以在这里看到:https://github.com/xmcax/javaspark-native/tree/b9235b1f777de661c0087fbfef4ccaf39303ad3e