我使用Spring Kafka文档中的批处理模式在Spring Boot App中设置了Kafka Consumer。它似乎工作,直到我们加载主题。然后,抵消没有更新,它将一遍又一遍地处理同一批消费者消息。
默认情况下,批量大小基于框架中的某些算法显示为动态。因此,随着主题越来越多,它从批量处理数百封邮件变为数千封。通过将批量大小(max.poll.records)设置为25,我能够解决这个问题,但在我理解根本原因之前,我并不真正信任Kafka / Spring-Kafka。
批次需要3000毫秒到15000毫秒才能完成。
工厂属性:
public Map<String, Object> getKafkaConfigurationProperties() {
ImmutableMap.Builder<String, Object> builderMap = new ImmutableMap.Builder<String, Object>()
.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, Joiner.on(',').join(getBootstrapServers()))
.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, getKeyDeserializer())
.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 100)
.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false)
.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 15000)
.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, getValueDeserializer())
.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 25);
String groupId = getConsumerGroup();
if (StringUtils.isNotBlank(groupId)) {
builderMap.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
}
return builderMap.build();
}
工厂建设:
private KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> makeKafkaListener(
ConsumerFactory<String, String> consumerFactory, KafkaConsumerConfiguration configuration) {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory);
factory.setConcurrency(6);
factory.setBatchListener(true);
factory.getContainerProperties().setAckMode(AbstractMessageListenerContainer.AckMode.BATCH);
return factory;
}
侦听器服务使用注释:
@KafkaListener(
containerFactory = "serviceListenerContainerFactory",
group = "kafkaListenerContainers",
id = "widgetListener",
topics = "widget.topic.prod")
public void listenProduct(List<ConsumerRecord<String, String>> consumerRecordList) { ... stuff ... }