我正在运行一个zookeeper和kafka示例,它来自ubuntu18.04中的docker yaml文件
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
ports:
- "9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_CREATE_TOPICS: "test-topic:5:2"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
它的工作原理是
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3d84a6b39f7 wurstmeister/kafka "start-kafka.sh" 3 minutes ago Up 3 minutes 0.0.0.0:49157->9092/tcp desktop_kafka_1
b2012f08b3f9 wurstmeister/zookeeper "/bin/sh -c '/usr/sb…" 5 hours ago Up 3 minutes 22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp desktop_zookeeper_1
但是,Kafka客户端未能将Kafka连接为
const { Kafka,logLevel,CompressionCodecs,CompressionTypes } = require('kafkajs');
const kafka = new Kafka({
logLevel: logLevel.DEBUG,
brokers: ['localhost:9092'], // tried on ['192.168.1.6:9092']
clientId: 'example-producer',
})
const topic = 'topic-test'
const producer = kafka.producer()
const getRandomNumber = () => Math.round(Math.random(10) * 1000)
const createMessage = num => ({
key: `key-${num}`,
value: `value-${num}-${new Date().toISOString()}`,
})
const sendMessage = () => {
return producer
.send({
topic,
compression: CompressionTypes.GZIP,
messages: Array(getRandomNumber())
.fill()
.map(_ => createMessage(getRandomNumber())),
})
.then(console.log)
.catch(e => console.error(`[example/producer] ${e.message}`, e))
}
const run = async () => {
await producer.connect()
setInterval(sendMessage, 3000)
}
run().catch(e => console.error(`[example/producer] ${e.message}`, e))
代码输出
[example/producer] Connection error: connect ECONNREFUSED 127.0.0.1:9092 KafkaJSNonRetriableError
Caused by: KafkaJSConnectionError: Connection error: connect ECONNREFUSED 127.0.0.1:9092
at Socket.onError (/home/xsz/Desktop/node_modules/kafkajs/src/network/connection.js:152:23)
at Socket.emit (events.js:314:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
name: 'KafkaJSNumberOfRetriesExceeded',
retriable: false,
helpUrl: undefined,
originalError: KafkaJSConnectionError: Connection error: connect ECONNREFUSED 127.0.0.1:9092
at Socket.onError (/home/xsz/Desktop/node_modules/kafkajs/src/network/connection.js:152:23)
at Socket.emit (events.js:314:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
retriable: true,
helpUrl: undefined,
broker: 'localhost:9092',
code: 'ECONNREFUSED'
},
retryCount: 5,
retryTime: 10304
}
在docker-compose.ymal中,kafka\u播发的\u主机\u名称被配置为localhost或192.168.1.6(主机的本地ip地址),两者都显示与上面相同的错误。
注意:使用
ip地址显示命令输出
1: lo: <LOOPBACK,UP,LOWER_UP>
inet 127.0.0.1/8 scope host lo
3: wlx08570033e6c1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu
inet 192.168.1.6/24 brd 192.168.1.255 scope global noprefixroute wlx08570033e6c1
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
5: br-c66cb3672872: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu inet 172.18.0.1/16 brd 172.18.255.255 scope global br-c66cb3672872
22: br-521b1eb41768: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu
inet 172.19.0.1/16 brd 172.19.255.255 scope global br-521b1eb41768
最新尝试:
我修改了docker-compose.yaml
KAFKA_ADVERTISED_HOST_NAME: 172.17.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
在主机上运行代码。
const kafka = new Kafka({
logLevel: logLevel.DEBUG,
brokers: ['172.17.0.1:9092'], // tried on ['192.168.1.6:9092']
clientId: 'example-producer',
})
但仍然面临同样的问题,有什么不对
2条答案
按热度按时间vmpqdwk31#
在docker compose文件中,您没有正确Mapkafka示例的端口,您应该进行更改
具有
现在,容器被分配了一个任意端口
0.0.0.0:49157->9092/tcp
dtcbnfnu2#
需要做一些改变
首先,将连接主机更改为
localhost
```const kafka = new Kafka({
logLevel: logLevel.DEBUG,
brokers: ['localhost:9092'],
clientId: 'example-producer',
})
最后,在编写文件时只使用一个代理
我使用kafka-tool2.0查看您的配置,似乎只有一个分区用于“topictest”,这就是为什么您看到这个错误使用更多代理来避免这个错误
在linux mint 20上品尝
输出: