卡夫卡消费者无法使用eclipse

时间:2015-06-26 03:17:09

标签: apache-zookeeper apache-kafka

Kafka 0.8.2.2.3和zookeper都在VM内部运行。我能够分别使用kafka-console-producer.sh和kafka-console-consumer.sh成功运行VM中的生产者和消费者。甚至我也可以使用kafka-console-consumer.sh从主机上使用Kafka消息。但是当我尝试使用eclipse中的java运行使用者时,zookeeper会记录错误

2015-06-26 03:06:26,323 - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1007] - Closed socket connection for client /192.168.1.12:59549 (no session established for client)
2015-06-26 03:07:26,225 - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@197] - Accepted socket connection from /192.168.1.12:59617
2015-06-26 03:07:26,226 - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@357] - caught end of stream exception
EndOfStreamException: Unable to read additional data from client sessionid 0x0, likely client has closed socket
    at org.apache.zookeeper.server.NIOServerCnxn.doIO(NIOServerCnxn.java:228)
    at org.apache.zookeeper.server.NIOServerCnxnFactory.run(NIOServerCnxnFactory.java:208)
    at java.lang.Thread.run(Thread.java:745)

以下是我的Kafka消费代码

package com.truckevent.producer;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;


public class KafkaConsumer {

    public static void main(String[] args) throws Exception {

        String group = "hello" ;


        Properties props = new Properties();
        props.put("zookeeper.connect", "192.168.1.12:2181");
        props.put("group.id", group);
        props.put("zookeeper.session.timeout.ms", "20000");
        props.put("zookeeper.sync.time.ms", "2030");
        props.put("auto.commit.interval.ms", "10000");
        props.put("auto.offset.reset", "smallest");

        ConsumerConfig cf = new ConsumerConfig(props) ;

        ConsumerConnector consumer = Consumer.createJavaConsumerConnector(cf) ;

        String topic = "event" ;

        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
        topicCountMap.put(topic, new Integer(1));
        Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
        List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);


        KafkaStream<byte[],byte[]> stream = streams.get(0) ;

        ConsumerIterator<byte[], byte[]> it = stream.iterator();
        int i = 1 ;
        while (it.hasNext()) {

            System.out.println(i + ": " + new String(it.next().message()));
            ++i;
        }
        consumer.shutdown(); 
    }
}

我不知道为什么我无法使用java代码中的消息。 Kafka在2181年在6667号港口和动物园管理员处运行。

1 个答案:

答案 0 :(得分:0)

  • 请检查zookeeper是否绑定(netstat -lntp)到0.0.0.0或仅限于localhost(然后它只接受来自vm本身的连接),这是一个相当新的选项(clientPortBindAddress,检查你是否指定了它,默认情况下,如果您不指定它,它将绑定到所有地址)
  • 它可能连接到您的vm配置。例如。在我使用vagrant + virtual box的情况下,我使用config.vm.network在private和network机器之间创建专用网络:private_network,ip:192.168.1.12
  • 此外 - 检查您的消费者主体是否不会抛出一些可能连接的异常,并提供一些额外的信息(您将异常抛到最高级别)