问题描述
我正在使用 spring-boot-maven-plugin 通过以下命令从我的应用程序构建 docker 映像:
@H_502_5@spring-boot::build-image
我需要使用这个 jvm 参数更改 jvm 直接内存的默认大小(默认为 10m):
@H_502_5@-XX:MaxDirectMemorySize=64M
这是我在应用程序的 @H_502[email protected] 中不顾一切地尝试这样做的尝试,我正在尝试我想到的一切:
@H_502_5@<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <configuration> <jvmArguments>-XX:MaxDirectMemorySize=64M</jvmArguments> <environmentvariables> <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS> <JAVA_OPTS>-XX:MaxDirectMemorySize=64M</JAVA_OPTS> </environmentvariables> <systemPropertyVariables> <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS> <JAVA_OPTS>-XX:MaxDirectMemorySize=64M</JAVA_OPTS> </systemPropertyVariables> </configuration> </execution> </executions> <configuration> <layers> <enabled>true</enabled> </layers> <image> <name>docker.io/example/${project.artifactId}:${project.version}</name> </image> <excludeDevtools>true</excludeDevtools> <systemPropertyVariables> <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS> <JAVA_OPTS>-XX:MaxDirectMemorySize=64M</JAVA_OPTS> </systemPropertyVariables> <environmentvariables> <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS> <JAVA_OPTS>-XX:MaxDirectMemorySize=64M</JAVA_OPTS> </environmentvariables> <jvmArguments>-XX:MaxDirectMemorySize=64M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005</jvmArguments> </configuration> </plugin>
但是直接内存仍然设置为 10M :( 当应用程序启动时,我可以在日志文件中看到:
@H_502_5@Picked up JAVA_TOOL_OPTIONS: -Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties -agentpath:/layers/paketo-buildpacks_bellsoft-liberica/jvmkill/jvmkill-1.16.0-RELEASE.so=printHeapHistogram=1 -XX:ActiveProcessorCount=8 -XX:MaxDirectMemorySize=10M -Xmx11521920K -XX:MaxMetaspaceSize=144375K -XX:ReservedCodeCacheSize=240M -Xss1M -Dorg.springframework.cloud.bindings.boot.enable=true
谁能告诉我我做错了什么?
注意:
Spring Boot:2.3.7.RELEASE
当我使用 @H_502_5@-e 手动运行 docker 镜像时,jvm 参数的更改正在起作用:
@H_502_5@docker run -e JAVA_TOOL_OPTIONS=-XX:MaxDirectMemorySize=64M docker.io/example/app-name:0.0.1
这个 mvn 插件使用 Buildpack 创建 docker 镜像:
https://paketo.io/docs/buildpacks/language-family-buildpacks/java/#about-the-jvm
更新:
根据文档,这应该是可行的解决方案,但事实并非如此:
@H_502_5@ <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <layers> <enabled>true</enabled> </layers> <image> <name>docker.io/example/${project.artifactId}:${project.version}</name> <env> <JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</JAVA_TOOL_OPTIONS> </env> </image> </configuration>
解决方法
这是一个关于如何使运行时环境“粘性”的有效解决方案:
....
<image>
<name>docker.io/example/${project.artifactId}:${project.version}</name>
<env>
<BPE_APPEND_JAVA_TOOL_OPTIONS>-XX:MaxDirectMemorySize=64M</BPE_APPEND_JAVA_TOOL_OPTIONS>
<BPE_DELIM_JAVA_TOOL_OPTIONS xml:space="preserve"> </BPE_DELIM_JAVA_TOOL_OPTIONS>
</env>
</image>
...
spring-boot-maven-plugin 正在使用 paketo buildpacks:
文档: https://github.com/paketo-buildpacks/environment-variables
,对于在 Mac 上使用 Docker + Spring Boot 的构建映像的任何人,请注意,在 Mac 上编写 Docker 的默认设置时,它是一个 2GB 的 VM 映像,它运行 Linux,Docker 在此 Linux VM 中运行。
使用当前的 Spring 2.3.X 默认 Pareto 构建器,这导致 docker 映像有大约 300MB 可用,但根据内存计算器,相当简单的 spring 应用程序(对我而言)需要大约 600MB。
所以无论 //Add your obfucate code
//--no-build is needed
dotnet pack --no-build
//upload to nuget
nuget setapikey xxx -source xxxxxx
nuget push xx.nupkg -source xxxxxx
的值是多少,它都行不通。一旦我增加了 docker VM 可用的内存,它就可以正常工作