奢望留念
2016-10-25 09:13
采纳率: 50%
浏览 4.9k
已采纳

kafka消费者迭代器卡死

配置kafka+zk在虚拟机上,自己在本机上的生产者可以正常发送消息,但是消费者在
ConsumerIterator iterator = stream.iterator();
之后 iterator 的任何操作都直接卡死了,请问这个是怎么回事?在这个操作之前debug是可以看到变量内部的数值的,这个操作之后就不能看了,value全部清空了。
贴源码

 package com.weixinjia.recreation.queue.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.message.MessageAndMetadata;

public class ConsumerGroupExample extends Thread{

    private final ConsumerConnector consumer;
    private final String topic;
    private  ExecutorService executor;

    public ConsumerGroupExample(String a_zookeeper, String a_groupId, String a_topic) {
        consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
                createConsumerConfig(a_zookeeper, a_groupId));
        this.topic = a_topic;
    }

    public void shutdown() {
        if (consumer != null) consumer.shutdown();
        if (executor != null) executor.shutdown();
        try {
            if (!executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) {
                System.out.println("Timed out waiting for consumer threads to shut down, exiting uncleanly");
            }
        } catch (InterruptedException e) {
            System.out.println("Interrupted during shutdown, exiting uncleanly");
        }
   }

    public void run(int a_numThreads) {
        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
        topicCountMap.put(topic, new Integer(a_numThreads));
        Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
        List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);

        // now launch all the threads
        //
//        executor = Executors.newFixedThreadPool(a_numThreads);
        System.out.println(streams.size());
        // now create an object to consume the messages
        //
        int threadNumber = 0;
        for (final KafkaStream<byte[], byte[]> stream : streams) {
            executor.submit(new ConsumerTest(stream, threadNumber));
            threadNumber++;
            ConsumerIterator<byte[], byte[]> iterator = stream.iterator();
            System.out.println(iterator);
            while(iterator.hasNext()){
                MessageAndMetadata<byte[], byte[]> next = iterator.next();
                byte[] message = next.message();
                String string = new String(message);
                System.out.println(string);
            }
        }
        System.out.println("消息输出完成");
    }

    private static ConsumerConfig createConsumerConfig(String a_zookeeper, String a_groupId) {
        Properties props = new Properties();
        props.put("zookeeper.connect", a_zookeeper);
        props.put("group.id", a_groupId);
        props.put("zookeeper.session.timeout.ms", "1500");
        props.put("zookeeper.sync.time.ms", "4000");
        props.put("auto.commit.interval.ms", "1000");
        props.put("fetch.message.max.bytes", "10240000");
        props.put("auto.commit.enable", "true");

        return new ConsumerConfig(props);
    }

    public static void main(String[] args) {
        String zooKeeper = "masterServce:2181";
        String groupId = "group-1";
        String topic = "test2";
        int threads = 3;

        ConsumerGroupExample example = new ConsumerGroupExample(zooKeeper, groupId, topic);
        example.run(threads);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException ie) {

        }
        example.shutdown();
    }
}


消息在kafka那边直接用kafka-console-consumer.sh是可以查询到的

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 奢望留念 2016-10-27 07:28
    已采纳

    明白了,offset已经指到最后了,然后获取不到数据就一直等待状态,我以为卡死,生产者发送几个数据进去就行了

    打赏 评论
  • devmiao 2016-10-25 09:45
    打赏 评论
  • oyljerry 2016-10-25 12:29

    是否消息太多。阻塞了。然后就是直接查看kafka上数据是否有

    打赏 评论
  • 奢望留念 2016-10-27 02:00

    对了,那个转换的时候转换之后所有的值都丢失了,有没有遇到这种问题,所有的引用都失效了

    打赏 评论

相关推荐 更多相似问题