我正在尝试构建Micronaut(v1.0.4)应用程序的本机映像。
此应用程序将Consul用作服务发现。
我已经使用--features
选项创建了应用程序:
$ mn create-app my-app --features discovery-consul --features graal-native-image --build maven
该应用程序在我的本地计算机上可以正常运行,但是当我尝试使用本机映像构建docker容器时,出现错误:
$ ./docker-build.sh
error: No instances are allowed in the image heap for a class that is initialized or reinitialized at image runtime:
sun.security.provider.NativePRNG
Detailed message:
Error: No instances are allowed in the image heap for a class that is initialized or reinitialized at image runtime: sun.security.provider.NativePRNG
Trace: object java.security.SecureRandom
method com.sun.jndi.dns.DnsClient.query(DnsName, int, int, boolean, boolean)
Call path from entry point to com.sun.jndi.dns.DnsClient.query(DnsName, int, int, boolean, boolean):
at com.sun.jndi.dns.DnsClient.query(DnsClient.java:178)
at com.sun.jndi.dns.Resolver.query(Resolver.java:81)
at com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129)
at javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:142)
at io.micronaut.discovery.client.DnsResolver.getCNamesFromTxtRecord(DnsResolver.java:59)
at io.micronaut.discovery.client.EndpointUtil.getEC2DiscoveryUrlsFromZone(EndpointUtil.java:197)
at io.micronaut.discovery.client.EndpointUtil.getServiceUrlsFromDNS(EndpointUtil.java:141)
如果我删除Consul集成,它将正常工作。
我在官方文档中找不到任何有用的信息:
Microservices as GraalVM native images
有人知道问题出在哪里吗?
答案 0 :(得分:0)
浏览了多个问题和帖子后,我最终找到了答案。
要消除此故障,只需在com.sun.jndi.dns.DnsClient
中创建本机映像时,将--delay-class-initialization-to-runtime
类添加到选项Dockerfile
下的类列表中即可:
Dockerfile
RUN native-image --no-server \
...
--delay-class-initialization-to-runtime=...,com.sun.jndi.dns.DnsClient \
-H:-UseServiceLoaderFeature \
--allow-incomplete-classpath \
-H:Name=model-quotes \
-H:Class=model.quotes.Application
...
这样做之后,一切正常,并且成功生成了docker镜像。
最好在默认生成的Dockerfile中添加此类。使用Micronaut CLI生成新项目并发现不更改任何内容就无法使用本机映像会很烦人。