Docker (Compose) 客户端过早连接到 Kafka [英] Docker (Compose) client connects to Kafka too early
问题描述
我正在尝试使用 Docker 和 Docker Compose 运行 Kafka.这是docker-compose.yml
:
I am trying to run Kafka with Docker and Docker Compose. This is the docker-compose.yml
:
version: "2"
services:
zookeeper:
image: "wurstmeister/zookeeper"
ports:
- "2181:2181"
kafka:
build:
context: "./services/kafka"
dockerfile: "Dockerfile"
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: "0.0.0.0"
KAFKA_CREATE_TOPICS: "test:1:1"
KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
users:
build:
context: "./services/users"
dockerfile: "Dockerfile"
ports:
- "4001:4001"
environment:
NODE_ENV: "develop"
ZOOKEEPER_HOST: "zookeeper"
ZOOKEEPER_PORT: "2181"
volumes:
- "./services/users:/service"
用户服务仅尝试连接(使用 Node.js 中的 kafka-node)并侦听主题并在每次运行时向其发布一条消息.
The users service only tries to connect (using kafka-node in Node.js) and listens on a topic and publishes one message to it every time it is ran.
问题是我不断收到连接被拒绝的错误.我正在使用 Dockerize 等待 kafka 端口在 Dockerfile
中可用,行 CMD dockerize -wait tcp://kafka:9092 node/service/index.js
.
The problem is that I keep getting Connection Refused errors. I am using Dockerize to wait for the kafka port to be available in the Dockerfile
with the line CMD dockerize -wait tcp://kafka:9092 node /service/index.js
.
它在启动用户容器之前等待端口可用并且该系统可以工作,但它不是在正确的时间.看来Kafka在选举出leader之前就打开了9092端口.
It waits for the port to be available before starting the users container and this system works, but it is not at the right time. It seems that Kafka is opening the 9092 port before it has elected a leader.
当我先运行 Kafka 并让它完全启动然后再运行我的应用程序时,它运行流畅.
When I run Kafka first and let it start completely and then run my app, it runs smoothly.
如何在开始服务之前等待正确的时间?
How do I wait for the correct moment before starting my service?
推荐答案
尝试 docker-compose 2.1 或 3 版,因为它包含一个 healthcheck
指令.
请参阅Docker Compose 在启动 Y 之前等待容器 X"作为示例.
Try the docker-compose version 2.1 or 3, as it includes an healthcheck
directive.
See "Docker Compose wait for container X before starting Y" as an example.
您可以:
depends_on:
kafka:
condition: service_healthy
并在 kafka 中添加:
And in kafka add:
healthcheck:
test: ["CMD", ...]
interval: 30s
timeout: 10s
retries: 5
例如使用 curl
命令来测试 kafka 是否选举了领导者.
with a curl
command for instance which would test if kafka has elected a leader.
这篇关于Docker (Compose) 客户端过早连接到 Kafka的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!