首页 > 其他 > 详细

Kafka多个消费者的小例子

时间:2020-11-29 23:03:57      阅读:31      评论:0      收藏:0      [点我收藏+]

Kafka多个消费者的小例子

public class FirstMultiConsumerThreadDemo2 {
    public static final String brokerList = "10.211.55.3:9092";
    public static final String topic = "topic-demo";
    public static final String groupId = "group.demo";

    public static Properties initConfig() {
        Properties props = new Properties();
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
//        props.put(ConsumerConfig.CLIENT_ID_CONFIG,"consumer.client.id.demo");
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,true);
        return props;
    }

    public static void main(String[] args) {
        Properties props = initConfig();
        KafkaConsumerThread consumerThread = new KafkaConsumerThread(props,topic,Runtime.getRuntime().availableProcessors());
        consumerThread.start();
    }

    public static class KafkaConsumerThread extends Thread {
        private KafkaConsumer<String,String> kafkaConsumer;
        private ExecutorService executorService;
        private int threadNumber;

        public KafkaConsumerThread(Properties props,String topic,int threadNumber) {
            this.kafkaConsumer = new KafkaConsumer<>(props);
            this.kafkaConsumer.subscribe(Collections.singletonList(topic));
            this.threadNumber = threadNumber;
            executorService = new ThreadPoolExecutor(threadNumber,threadNumber,0L, TimeUnit.MILLISECONDS,
                    new ArrayBlockingQueue<>(1000),new ThreadPoolExecutor.CallerRunsPolicy());
        }

        @Override
        public void run() {
            try {
                while (true) {
                    ConsumerRecords<String, String> records = kafkaConsumer.poll(Duration.ofMillis(100));
                    if (!records.isEmpty()) {
                        //将一批消息,即records封装成任务类,提交给线程池
                        //通常这一步最为耗时,通过异步的方式,降低处理业务逻辑所耗费的时间
                        executorService.submit(new RecordHandler(records));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                kafkaConsumer.close();
            }
        }
    }

    public static class RecordHandler extends Thread{
        public final ConsumerRecords<String,String> records;
        public RecordHandler(ConsumerRecords<String,String> records){
            this.records = records;
        }

        @Override
        public void run() {
            //处理records
        }
    }


}

RecordHandler类是用来处理消息的,注意线程池的最后一个参数设置的是:CallerrunsPolicy,这样可以防止线程池的总体消费能力跟不上poll()的能力,从而导致异常现象的发生。

一般而言,poll()拉取消息的速度是相当快的,而整体消费的瓶颈也正是在处理消息这一块,如果我们通过异步的方式,就能 带动整体消费性能的提升。

如下图:

技术分享图片

将处理消息改成多线程的实现方式。

Kafka多个消费者的小例子

原文:https://www.cnblogs.com/heliusKing/p/14057721.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!