客户端太早连接到Kafka

客户端太早连接到Kafka

本文介绍了Docker(Compose)客户端太早连接到Kafka的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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等待 Dockerfile 中带有行 CMD dockerize -wait tcp:// kafka:9092节点的kafka端口可用/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在选举领导人之前就打开了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,因为其中包含指令。

请参阅 Docker例如,在开始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中添加:

healthcheck:
    test: ["CMD", ...]
    interval: 30s
    timeout: 10s
    retries: 5

卷曲命令将测试kafka是否选出了领导人。

with a curl command for instance which would test if kafka has elected a leader.

这篇关于Docker(Compose)客户端太早连接到Kafka的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!