KafkaJS:尝试生成有关主题的消息时 ECONNREFUSED
KafkaJS: ECONNREFUSED when trying to produce a message on a topic
我正在使用 KafkaJS 生成有关 Kafka 主题的消息。
为此,我使用 wurstmeister
图像将 Kafka 服务器放在 Docker 中。
我想做什么:Poll
容器向 Poll
主题生成消息并使用来自 responsePoll
主题的消息。但是我在尝试生成消息时出错
错误:poll | {"level":"ERROR","timestamp":"2020-10-24T15:21:27.113Z","logger":"kafkajs","message":"[Connection] Connection error: connect ECONNREFUSED 127.0.0.1:9092","broker":"127.0.0.1:9092","clientId":"BlueOriginX","stack ":"Error: connect ECONNREFUSED 127.0.0.1:9092\n at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)"}
这是 docker-compose.yml
文件:
version: '3.4'
services:
poll:
container_name: poll
build: ./Services/Poll
ports:
- "4003:4003"
networks:
- blueorigin
zookeeper:
image: wurstmeister/zookeeper:latest
ports:
- "2181:2181"
networks:
- blueorigin
kafka:
image: wurstmeister/kafka:2.11-1.1.1
ports:
- "9092:9092"
links:
- zookeeper
environment:
KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
KAFKA_DELETE_TOPIC_ENABLE: 'true'
KAFKA_CREATE_TOPICS: "Poll:1:1,responsePoll:1:1"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- blueorigin
networks:
blueorigin:
我想错误来自我的 docker 设置,但我不知道 :/
编辑:
这是生成消息的代码:
const startPoll = async () =>{
const producer = kafka.producer()
await producer.connect()
await producer.send({
topic: 'Poll',
messages: [
{ value: 'New Poll' },
],
})
await producer.disconnect()
}
我没有您的投票应用程序,因此我将其替换为 Confluent Rest Proxy,但此设置有效。我可以在此处查看主题 http://localhost:8086/topics
。
version: '3.4'
services:
restproxy:
image: confluentinc/cp-kafka-rest:latest
ports:
- "8086:8086"
environment:
KAFKA_REST_HOST_NAME: restproxy
KAFKA_REST_BOOTSTRAP_SERVERS: PLAINTEXT://kafka:9092
KAFKA_REST_LISTENERS: http://0.0.0.0:8086
zookeeper:
image: wurstmeister/zookeeper:latest
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka:2.11-1.1.1
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
KAFKA_DELETE_TOPIC_ENABLE: 'true'
KAFKA_CREATE_TOPICS: "test:1:1"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
您不需要它们是遗留的链接和网络。当您尝试从一项服务连接到另一项服务时,使用 localhost 也不起作用,这可能就是它不适合您的原因。
我正在使用 KafkaJS 生成有关 Kafka 主题的消息。
为此,我使用 wurstmeister
图像将 Kafka 服务器放在 Docker 中。
我想做什么:Poll
容器向 Poll
主题生成消息并使用来自 responsePoll
主题的消息。但是我在尝试生成消息时出错
错误:poll | {"level":"ERROR","timestamp":"2020-10-24T15:21:27.113Z","logger":"kafkajs","message":"[Connection] Connection error: connect ECONNREFUSED 127.0.0.1:9092","broker":"127.0.0.1:9092","clientId":"BlueOriginX","stack ":"Error: connect ECONNREFUSED 127.0.0.1:9092\n at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)"}
这是 docker-compose.yml
文件:
version: '3.4'
services:
poll:
container_name: poll
build: ./Services/Poll
ports:
- "4003:4003"
networks:
- blueorigin
zookeeper:
image: wurstmeister/zookeeper:latest
ports:
- "2181:2181"
networks:
- blueorigin
kafka:
image: wurstmeister/kafka:2.11-1.1.1
ports:
- "9092:9092"
links:
- zookeeper
environment:
KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
KAFKA_DELETE_TOPIC_ENABLE: 'true'
KAFKA_CREATE_TOPICS: "Poll:1:1,responsePoll:1:1"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- blueorigin
networks:
blueorigin:
我想错误来自我的 docker 设置,但我不知道 :/
编辑: 这是生成消息的代码:
const startPoll = async () =>{
const producer = kafka.producer()
await producer.connect()
await producer.send({
topic: 'Poll',
messages: [
{ value: 'New Poll' },
],
})
await producer.disconnect()
}
我没有您的投票应用程序,因此我将其替换为 Confluent Rest Proxy,但此设置有效。我可以在此处查看主题 http://localhost:8086/topics
。
version: '3.4'
services:
restproxy:
image: confluentinc/cp-kafka-rest:latest
ports:
- "8086:8086"
environment:
KAFKA_REST_HOST_NAME: restproxy
KAFKA_REST_BOOTSTRAP_SERVERS: PLAINTEXT://kafka:9092
KAFKA_REST_LISTENERS: http://0.0.0.0:8086
zookeeper:
image: wurstmeister/zookeeper:latest
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka:2.11-1.1.1
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
KAFKA_DELETE_TOPIC_ENABLE: 'true'
KAFKA_CREATE_TOPICS: "test:1:1"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
您不需要它们是遗留的链接和网络。当您尝试从一项服务连接到另一项服务时,使用 localhost 也不起作用,这可能就是它不适合您的原因。