gpt4 book ai didi

java - Docker-Compose:如何健康检查 OpenJDK:8 容器?

转载 作者:行者123 更新时间:2023-11-30 06:00:31 28 4
gpt4 key购买 nike

我正在尝试对扩展 openjdk:8 的容器进行运行状况检查。相应的Dockerfile可以在 https://github.com/jhipster/jhipster-registry/blob/master/Dockerfile 找到。

我有以下目录结构:

test/
├── central-server-config
│   └── application.yml
├── docker-compose.yml
├── jhipster-registry.yml
└── Ping.jar

application.yml

#common configuration shared between all applications
configserver:
name: Docker JHipster Registry
status: Connected to the JHipster Registry running in Docker

jhipster:
security:
authentication:
jwt:
secret: 3ac0a39ed9a2a58ca74d9d36c5227e51225480e2

eureka:
client:
service-url:
defaultZone: http://admin:${jhipster.registry.password}@jhipster-registry:8761/eureka/

docker-compose.yml

version: '2.1'
services:
jhipster-registry:
extends:
file: jhipster-registry.yml
service: jhipster-registry
mem_limit: 512m
ports:
- 8761:8761
healthcheck:
test: ["CMD", "java", "-jar", "Ping.jar", "localhost", "8761"]
interval: 30s
retries: 10

jhipster-registry.yml

version: '2.1'
services:
jhipster-registry:
image: jhipster/jhipster-registry:v4.0.2
volumes:
- ./central-server-config:/central-config
# By default the JHipster Registry runs with the "dev" and "native"
# Spring profiles.
# "native" profile means the filesystem is used to store data, see
# http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html
environment:
- SPRING_PROFILES_ACTIVE=dev,native
- SECURITY_USER_PASSWORD=password
- JHIPSTER_REGISTRY_PASSWORD=password
# - GIT_URI=https://github.com/jhipster/jhipster-registry/
# - GIT_SEARCH_PATHS=central-config
ports:
- 8761:8761

Ping.java

package ping;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Main {
public static void main(String[] args) {
if (args.length != 2) {
System.exit(-1);
}

String host = args[0];
int port = 0;

try {
port = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
e.printStackTrace();
System.exit(-2);
}

try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(host, port), 10 * 1000);
System.exit(0);
} catch (IOException e) {
System.exit(1);
}
}
}

我通过运行来启动容器

docker-compose up -d --force-recreate

在测试目录中,我可以通过浏览器访问jhipster-registry。也可以通过上面的jar程序访问:

izio@1z10:~$ cd Desktop/
izio@1z10:~/Desktop$ java -jar Ping.jar localhost 8761
izio@1z10:~/Desktop$ echo $?
0
izio@1z10:~/Desktop$

返回 0,表示它可以连接到服务。

如果有人想知道,我也尝试使用 curl 但没有成功(几乎可以肯定,docker 镜像中没有提供它来减小大小),所以我切换到这个 Java > 片段希望没有问题,因为它是 openjdk...

使用此配置,jhipster-registry 在大约 3 分钟内启动,但无论我等待多少时间,它总是显示为 health running ,最后显示为 unhealthy (之后10 x 30'' = 300'' = 5')。

您知道此健康检查有什么问题吗?我提供了我的最小的不起作用的示例,以便任何人都可以对其进行测试。我需要进行此检查,以便能够启动另一个服务,该服务仅在准备好接受连接时才需要连接到注册表,而不仅仅是在容器启动后,否则应用程序将失败并停止,除非我设置restart: on-failure,但我更愿意仅在其他服务启动时启动它,而不是不断重新启动,希望找到注册表就绪。

最佳答案

此处的问题是 Jar 文件在容器的文件系统 上不可用。为了使其可用,我们需要按以下方式使用卷:

jhipster-registry:
extends:
file: jhipster-registry.yml
service: jhipster-registry
mem_limit: 512m
ports:
- 8761:8761
networks:
- backend
volumes:
- ${PWD}/Ping.jar:/Ping.jar
healthcheck:
test: ["CMD", "java", "-jar", "/Ping.jar", "localhost", "8761"]
interval: 30s
retries: 20

并从根目录 (/) 开始引用 Jar 文件。

关于java - Docker-Compose:如何健康检查 OpenJDK:8 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52347152/

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