kafka springboot+@KafkaListener 自动提交的简单使用

spring-boot 版本 1.5.12

依赖使用spring-kafka1.3.3(对应kafka-clients版本0.11.0.0,请使用于kafka版本对应版本的依赖)

        
            org.springframework.boot
            spring-boot-starter-parent
            1.5.12.RELEASE
            
                

        
            org.springframework.kafka
            spring-kafka
            1.3.3.RELEASE
        

 

1、自定义监听工厂  (resources目录下面kafka.properties文件中定义对应参数)

kafka.bootstrapServers=127.0.0.1::9093
kafka.groupId=test
kafka.sessionTimeout=30000
kafka.maxPollRecords=50
kafka.autoOffsetReset=latest
#kafka.max.poll.interval.ms=
kafka.autoCommitIntervalMs=2000
kafka.consumerRequestTimeoutMs=320000
#消费者并发启动个数(对应分区个数)每个listener方法
kafka.concurrency=10
@Configuration
@PropertySource("kafka.properties")
@ConfigurationProperties(prefix = "kafka")
@Data
public class KonkaKafkaConfig {

    private String bootstrapServers;

    private String groupId;

    private String sessionTimeout;

    private String maxPollRecords;

    private String autoOffsetReset;

    private String autoCommitIntervalMs;

    private String consumerRequestTimeoutMs;

    private Integer concurrency;
    @Bean("kafkaListenerContainerFactory")
    public KafkaListenerContainerFactory batchFactory(){
        ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(new DefaultKafkaConsumerFactory<>(consumerConfigs()));
        factory.setBatchListener(true);
        factory.setConcurrency(concurrency);
        return factory;
    }

    private Map consumerConfigs() {
        Map props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG,consumerRequestTimeoutMs);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
        props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitIntervalMs);
        props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaLogMessageDeSer.class);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPollRecords);//每一批数量
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
//        props.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG) 超过对接时间认为是lock
        return props;
    }
}

2、监听器

@Component
public class KafkaListener {

    private final static Logger LOGGER = LoggerFactory.getLogger(KafkaListener.class);


    @KafkaListener(containerFactory = "kafkaListenerContainerFactory", topics = "ucenter")
    public void consumerListener(List consumerRecords) {
        if (consumerRecords.size() > 0) {
            PartitionCounter.addCounter(consumerRecords.get(0).partition(), consumerRecords.size());
        }
        Iterator iterator = consumerRecords.iterator();
        while (iterator.hasNext()) {
            ConsumerRecord consumerRecord = iterator.next();
            String key = consumerRecord.key().toString();
            String value = consumerRecord.value().toString();
        }
    }

}

3、spring-boot容器即可
(参数详解看后面文章)

 

 

 

你可能感兴趣的:(kafka)