gpt4 book ai didi

maven - jib-maven spring boot 配置文件

转载 作者:行者123 更新时间:2023-12-02 20:34:44 25 4
gpt4 key购买 nike

在我的springboot项目中,一共有三个配置文件

application.yml --> used in local development
application-test.yml --> running in a test environment
application-prod.yml --> running in a production environment

pom.xml

...

<properties>
<docker-repository>self-ip:port</docker-repository>
</properties>

...

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.7.0</version>
<configuration>
<allowInsecureRegistries>true</allowInsecureRegistries>
<from>
<image>openjdk:8-jre-alpine</image>
</from>
<to>
<image>${docker-repository}/${project.groupId}/${project.artifactId}</image>
</to>
<container>
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
<jvmFlags>
<jvmFlag>$JAVA_OPTS</jvmFlag>
<jvmFlag>-Dfile.encoding=UTF-8</jvmFlag>
<jvmFlag>-XX:+UseG1GC</jvmFlag>
<jvmFlag>-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -Xloggc:/var/log/gc.log</jvmFlag>
<jvmFlag>-Djava.security.egd=file:/dev/./urandom</jvmFlag>
</jvmFlags>
</container>
</configuration>
</plugin>
</plugins>
</build>

然后推送到我的私有(private)docker存储库,就可以了

mvn -DsendCredentialsOverHttp=true jib:build

在不同机器上运行

# On my test machine, I want to run like this
docker run --name test-xxx -d \
-p 8080:8080 -p 9080:9080 \
-e JAVA_OPTS="-Xms256m -Xmx256m -Dspring.profiles.active=test -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9080" \
-v ~/logs:/logs \
self-ip:port/xxx/yyy

# on my production machine, I want to run like this
docker run --name prod-xxx -d \
-p 8080:8080 \
-e JAVA_OPTS="-server -Xms1G -Xmx1G -Dspring.profiles.active=prod" \
-v /mnt/logs:/logs \
self-ip:port/xxx/yyy

当我在测试机上运行时,失败

docker container ls -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
XXXXXXXXXXXX xxx "java $JAVA_OPTS -Df…" 3 seconds ago Exited (1) 2 seconds ago test-xxx

我的期望是这样的

# test machine
java -Xms256m -Xmx256m -Dspring.profiles.active=test \
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9080 \
-Dfile.encoding=UTF-8 -XX:+UseG1GC -verbose:gc \
-XX:+PrintGCDetails -XX:+PrintGCDateStamps \
-XX:+PrintGCTimeStamps -Xloggc:/logs/gc.log \
-Djava.security.egd=file:/dev/./urandom \
-cp /app/resources:/app/classes:/app/libs/* \
xxx.yyy.MainClass

# production machine
java -server -Xms1G -Xmx1G -Dspring.profiles.active=prod \
-Dfile.encoding=UTF-8 -XX:+UseG1GC -verbose:gc \
-XX:+PrintGCDetails -XX:+PrintGCDateStamps \
-XX:+PrintGCTimeStamps -Xloggc:/logs/gc.log \
-Djava.security.egd=file:/dev/./urandom \
-cp /app/resources:/app/classes:/app/libs/* \
xxx.yyy.MainClass

我应该如何配置它? container --> environmentdockerClient --> environment
我在文档中没有找到相关的例子,都只是一句话

<小时/>

更新1

嗨@Haran,我修改了<jvmFlag>$JAVA_OPTS</jvmFlag>JAVA_TOOL_OPTS并拒绝

并运行测试

docker container rm test-xxx
docker run --name test-xxx -d \
-p 8080:8080 \
-e "JAVA_TOOL_OPTIONS=-Xms256m -Xmx256m -Dspring.profiles.active=test -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9080" \
ip:port/xx/yy

它也运行失败

$ docker container ls -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
XXXXXXXXXXXX xxx "java $JAVA_TOOL_OPTS -Df…" 3 seconds ago Exited (1) 2 seconds ago test-xxx

然后我删除 $JAVA_TOOL_OPTS 并重复上面的 repush 和 docker rm & run ,运行成功,但是没有自定义环境

$ docker exec -it test-xxx ps auxf
PID USER TIME COMMAND
1 root 0:05 java -Dfile.encoding=UTF-8 -XX:+UseG1GC -verbose:gc -XX:+P
59 root 0:00 ps auxf


$ docker ps --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xx yy "java -Dfile.encoding=UTF-8 -XX:+UseG1GC '-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -Xloggc:/logs/gc.log' -Djava.security.egd=file:/dev/./urandom -cp /app/resources:/app/classes:/app/libs/* xx.yy.MainClass" 34 seconds ago Up 34 seconds 0.0.0.0:8080->8080/tcp test-xxx
<小时/>

更新2

docker -e ... 的引号内部和外部的命令没有区别。

$ docker run --name test-xxx -d -p 8080:8080 \
-e JAVA_TOOL_OPTIONS="-Xms256m -Xmx256m -Dspring.profiles.active=test -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9080" \
ip:port/xx/yy
xxxxx

$ docker exec -it test-xxx env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
HOSTNAME=4a7c5045109b
TERM=xterm
JAVA_TOOL_OPTIONS=-Xms256m -Xmx256m -Dspring.profiles.active=test -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9080
JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/jre
JAVA_VERSION=8u212
JAVA_ALPINE_VERSION=8.212.04-r0
LANG=C.UTF-8
HOME=/root


$ docker run --name test-xxx1 -d -p 8081:8080 \
-e "JAVA_TOOL_OPTIONS=-Xms256m -Xmx256m -Dspring.profiles.active=test -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9080" \
ip:port/xx/yy
xxxx

$ docker exec -it test-xxx1 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
HOSTNAME=ae333ad8836f
TERM=xterm
JAVA_TOOL_OPTIONS=-Xms256m -Xmx256m -Dspring.profiles.active=test -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9080
JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/jre
JAVA_VERSION=8u212
JAVA_ALPINE_VERSION=8.212.04-r0
LANG=C.UTF-8
HOME=/root


$ docker ps --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xxx ip:port/xx/yy "java -Dfile.encoding=UTF-8 -XX:+UseG1GC -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -Xloggc:/var/log/gc.log -Djava.security.egd=file:/dev/./urandom -cp /app/resources:/app/classes:/app/libs/* xx.yy.MainClass" 3 minutes ago Up 3 minutes 0.0.0.0:8081->8080/tcp test-xxx1
xx ip:port/xx/yy "java -Dfile.encoding=UTF-8 -XX:+UseG1GC -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -Xloggc:/var/log/gc.log -Djava.security.egd=file:/dev/./urandom -cp /app/resources:/app/classes:/app/libs/* xx.yy.MainClass" 3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/tcp test-xxx

最佳答案

要设置JVM参数,只需设置 JAVA_TOOL_OPTIONS在运行时。实际上,我从您的更新中可以看出您已经尝试过此操作。

$ docker run ... -e "JAVA_TOOL_OPTIONS=..."

大多数 JVM 会自动获取 JAVA_TOOL_OPTIONS 环境变量,因此您无需传递 JAVA_TOOL_OPTIONS 中的内容。作为 java 的命令行参数二进制。您可以在您的计算机上本地验证此行为(容器内部或外部):

$ JAVA_TOOL_OPTIONS=-Xmx256m java ABCD # no need to pass extra arguments
Picked up JAVA_TOOL_OPTIONS: -Xmx256m
Error: Could not find or load main class ABCD
$ docker run -e "JAVA_TOOL_OPTIONS=-Xmx256m" --entrypoint java openjdk:11 ABCD
Picked up JAVA_TOOL_OPTIONS: -Xmx256m
Error: Could not find or load main class ABCD
Caused by: java.lang.ClassNotFoundException: ABCD

所以,只需定义 JAVA_TOOL_OPTIONS在运行时(并且您已经使用 docker run -e 完成了它),并且不定义 <jvmFlags>或修改<entrypoint> .

最后是JAVA_TOOL_OPTIONS ,不是JAVA_TOOL_OPTS .

关于maven - jib-maven spring boot 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58703508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com