问题是这样的,
我在服务器上跑了一个 Kafka,是跑的 wurstmeister/kafka 镜像,使用的默认网络。配置:
kafka:
image: wurstmeister/kafka
container_name: kafka
environment:
- KAFKA_ADVERTISED_PORT=port
- KAFKA_ADVERTISED_HOST_NAME=公网 ip
- KAFKA_ZOOKEEPER_CONNECT=本机内网 ip:zookeeper port
- KAFKA_LOG_RETENTION_HOURS=4
- KAFKA_LOG_RETENTION_BYTES=1000000000
ports:
- port:9092
然后在同一台服务器,使用 docker run 命令,启动了另外一个容器,使用的是 node-alpine 镜像,内部通过 kafka-node 库连接 kafka,怎么都连不上。
启动 node 容器后,进入容器,telnet 命令可以连上 Kafka 容器的端口。
在本机,不在容器内部,同一个连接(公网 ip:port )可以正常连上 Kafka。
我在本机装了 Conduktor 这个 Kafka 客户端,也能连上 Kafka,可以正常收发消息。
有什么思路吗?谢谢!
启动 kafka,然后启动 node,看 node 镜像的日志就会卡住。然后 stop Kafka,node 这边就会报错:
events.js:174
throw er; // Unhandled 'error' event
^
NestedError: refreshBrokerMetadata failed
at async.waterfall (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:378:35)
at /app/node_modules/daoqun-core/node_modules/kafka-node/node_modules/async/dist/async.js:473:16
at next (/app/node_modules/daoqun-core/node_modules/kafka-node/node_modules/async/dist/async.js:5329:29)
at /app/node_modules/daoqun-core/node_modules/kafka-node/node_modules/async/dist/async.js:969:16
at KafkaClient.getAvailableBroker (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:330:12)
at async.waterfall.callback (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:373:23)
at nextTask (/app/node_modules/daoqun-core/node_modules/kafka-node/node_modules/async/dist/async.js:5324:14)
at Object.waterfall (/app/node_modules/daoqun-core/node_modules/kafka-node/node_modules/async/dist/async.js:5334:5)
at KafkaClient.refreshBrokerMetadata (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:372:9)
at Immediate._onImmediate (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:782:20)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
Caused By: Error: Unable to find available brokers to try
at KafkaClient.getAvailableBroker (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:330:21)
at async.waterfall.callback (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:373:23)
at nextTask (/app/node_modules/daoqun-core/node_modules/kafka-node/node_modules/async/dist/async.js:5324:14)
at Object.waterfall (/app/node_modules/daoqun-core/node_modules/kafka-node/node_modules/async/dist/async.js:5334:5)
at KafkaClient.refreshBrokerMetadata (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:372:9)
at Immediate._onImmediate (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:782:20)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
Emitted 'error' event at:
at KafkaClient.<anonymous> (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/baseProducer.js:101:10)
at KafkaClient.emit (events.js:198:13)
at async.waterfall (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:378:21)
at /app/node_modules/daoqun-core/node_modules/kafka-node/node_modules/async/dist/async.js:473:16
[... lines matching original stack trace ...]
at KafkaClient.refreshBrokerMetadata (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:372:9)
at Immediate.<anonymous> (/app/node_modules/daoqun-core/node_modules/kafka-node/lib/kafkaClient.js:782:20)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
1
Xbluer 2020-01-12 16:46:08 +08:00
docker run 启动 node-alpine 的时候添加这个参数 --link=kafka
|
2
Maco 2020-01-12 17:07:38 +08:00
把 Kafka 和 node 写在一个 docker-compose 中,使用一个网络,node 连接使用 container_name: kafka+端口连接。
|
3
Maco 2020-01-12 17:09:06 +08:00
根据你的描述,最大的可能就是 node 和 kafka 不在一个网络中
|
4
ilaipi OP @Xbluer #1 谢谢~我试了加了 link,启动命令改成:
docker run --env-file .env --env NODE_REPO=mr -p 11111:11111 -dit --restart always --name=mr_dev --link=kafka:kafka --net middleware_default mr:0.0.1 然后连接地址是 kafka:9092 还是一样的反应。我进入 node 容器,telnet kafka 9092 也是能通.. 无奈了 |
5
ilaipi OP @Maco #3 我也怀疑是网络问题,但是明明在 node 容器里都能 telnet kafka 端口。。。搞了两天了,唉
|
6
ilaipi OP 抱歉了,程序是启动成功了。。
只是程序启动成功打的一句日志,记到日志文件里,所以 docker logs 命令看不到,我一直以为是卡住了。。。。 两天时间啊。。。 |