docker 如何访问Quarkus本地映像中的资源?

to94eoyn  于 2023-02-21  发布在  Docker
关注(0)|答案(2)|浏览(146)

我开始玩quarkus和graalvm。我添加文件(txt和jpg)到项目(src/main/resources/)中的资源。为了确保我可以在控制器中访问这个文件,我显示了它的大小:

URL url = Thread.currentThread().getContextClassLoader().getResource("/Resource2.txt");
File file = new File(url.toURI());
return "Hello My Friend! File size in bytes = " + file.length();

当我用Maven(mvn quarkus:dev)运行它时,它可以工作。控制器代码是here
当我创建原生Quarkus应用程序并尝试在Docker中运行时发生问题。为了确保该文件包含在原生映像中,我添加了一个大的jpg文件(3.3MB),创建了resources-config.json

{ "resources": 
 { "includes": [
        { "pattern": "IMG_3_3M\\.jpg$"},
        { "pattern": "Resources2\\.txt$"}
       ]
}}

并在application.properties中添加:quarkus.native.additional-build-args = -H:ResourceConfigurationFiles=resources-config.json。本地流道大小从以下值增加:

  • 39M Mar 21 12:48 hello-quarkus-1.0-SNAPSHOT-runner
  • 至:44M Mar 21 12:19 hello-quarkus-1.0-SNAPSHOT-runner

因此,我假设jpg文件包括在内,但仍然在Docker图像内运行本机应用程序时,我得到了NPE:

Caused by: java.lang.NullPointerException
  at it.tostao.quickstart.GreetingResource.hello(GreetingResource.java:24)

其中第24行:是url.toURI()

知道如何读取本机映像中的资源吗?配置中是否缺少某些内容?

以下是重现该问题的示例映像,构建和运行本机映像所需的所有命令可在README.MD中找到:
https://github.com/sleski/hello-quarkus
到目前为止,我检查了这个网址,仍然无法在本机映像中找到资源:
· How to include classpath resources in a Quarkus native image?
· How to read classpath resources in Quarkus native image?
· https://quarkus.io/guides/writing-native-applications-tips
· Read txt file from resources folder on maven Quarkus project From Docker Container

bprjcwpo

bprjcwpo1#

首先修复json

{
    "resources": [
      {
        "pattern": "Resource2.txt"
      }
    ]
}

或者你可以有 *.txt作为模式.喜欢在这文档
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/Resources.md表示使用

InputStream resource = ModuleLayer.boot().findModule(moduleName).getResourceAsStream(resourcePath);

当我尝试我有问题.你可以看到这工作代码下面为你的项目

@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";
    }

}

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] ------------------------------------------------------------------------
kqlmhetl

kqlmhetl2#

这是我如何解决问题-更改的示例在此分支中:https://github.com/sleski/hello-quarkus/tree/solution_for_native
说明:
图像位于:src/main/resources/images,名称为:在application.properties中,我添加了images.location变量:

images.location=src/main/resources/images/
%prod.images.location=/work/images/

在java控制器中我添加了:

@ConfigProperty(name = "images.location")
String imageLocation;

docker.native中添加:COPY target/classes/images/*.jpg /work/images/
当我用querkus:dev启动应用程序时,它从src/main/resources/images获取图像,当我运行narive image时:/work/images.
在这两种情况下均有效:File size is = 3412177.

相关问题