tos*_*tao 5 docker graalvm quarkus quarkus-native
我开始玩 quarkus 和 graalvm。我将文件(txt 和 jpg)添加到项目 ( src/main/resources/) 中的资源中。为了确保我可以在控制器中访问该文件,我显示了它的大小:
URL url = Thread.currentThread().getContextClassLoader().getResource("/Resource2.txt");\nFile file = new File(url.toURI());\nreturn "Hello My Friend! File size in bytes = " + file.length();\nRun Code Online (Sandbox Code Playgroud)\n当我用 maven ( mvn quarkus:dev) 运行它时,它就可以工作了。控制器代码在这里。
当我创建本机 Quarkus 应用程序并尝试在 docker 内运行时出现问题。\n为了确保该文件包含在本机映像中,我添加了一个大 jpg 文件 (3.3MB),创建了resources-config.json:
{ "resources": \n { "includes": [\n { "pattern": "IMG_3_3M\\\\.jpg$"},\n { "pattern": "Resources2\\\\.txt$"}\n ]\n}}\nRun Code Online (Sandbox Code Playgroud)\n并application.properties添加:\n quarkus.native.additional-build-args = -H:ResourceConfigurationFiles=resources-config.json。原生跑步者尺寸增加自:
39M Mar 21 12:48 hello-quarkus-1.0-SNAPSHOT-runner44M Mar 21 12:19 hello-quarkus-1.0-SNAPSHOT-runner所以我假设包含了 jpg 文件,但是当在 docker 映像内运行本机应用程序时,我仍然得到 NPE:
\n Caused by: java.lang.NullPointerException\n at it.tostao.quickstart.GreetingResource.hello(GreetingResource.java:24)\nRun Code Online (Sandbox Code Playgroud)\n其中第 24 行: 是url.toURI().
知道如何读取本机图像中的资源吗?配置中是否缺少某些内容?
\n这是重现问题的示例图像,构建和运行本机图像所需的所有命令都可以在README.MD中找到:
\nhttps://github.com/sleski/hello-quarkus
\n到目前为止,我检查了这个网址,仍然无法在本机图像中找到资源:
\n\xe2\x80\xa2如何在 Quarkus 本机映像中包含类路径资源?
\n\xe2\x80\xa2如何读取 Quarkus 原生镜像中的类路径资源?
\n\xe2\x80\xa2 https://quarkus.io/guides/writing-native-applications-tips
\n\xe2\x80\xa2从 Docker 容器中的 maven Quarkus 项目上的资源文件夹中读取 txt 文件
\n首先修复json
{
"resources": [
{
"pattern": "Resource2.txt"
}
]
}
Run Code Online (Sandbox Code Playgroud)
或者您可以将 *.txt 作为模式。就像在文档中一样
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/Resources.md说使用
InputStream resource = ModuleLayer.boot().findModule(moduleName).getResourceAsStream(resourcePath);
Run Code Online (Sandbox Code Playgroud)
当我尝试时遇到了问题。您可以在下面看到您项目的工作代码
@Path("/hello")
public class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() throws IOException {
String moduleName = "java.base";
String resourcePath = "/Resource2.txt";
Module resource = ModuleLayer.boot().findModule(moduleName).get();
InputStream ins = resource.getResourceAsStream(resourcePath);
if (ins == null) {
System.out.println("module came empty, now trying to load from GreetingResource");
ins = GreetingResource.class.getResourceAsStream(resourcePath);
}
if (ins != null) {
StringBuilder sb = new StringBuilder();
for (int ch; (ch = ins.read()) != -1; ) {
sb.append((char) ch);
}
return "Hello My Friend! File size in bytes = " + sb;
}
return "empty";
}
}
Run Code Online (Sandbox Code Playgroud)
GreetingResource.class.getResourceAsStream(resourcePath);实际上是把资源带到这里来的。我认为这个功能将来可能会改变,所以我也在代码中留下了 ModuleLayer 。我用的是graalvm 17-21.3.0
你可以在下面找到构建日志
[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildRunner] C:\Program Files\GraalVM\graalvm-ce-java17-21.3.0\bin\native-image.cmd -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=3 -J-Duser.language=en -J-Duser.country=GB -J-Dfile.encoding=UTF-8 -H:-ParseOnce -J--add-exports=java.security.jgss/sun.security.krb5=ALL-UNNAMED -J--add-opens=java.base/java.text=ALL-UNNAMED -H:ResourceConfigurationFiles=resources-config.json -H:+PrintAnalysisCallTree -H:Log=registerResource:verbose -H:InitialCollectionPolicy=com.oracle.svm.core.genscavenge.CollectionPolicy\$BySpaceAndTime -H:+JNI -H:+AllowFoldMethods -J-Djava.awt.headless=true -H:FallbackThreshold=0 -H:+ReportExceptionStackTraces -H:-AddAllCharsets -H:EnableURLProtocols=http -H:-UseServiceLoaderFeature -H:+StackTrace -J--add-exports=java.management/sun.management=ALL-UNNAMED hello-quarkus-1.0-SNAPSHOT-runner -jar hello-quarkus-1.0-SNAPSHOT-runner.jar
[hello-quarkus-1.0-SNAPSHOT-runner:20428] classlist: 2,920.35 ms, 0.94 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] (cap): 1,493.84 ms, 0.94 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] setup: 2,871.07 ms, 0.94 GB
[Use -Dgraal.LogFile=<path> to redirect Graal log output to a file.]
[thread:1] scope: main
[thread:1] scope: main.registerResource
ResourcesFeature: registerResource: Resource2.txt
14:23:38,709 INFO [org.jbo.threads] JBoss Threads version 3.4.2.Final
[thread:1] scope: main.registerResource
ResourcesFeature: registerResource: java/lang/uniName.dat
[hello-quarkus-1.0-SNAPSHOT-runner:20428] (clinit): 475.20 ms, 5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] (typeflow): 2,931.83 ms, 5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] (objects): 24,294.27 ms, 5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] (features): 2,979.07 ms, 5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] analysis: 32,083.24 ms, 5.14 GB
# Printing call tree to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\call_tree_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142406.txt
# Printing list of used methods to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\used_methods_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142407.txt
# Printing list of used classes to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\used_classes_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142407.txt
# Printing list of used packages to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\used_packages_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142407.txt
# Printing call tree for vm entry point to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_vm_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for methods to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_methods_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for virtual methods to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_virtual_methods_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for entry points to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_entry_points_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for direct edges to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_direct_edges_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for overriden by edges to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_override_by_edges_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for virtual edges to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_virtual_edges_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
[hello-quarkus-1.0-SNAPSHOT-runner:20428] universe: 1,547.28 ms, 5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] (parse): 4,919.32 ms, 4.87 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] (inline): 7,013.78 ms, 5.83 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] (compile): 27,387.04 ms, 5.56 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] compile: 41,595.59 ms, 5.56 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] image: 2,515.22 ms, 5.56 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] write: 858.79 ms, 5.56 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428] [total]: 90,068.97 ms, 5.56 GB
# Printing build artifacts to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\hello-quarkus-1.0-SNAPSHOT-runner.build_artifacts.txt
[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 94323ms
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:37 min
[INFO] Finished at: 2022-03-24T14:24:56Z
[INFO] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4483 次 |
| 最近记录: |