无法远程使用kafka事件错误:连接ECONNREFUSED 5.6.7.8:9092

时间:2018-05-04 11:05:48

标签: python node.js apache-kafka kafka-consumer-api apache-kafka-connect

我按照这里提到的教程设置了我的kafka制作人: https://www.digitalocean.com/community/tutorials/how-to-install-apache-kafka-on-ubuntu-14-04

我正在使用cron将一些事件推送到生产者,并在服务器上使用以下脚本:IP:1.2.3.4

#!/usr/bin/env python
import threading, logging, time
import multiprocessing
import requests
import datetime
import json
from kafka import KafkaProducer

class CheckApis():
    apisList = {"a": "https://test.eng.com/"}
    kafkaProducer = "1.2.3.4:9092"
    kafkaTopic = "sometopic"
    producer = KafkaProducer(bootstrap_servers=kafkaProducer)
    for key, value in apisList.items():
        headers = {};
        response = requests.request("GET", value, headers=headers)
        message = {"app_name": key, "status": response.status_code, "message": "none", "timestamp": str(datetime.datetime.utcnow())}
        producer.send(kafkaTopic, json.dumps(message).encode('utf-8'));
        print (response.text)
        print (response.status_code)
    producer.close()

这很好用,我可以使用这个命令看到推送的事件:

~/kafka/bin/kafka-console-consumer.sh --zookeeper 1.2.3.4:2181 --topic sometopic --from-beginning

但是当我尝试从其他服务器(我的笔记本电脑)远程使用这些事件时,它会失败并显示错误:

  

错误:收听kafka事件时发生错误错误:   连接ECONNREFUSED 5.6.7.8:9092(这里有一些不同的IP而不是1.2.3.4)

这是我的消费者代码(在节点js中使用kafka-node):

var ConsumerGroup = require('kafka-node').ConsumerGroup;
    var healthConsumerOption = {
        host: '1.2.3.4:2181',
        autoCommit: true,
        groupId: os.hostname(),
        sessionTimeout: 15000,
        protocol: ['roundrobin'],
        fromOffset: 'latest'
    };
    var healthConsumerGroup = new ConsumerGroup(healthConsumerOption, healthTopics);
        listenHealthEventsKafka: function(connections){
            try{
                healthConsumerGroup.on('error', onError);
                healthConsumerGroup.on('message', onMessage);
                healthConsumerGroup.on('connect', function(){
                    logger.info("Health consumer group is ready. ")
                });

                function onMessage(message){
                    var jsonData = JSON.parse(message.value);
                    console.log(message);
                };

                function onError(error){
                    logger.error("Some error occured while listening to kafka events " +error);
                }

                process.once('SIGNINT', function(){
                    async.each([healthConsumerGroup], function(consumer, callback){
                        logger.info("Closing the kafka health consumer process ");
                        consumer.close(true, callback);
                    });
                })
            }catch(error){
                logger.error("Could not connect to kafka events for build " +error);
            }
        }

我是否需要在Kafka服务器(server.properties)上进行其他配置以允许远程访问..或者我做错了什么?请帮忙。

1 个答案:

答案 0 :(得分:2)

在您的Kafka经纪人server.properties上,您需要将advertised.listeners设置为外部IP ,以便客户端可以正确连接到它。否则,他们会尝试连接到内部IP(因为advertised.listeners默认为listeners,除非明确设置)

参考:https://kafka.apache.org/documentation/#brokerconfigs