kafka动态创建消费者(实时更新topic和servers)

一、疑问描述
spring-kafka通过 @KafkaListener 的方式配置订阅的topic,通过@Configuration 配置创建kafkaListenerContainerFactory。
如下:

@Configuration
@EnableKafka
public class KafkaConfig {

    private static final String KAFKA_SERVERS_CONFIG = "10.192.77.202:9092";
    private static final String LOCAL_GROUP_ID = "test";

    @Bean
    ConcurrentKafkaListenerContainerFactory<Integer, String>
    kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }

    @Bean
    public ConsumerFactory<Integer, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_SERVERS_CONFIG);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, LOCAL_GROUP_ID);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        return props;
    }

    @Bean
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_SERVERS_CONFIG);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return props;
    }

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<String, String>(producerFactory());
    }

    @Bean
    public ProducerFactory<String, String> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    @KafkaListener(topics = "TEST_TOPIC_NEW")
    public void listen(String data) {
        System.out.println("kafkaconfig =listen======="+data);
    }
}

但想要动态的创建监听者对象,如通过数据库的方式配置KAFKA_SERVERS_CONFIG 和LOCAL_GROUP_ID ,并且可以不用重启服务,实现热更新。通过spring-kafka提供的接口没有找到好的解决方法。

二、解决方案
所以,考虑通过最基本的手动创建消费者对象。
通过定时任务,每三分钟check一次,从数据库读取相应配置,将已有配置写入缓存,当读取的配置和缓存不一致时,销毁已有消费者,创建新的消费者。
如果有好的方案,谢谢告知~

/**
 * 每三分钟check一次kafka配置
 * @throws Exception
 */
@Scheduled(cron = "1 1/3 * * * ? ")
public void deviceNotifyConfig(){
    Map<String, String> kafkaConfigs = systemConfigService.fetchConfigLikeKey("kafka");
    if(kafkaConfigs != null && kafkaConfigs.size() != 0)
    {
        String kafkaIp = kafkaConfigs.get("kafkaIp");
        String kafkaPort = kafkaConfigs.get("kafkaPort");
        String kafkaUserName = kafkaConfigs.get("kafkaUserName");
        String kafkaPassword = kafkaConfigs.get("kafkaPassword");
        if(StringUtils.isNotEmpty(KafkaLinkCache.kafkaConfigCache))
        {
            if (!KafkaLinkCache.kafkaConfigCache.equals(kafkaIp + "_" + kafkaPort))
            {
                //关闭已有消费者对象
                KafkaConsumer<String, String> consumer = KafkaLinkCache.DEVICE_CONSUMER_MAP.get("kafkaComsumer");
                if(consumer != null)
                {
                    resourceNotifyConsumer.closeConsumer();
                }
                KafkaLinkCache.DEVICE_CONSUMER_MAP.clear();
                this.handlerConsumer(kafkaIp, kafkaPort);
            }
        }
        else
        {
            this.handlerConsumer(kafkaIp, kafkaPort);
        }
    }else
    {
        //关闭已有消费者对象
        KafkaConsumer<String, String> consumer = KafkaLinkCache.DEVICE_CONSUMER_MAP.get("kafkaComsumer");
        if(consumer != null)
        {
            resourceNotifyConsumer.closeConsumer();
        }
        KafkaLinkCache.DEVICE_CONSUMER_MAP.clear();
    }
}

private void handlerConsumer(String kafkaIp, String kafkaPort) {
    Properties props = new Properties();
    props.setProperty("bootstrap.servers", kafkaIp + ":" + kafkaPort);
    // key反序列化
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    // value反序列化
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    // 每个消费者都必须属于某一个消费组,所以必须指定group.id
    props.put("group.id", "test");

    // 构造消费者对象
    deviceNoifyThreadExecutor.execute(()->{
        KafkaConsumer<String, String> consumerObj = null;
        // 指定多主题:
        List<String> topics = CbdmOptUtil.stringToStringList(PropertiesUtil.getProperty("kafka.subscribe.topics"), ConstParamErrorCode.DEFAULT_SPLIT_KEY, false);
        try {
            consumerObj = new KafkaConsumer<>(props);
            if(consumerObj != null) {
                consumerObj.subscribe(topics);
                resourceNotifyConsumer.setConsumer(consumerObj);
                KafkaLinkCache.DEVICE_CONSUMER_MAP.put("kafkaComsumer", consumerObj);
                resourceNotifyConsumer.onMessage();
            }
        } catch(Exception e) {
            LogUtils.logError(RunTimeLogUtil.toErrorLog(ConstParamErrorCode.SYSTEM_CODE_FAIL + "", LogObjectTypeEnum.SYSTEM,"consume",
                    "resolve data platform notify error"),e);
        }finally {
            // 关闭
            consumerObj.close();
        }
    });

    //保存配置
    KafkaLinkCache.kafkaConfigCache = kafkaIp + "_" + kafkaPort;
}

@Component(value = "resourceNotifyConsumer")
public class ResourceNotifyConsumer {

    private Logger logger = LoggerFactory.getLogger(ResourceNotifyConsumer.class);

    @Resource
    IAccessDeviceService resourceService;

    private KafkaConsumer<String, String> consumer = null;

    public KafkaConsumer<String, String> getConsumer() {
        return consumer;
    }

    public void setConsumer(KafkaConsumer<String, String> consumer) {
        this.consumer = consumer;
    }

    public void closeConsumer()
    {
        //consumer非线程安全,依靠gc回收
        consumer = null;
    }

    public void onMessage(){
        try{
            logger.info(RunTimeLogUtil.toLog(LogObjectTypeEnum.SYSTEM,"consume","Get resource Notify start",null,null));

            while (true) {
                if(consumer != null)
                {
                    // timeout 阻塞时间,从kafka中取出100毫秒的数据,有可能一次取出0到N条
                    List<Map<String,Object>> datas = new ArrayList<>();
                    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
                    // 遍历
                    for (ConsumerRecord<String, String> record : records) {
                        Map<String,Object> notifyDto = ( Map<String,Object> ) JsonUtils.jsonToMap(record.value());
                        datas.add(notifyDto);
                    }
                    // 拿出结果
                    if(CollectionUtils.isNotEmpty(datas)){
                        logger.info(RunTimeLogUtil.toLog(LogObjectTypeEnum.SYSTEM,"consume","Get resource Notify",null,null, "record"),JsonUtils.object2Json(datas));
                        // 起线程处理 资源变更通知
                        resourceHandle(datas);
                    }
                } else {
                    break;
                }
            }
        }catch (Throwable e){
            logger.error(RunTimeLogUtil.toErrorLog(ConstParamErrorCode.SYSTEM_CODE_FAIL + "",LogObjectTypeEnum.SYSTEM,"consume",
                   "resolve resource notify error"),e);
        }
    }

    /**
     *
     * @param datas
     */
    private void resourceHandle(List<Map<String,Object>> datas){
        if(CollectionUtils.isNotEmpty(datas)){
            try {
                new Thread(() -> resourceService.dealResource(datas)).start();
            }catch (Throwable e){
                logger.error(RunTimeLogUtil.toErrorLog(ConstParamErrorCode.SYSTEM_CODE_FAIL + "",LogObjectTypeEnum.SYSTEM,"consume",
                    "resourceHandle error"),e);
            }
        }else{
            logger.info(RunTimeLogUtil.toLog(LogObjectTypeEnum.SYSTEM,"consume","resource notify data is empty!",null,null));
        }
    }
}

你可能感兴趣的:(kafka动态创建消费者(实时更新topic和servers))