这是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)
给我错误'经纪人不可用'。有人可以告诉我我的设置有什么问题吗?
答案 0 :(得分:0)
注意该行:
const client = new kafka.Client("<machines private ip>:2181");
这不是Kafka正在监听的端口。 Kafka正在监听端口9092上的连接:
const client = new kafka.Client("<machines private ip>:9092");
此更改后应该可以使用。