kafka监听消费消息多个客户端实现广播模式

需求:多个客户端监听kafka,保证每个客户端都可以同时收到新增的消息。
环境:springboot框架、spring-kafka:1.3.2、kafka-clients:0.11.0.0
过程:一开始使用@kafkalistener(topics=“test”)来监听,但是发现如果服务器kafka中的topic只有一个partition的时候会出现只有一个客户端能消费到,这个的原因就是因为kafka的消费机制(同一个group下的消费者只能有一个消费者可以消费)但是@kafkalistener又无法根据不通客户端动态分配group,因此使用如下代码实现需求:

import java.util.Properties;
import java.util.UUID;

/**
 * @author shicz
 * @version 1.0
 * @date 2020/12/30 10:36
 */
@Configuration
public class KafkaConfig {
    private static final Logger log = LoggerFactory.getLogger(KafkaConfig.class);
    @Autowired
    private KafkaConsumerListener kafkaConsumerListener;

    public static KafkaConsumer<String, String> kafkaConsumer;

    @Bean
    public String loadKafkaConfig() {
        Properties p = new Properties();
        p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, PropertiesUtil.getValue("kafka.bootstrap-servers","10.19.159.132:9092"));
        p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, PropertiesUtil.getValue("kafka.key-deserializer","org.apache.kafka.common.serialization.StringDeserializer"));
        p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, PropertiesUtil.getValue("kafka.value-deserializer","org.apache.kafka.common.serialization.StringDeserializer"));
        p.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, PropertiesUtil.getValue("kafka.enable-auto-commit","true"));
        p.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, PropertiesUtil.getValue("auto.commit.interval.ms","1000"));
        p.put(ConsumerConfig.GROUP_ID_CONFIG, String.valueOf(System.currentTimeMillis()));//动态分配group
        p.put(ConsumerConfig.CLIENT_ID_CONFIG, String.valueOf(System.currentTimeMillis()+1000));//动态分配clientid

        kafkaConsumer = new KafkaConsumer<String, String>(p);
        kafkaConsumer.subscribe(Collections.singletonList(PropertiesUtil.getValue("kafka.consumer.topic","alarm")));// 订阅消息
        log.info("滨海告警消息订阅成功!kafka配置:" + p.toString());
        //启动消息监听线程
        KafkaListenerJob kafkaListenerJob = new KafkaListenerJob(kafkaConsumerListener);
        Thread t = new Thread(kafkaListenerJob);
        t.start();
        return null;
    }
}

import com.alibaba.fastjson.JSON;
import com.example.demo.service.KafkaConsumerListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author shicz
 * @version 1.0
 * @date 2020/12/30 10:37
 */
public class KafkaListenerJob implements Runnable {
    private static final Logger log = LoggerFactory.getLogger(KafkaListenerJob.class);
    private KafkaConsumerListener kafkaConsumerListener;

    //注入消息监听处理类
    public KafkaListenerJob(KafkaConsumerListener kafkaConsumerListener) {
        this.kafkaConsumerListener = kafkaConsumerListener;
    }

    @Override
    public void run() {
        log.info("滨海告警kafka消息监听任务已启动!");
        //进行消息监听
        while (true) {
            ConsumerRecords<String, String> records = KafkaConfig.kafkaConsumer.poll(100);
            for (ConsumerRecord<String, String> record : records) {
                try {
                    kafkaConsumerListener.listen(record);
                } catch (Exception e) {
                    log.error("滨海告警消息消费异常!", e);
                }
            }
        }
    }
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.*;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author shicz
 * @version 1.0
 * @date 2020/12/30 10:37
 */
@Component
public class KafkaConsumerListener {
    private static final Logger log = LoggerFactory.getLogger(KafkaListenerJob.class);
    /**
     * kafka消息处理类
     *
     * @param consumerRecord
     */
    public void listen(ConsumerRecord<String, String> consumerRecord) {
        try {
            String value = (String) consumerRecord.value();
        }catch (Exception e){
            log.error("监听报错:::::::::",e);
            e.printStackTrace();
        }
    }
}

这样就实现了多个客户端监听同一个topic实现广播模式。

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐