1

我有一个这样定义的 Kafka 处理器。

import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.kafka.common.serialization.StringDeserializer
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
import reactor.core.scheduler.Schedulers
import reactor.kafka.receiver.KafkaReceiver
import reactor.kafka.receiver.ReceiverOptions
import reactor.kafka.receiver.ReceiverRecord
import reactor.kotlin.core.publisher.toMono
import reactor.util.retry.Retry
import java.time.Duration
import java.util.*

@Component
class KafkaProcessor {
    private val logger = LoggerFactory.getLogger(javaClass)
    
    private val consumerProps = hashMapOf(
        ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to StringDeserializer::javaClass,
        ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to StringDeserializer::javaClass,
        ConsumerConfig.GROUP_ID_CONFIG to "groupId",
        ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to "earliest",
        ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to "localhost:9092"
    )

    private val receiverOptions = ReceiverOptions.create<String, String>(consumerProps)
        .subscription(Collections.singleton("some-topic"))
        .commitInterval(Duration.ofSeconds(1))
        .commitBatchSize(1000)
        .maxCommitAttempts(1)

    private val kafkaReceiver: KafkaReceiver<String, String> = KafkaReceiver.create(receiverOptions)

    @Bean
    fun processKafkaMessages(): Unit {
        kafkaReceiver.receive()
            .groupBy { m -> m.receiverOffset().topicPartition() }
            .flatMap { partitionFlux ->
                partitionFlux.publishOn(Schedulers.elastic())
                    .concatMap { receiverRecord ->
                        processRecord(receiverRecord)
                            .map { it.receiverOffset().acknowledge() }
                    }
            }
            .retryWhen(
                Retry.backoff(3, Duration.ofSeconds(1))
                    .maxBackoff(Duration.ofSeconds(3))
                    .doBeforeRetry { rs ->
                        logger.warn("Retrying: ${rs.totalRetries() + 1}/3 due to ${rs.failure()}")
                    }
                    .onRetryExhaustedThrow { _, u ->
                        logger.error("All ${u.totalRetries() + 1} attempts failed with the last exception: ${u.failure()}")
                        u.failure()
                    }
            )
            .subscribe()
    }
    
    private fun processRecord(record: ReceiverRecord<String, String>): Mono<ReceiverRecord<String, String>> {
        return record.toMono()
    }
}

有时,我得到这个错误。

org.apache.kafka.clients.consumer.RetriableCommitFailedException: Offset commit failed with a retriable exception. You should retry committing the latest consumed offsets.
Caused by: org.apache.kafka.common.errors.TimeoutException: The request timed out.

第一次重试看起来像这样。

Retrying: 1/3 due to org.apache.kafka.clients.consumer.RetriableCommitFailedException: Offset commit failed with a retriable exception. You should retry committing the latest consumed offsets

第二个和第三个看起来像这样。

Retrying: 2/3 due to reactor.core.Exceptions$ReactorRejectedExecutionException: Scheduler unavailable

Retrying: 3/3 due to reactor.core.Exceptions$ReactorRejectedExecutionException: Scheduler unavailable

一旦所有 3 次重试都用完,消息将如下所示。

All 4 attempts failed with the last exception: reactor.core.Exceptions$ReactorRejectedExecutionException: Scheduler unavailable 

当我确实收到该错误时,我需要重新启动应用程序才能重新连接到 Kafka 代理并提交记录。

我知道通过设置maxCommitAttempts1意味着一旦它击中 a RetriableCommitFailedException,它就不会再重试了。我认为retryWhen我放在processKafkaMessages()函数末尾的子句可以解决问题,以便管道可以自行恢复。

我设置它的原因maxCommitAttempts是因为它没有这里讨论的回退重试,并且默认的最大 100 次提交尝试是在 10 毫秒内完成的。所以,我想我应该编写自己的带有退避的重试逻辑。

问题是,我应该如何正确地进行自动提交的回退重试?是否可以为此使用编写单元测试EmbeddedKafka

语:Kotlin

反应堆卡夫卡库:io.projectreactor.kafka:reactor-kafka:1.2.2.RELEASE

4

0 回答 0