卡夫卡生产者docker无法连接到代理docker-AWS

时间:2019-06-26 08:42:26

标签: docker apache-kafka

这是yml文件,用于在AWS实例中为kafka和zookeeper调出Docker容器:

version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
    - "2181:2181"
  kafka:
    build: .
    ports:
    - "9092:9092"
  environment:
    KAFKA_ADVERTISED_HOSTNAME: <machines private ip>
    KAFKA_LISTENERS: PLAINTEXT://:9092
    KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://<machines private ip>:9092


    KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock

当我使用上述文件运行docker-compose命令时,将导致创建一个名为kafka-docker的带有kafka容器和zookeeper容器的docker网络。

现在,在默认的bridge码头工人网络中,我有另一个容器,其中包含以下nodejs代码:

const Producer = kafka.Producer;
const client = new kafka.Client("<machines private ip>:2181");

const producer = new Producer(client);
const kafka_topic = 'hello-topic';

event = ...
event_payload = ...
let payloads = [{topic:kafka_topic ,messages:JSON.stringify(event_payload),  partition: 0 }]
let push_status = producer.send(payloads, (err, data) => {
       if (err) {
            console.log(err);
       } else {
            console.log('[kafka-producer -> '+kafka_topic+']: broker update success');
            }
        });

console.log(err)给我错误'经纪人不可用'。有人可以告诉我我的设置有什么问题吗?

1 个答案:

答案 0 :(得分:0)

注意该行:

const client = new kafka.Client("<machines private ip>:2181");

这不是Kafka正在监听的端口。 Kafka正在监听端口9092上的连接:

const client = new kafka.Client("<machines private ip>:9092");

此更改后应该可以使用。