0


springboot 消费kafka 定时拉取固定条数

springboot 消费kafka 定时拉取固定条数

springboot消费kafka,频率限制
背景
上游预警信息发送到kafka,springboot消费后,进行推送到企业微信机器人。
企微机器人有个限制,每分钟最多20条,其余被丢掉。

两种方案
1 消费kafka时,控制速度,每分钟去拉取一次,每次拉取2条

2 直接消费kafka后,存表,每分钟的定时任务取20条进行推送

最终方案1 ,因为改动小,

关键代码
//设置为批量消费,每个批次数量在Kafka配置参数中设置ConsumerConfig.MAX_POLL_RECORDS_CONFIG
factory.setBatchListener(true);
factory.getContainerProperties().setIdleBetweenPolls(3000L);

packagecom.kafkatest.lisenter;importorg.apache.kafka.clients.consumer.ConsumerRecord;importorg.springframework.kafka.annotation.KafkaListener;importorg.springframework.stereotype.Component;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.util.List;/**
 * @ClassName KafkaConsumer
 * @Description
 * @Author wangdonglin
 * @Date 2023/1/18 10:31
 * @Version 1.0
 */@ComponentpublicclassKafkaConsumer{privatefinalLogger logger =LoggerFactory.getLogger(this.getClass());/**
     * 监听topic,批量消费
     */@KafkaListener(topics ="${topicName.topic}", containerFactory ="batchFactory")publicvoidlisten2(List<ConsumerRecord<String,String>> records){
        logger.info("listen-----");batchConsumer(records);}/**
     * 批量消费
     */publicvoidbatchConsumer(List<ConsumerRecord<String,String>> records){
        records.forEach(o->{System.out.println(o.value());});}}
packagecom.kafkatest.config;importcom.google.common.collect.Maps;importorg.apache.kafka.clients.consumer.ConsumerConfig;importorg.apache.kafka.clients.producer.ProducerConfig;importorg.apache.kafka.common.serialization.StringDeserializer;importorg.apache.kafka.common.serialization.StringSerializer;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.kafka.annotation.EnableKafka;importorg.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;importorg.springframework.kafka.config.KafkaListenerContainerFactory;importorg.springframework.kafka.core.DefaultKafkaConsumerFactory;importorg.springframework.kafka.core.DefaultKafkaProducerFactory;importorg.springframework.kafka.core.KafkaTemplate;importorg.springframework.kafka.core.ProducerFactory;importjava.util.Map;/**
 * @ClassName KafkaConfiguration
 * @Description
 * @Author wangdonglin
 * @Date 2023/1/18 10:25
 * @Version 1.0
 */@Configuration@EnableKafkapublicclassKafkaConfiguration{@Value("${spring.kafka.bootstrap-servers}")privateString bootstrapServers;@Value("${spring.kafka.consumer.enable-auto-commit}")privateBoolean autoCommit;@Value("${spring.kafka.consumer.auto-commit-interval}")privateInteger autoCommitInterval;@Value("${spring.kafka.consumer.group-id}")privateString groupId;@Value("${spring.kafka.consumer.max-poll-records}")privateInteger maxPollRecords;@Value("${spring.kafka.consumer.auto-offset-reset}")privateString autoOffsetReset;@Value("${spring.kafka.producer.retries}")privateInteger retries;@Value("${spring.kafka.producer.batch-size}")privateInteger batchSize;@Value("${spring.kafka.producer.buffer-memory}")privateInteger bufferMemory;/**
     *  生产者配置信息
     */@BeanpublicMap<String,Object>producerConfigs(){Map<String,Object> props =Maps.newHashMap();
        props.put(ProducerConfig.ACKS_CONFIG,"0");
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.RETRIES_CONFIG, retries);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
        props.put(ProducerConfig.LINGER_MS_CONFIG,1);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class);return props;}/**
     *  生产者工厂
     */@BeanpublicProducerFactory<String,String>producerFactory(){returnnewDefaultKafkaProducerFactory<>(producerConfigs());}/**
     *  生产者模板
     */@BeanpublicKafkaTemplate<String,String>kafkaTemplate(){returnnewKafkaTemplate<>(producerFactory());}/**
     *  消费者配置信息
     */@BeanpublicMap<String,Object>consumerConfigs(){Map<String,Object> props =Maps.newHashMap();
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPollRecords);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,autoCommit);
        props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,autoCommitInterval);
        props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG,120000);
        props.put(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG,180000);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);return props;}/**
     *  消费者批量工程
     */@BeanpublicKafkaListenerContainerFactory<?>batchFactory(){ConcurrentKafkaListenerContainerFactory<Integer,String> factory =newConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(newDefaultKafkaConsumerFactory<>(consumerConfigs()));consumerConfigs().forEach((k,v)->System.out.println(k+"===="+v));//设置为批量消费,每个批次数量在Kafka配置参数中设置ConsumerConfig.MAX_POLL_RECORDS_CONFIG
        factory.setBatchListener(true);
        factory.getContainerProperties().setIdleBetweenPolls(3000L);return factory;}}
标签: kafka spring boot java

本文转载自: https://blog.csdn.net/weixin_39815761/article/details/128726989
版权归原作者 wdl_1212 所有, 如有侵权,请联系我们删除。

“springboot 消费kafka 定时拉取固定条数”的评论:

还没有评论