[英]Kafka Spring consumer offsets are not committed with ConsumerRecordRecoverer
版本:
spring-boot : 2.2.2.RELEASE
spring-kafka : 2.3.7.RELEASE
kafka broker : 2.3.1 (via amazon MSK)
道具:
auto.offset.reset: earliest
enable.auto.commit: false
isolation.level: read_committed
我有一個KafkaListener
使用ConcurrentKafkaListenerContainerFactory
配置了ConsumerRecordRecoverer
的自定義實現。 我注意到當這個容器確實從一些異常中恢復時,所述恢復消息的消費者偏移量沒有提交。 只有當消息被成功處理(即沒有恢復)時才會提交偏移量。 但是,偵聽器/消費者/容器似乎確實保留了 memory 中的實際偏移量,因為在此應用程序保持運行時將前進超過恢復的消息。
如果 spring 引導應用程序在最后一條消息未成功處理並且將從實際提交的最后一個偏移量恢復,可能會重新處理已恢復但未提交偏移量的消息時,這將導致問題。
我通過對一個空主題的本地測試確認了這一點。
在這一點上,我假設我缺少 spring 工件上的一些關鍵配置或設置器,但我不清楚缺少什么。 我曾假設這將是使用DefaultAfterRollbackProcessor#setCommitRecovered
為true
的目的。
KafkaConfiguration
@Configuration
public class KafkaConfig {
@Bean
ConsumerRetryConfig retryConfig() {
return new ConsumerRetryConfig();
}
@Bean
public RetryTemplate consumerRetryTemplate(ConsumerRetryConfig consumerRetryConfig) {
RetryTemplate retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(consumerRetryConfig.getRetryWaitInterval());
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(consumerRetryConfig.getMaxRetries());
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
@Bean
@Lazy
FiniteRequeueingRecovererConfig finiteRequeueingRecovererConfig() {
return new FiniteRequeueingRecovererConfig();
}
@Bean
@Lazy
FiniteRequeueingRecordRecoverer finiteRequeueingRecordRecoverer(
KafkaTemplate<String, SpecificRecord> kafkaTemplate,
FiniteRequeueingRecovererConfig finiteRequeueingRecovererConfig
) {
return new FiniteRequeueingRecordRecoverer(kafkaTemplate, finiteRequeueingRecovererConfig.getMaxRequeues());
}
@Bean
@Lazy
DefaultAfterRollbackProcessor finiteRequeueingRollbackProcessor(
FiniteRequeueingRecordRecoverer finiteRequeueingRecordRecoverer,
ConsumerRetryConfig consumerRetryConfig
) {
DefaultAfterRollbackProcessor ret = new DefaultAfterRollbackProcessor(
finiteRequeueingRecordRecoverer,
new FixedBackOff(
consumerRetryConfig.getRetryWaitInterval(),
consumerRetryConfig.getMaxRetries()
)
);
ret.setCommitRecovered(true);
return ret;
}
@Bean
public ProducerFactory<String, SpecificRecord> avroMessageProducerFactory(KafkaProperties kafkaProperties) {
Map<String, Object> props = MapBuilder.<String, Object>builder()
.putAll(kafkaProperties.buildProducerProperties())
.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, UUID.randomUUID().toString())
.build();
return (kafkaAvroSerializer==null) ?
new DefaultKafkaProducerFactory<>(props) :
new DefaultKafkaProducerFactory(props, new StringSerializer(), kafkaAvroSerializer);
}
@Bean
public KafkaTemplate<String, SpecificRecord> avroMessageKafkaTemplate(ProducerFactory<String, SpecificRecord> avroMessageProducerFactory) {
return new KafkaTemplate<>(avroMessageProducerFactory);
}
@Bean
public KafkaTransactionManager<?,?> kafkaTransactionManager(ProducerFactory<String, SpecificRecord> avroMessageProducerFactory) {
return new KafkaTransactionManager<>(avroMessageProducerFactory);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<?, ?> finiteRequeueingKafkaListenerContainerFactory(
ConsumerFactory<Object, Object> consumerFactory,
ConcurrentKafkaListenerContainerFactoryConfigurer configurer,
KafkaTransactionManager<Object, Object> kafkaTransactionManager,
DefaultAfterRollbackProcessor finiteRequeueingRollbackProcessor
) {
ConcurrentKafkaListenerContainerFactory<Object, Object> factory =
new ConcurrentKafkaListenerContainerFactory<>();
configurer.configure(factory, consumerFactory);
factory.getContainerProperties().setTransactionManager(kafkaTransactionManager);
factory.setStatefulRetry(true);
factory.setAfterRollbackProcessor(finiteRequeueingRollbackProcessor);
return factory;
}
@KafkaListener(
id = "${some.listener-id}",
topics = "${some.topic}",
groupId = "${some.group-id}",
containerFactory = "finiteRequeueingKafkaListenerContainerFactory"
)
public void consume(
@Payload WebhookNotificationMessage message,
@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String key,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
@Header(KafkaHeaders.RECEIVED_TOPIC) String topic,
@Header(KafkaHeaders.RECEIVED_TIMESTAMP) long ts
) throws Exception {
// Do the thing, maybe throw an exception
}
}
FiniteRequeueingRecordRecoverer
public class FiniteRequeueingRecordRecoverer implements ConsumerRecordRecoverer {
private final Logger logger = LoggerLike.getLogger(FiniteRequeueingRecordRecoverer.class);
private KafkaTemplate<String, SpecificRecord> kafkaTemplate;
private Integer maxRequeues;
public FiniteRequeueingRecordRecoverer(KafkaTemplate<String, SpecificRecord> kafkaTemplate, Integer maxRequeues) {
this.kafkaTemplate = kafkaTemplate;
this.maxRequeues = maxRequeues;
}
@Override
public void accept(ConsumerRecord<?, ?> consumerRecord, Exception e) {
// Not sure the substance of this recoverer is relevant...but if so
// If the retry number in the avro record is < this.maxRequeues
// then increment the retries and re enqueue this message, move on
// If retries have been exhausted, do not requeue and send to a dead letter or just abandon
}
}
DefaultAfterRollbackProcessor
需要KafkaTemplate
將偏移量發送到新事務。
如果commitRecovered
為真並且沒有 KT,我們可能應該記錄一個警告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.