Kafka Producer(0.8.2) - 如何跟踪经纪人何时关闭?

时间:2015-08-17 18:03:26

标签: apache-kafka kafka-consumer-api

感谢您对此问题的帮助。

我正在使用Kafka 0.8.2

这是我写过的制作人代码。

问题是一切正常。我能够在成功发布后发布消息并获得确认(元数据)。 但问题是......

  1. 如何跟踪kafka经纪人是否已关闭。我需要显示错误消息如果代理已关闭。

  2. 如果经纪人关闭,我需要捕获同时发布的消息,直到经纪人处于关闭状态。运行

  3. 任何见解都会非常有用。

    public class cvsKafkaProducerAck {
    public static void main(String args[]) {
    
    
        String topic = "test_topic";
        String msg = "test...22";
    
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.RETRIES_CONFIG, "1");
        props.put(ProducerConfig.ACKS_CONFIG, "all");
        //props.put(ProducerConfig.METADATA_FETCH_TIMEOUT_CONFIG , "1");
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, 1);
        props.put(ProducerConfig.BLOCK_ON_BUFFER_FULL_CONFIG, true);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer");
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer");
    
        KafkaProducer<byte[], byte[]> m_kafkaProducer = new KafkaProducer<byte[], byte[]>(props);       
        ProducerRecord<byte[], byte[]> prMessage = new ProducerRecord<byte[],byte[]>(topic, msg.getBytes());
    
        try
        {
            RecordMetadata metadata = m_kafkaProducer.send(prMessage).get();
            System.out.println("The offset of the record we just sent is: " + metadata.offset());
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    
        m_kafkaProducer.close();
    
    }
    

    }

2 个答案:

答案 0 :(得分:0)

我们有一个使用node-zookeeper-client库的Kafka代理监控系统。

经纪人在Zookeeper节点/brokers/ids上注册。使用client.getChildren()功能,我们会不断监控寻找添加或删除代理的路径。 client.getChildren()函数可以在Zookeeper节点上放置一个观察者,当添加或删除子节点时,该节点将触发事件。

我们的节点应用程序从/brokers/ids路径获取启动时的已知代理,然后持续监视路径以进行更改,在添加或删除代理时发出警报。这是通过使用lodash库比较当前代理的数组与最后检索的代理列表的数组来完成的。

&#13;
&#13;
//this code is fired when the /brokers/ids/ path changes (i.e. a child node is added or removed)

//currentBrokers = list of brokers retrieved when the app started
//newBrokerList = latest list of brokers retrieved from client.getChildren()
var brokersChanged = _.difference(currentBrokers, newBrokerList); 

  //if 'brokersChanged' contains any values alert that
  //these brokers have become unregistered with zookeeper
  if(brokersChanged.length > 0){
    _.each(brokersChanged, function(broker){
      var indexToRemove = _.findIndex(currentBrokers, function(existingBroker) {
        if(existingBroker == broker){
          return existingBroker;
        }
      });

      currentBrokers.splice(indexToRemove,1);
    });    
  }
  else {
    //if 'brokersChanged' is empty a new broker has been registered
    //with zookeeper
    var newBroker = _.difference(data, currentBrokers);
    _.each(newBroker, function(broker){
      currentBrokers.push(broker);
    });
  }

//currentBrokers will now contain the updated list of brokers
&#13;
&#13;
&#13;

希望这有帮助

答案 1 :(得分:0)

您只需检查Kafka服务器上可用的JMX指标,以查看Kafka代理是否已关闭。检查每个kafka代理ReplicaManager.LeaderCount值,如果它是0,则该代理不是任何主题的主导者,无论是非活动/关闭。

您可以在此处找到更多可用指标 https://cwiki.apache.org/confluence/display/KAFKA/Available+Metrics