如何避免为默认致命异常击中所有可重试主题?

How to avoid hitting all retryable topics for fatal-by-default exceptions?

我的团队正在编写一项服务,该服务利用 Spring Kafka(版本 2.8.2)提供的可重试主题机制。这是配置的一个子集:

@Bean
public ConsumerFactory<String, UploadMessage> consumerFactory() {
    return new DefaultKafkaConsumerFactory<>(
            this.springProperties.buildConsumerProperties(),
            new StringDeserializer(),
            new ErrorHandlingDeserializer<>(new KafkaMessageDeserializer()));
}

@Bean
public RetryTopicConfiguration retryTopicConfiguration(KafkaTemplate<String, Object> kafkaTemplate) {
    final var retry = this.applicationProperties.retry();

    return RetryTopicConfigurationBuilder.newInstance()
            .doNotAutoCreateRetryTopics()
            .suffixTopicsWithIndexValues()
            .maxAttempts(retry.attempts())
            .exponentialBackoff(retry.initialDelay(), retry.multiplier(), retry.maxDelay())
            .dltHandlerMethod(DeadLetterTopicProcessor.ENDPOINT_HANDLER_METHOD)
            .create(kafkaTemplate);
}

KafkaMessageDeserializer 是一个自定义反序列化器,它解码 protobuf 编码的消息,并可能在失败的情况下抛出 SerializationException。此异常被 Spring Kafka 正确捕获并转换为 DeserializationException。我觉得有点困惑的是,截获的毒丸消息在最终到达死信之前击中了所有重试主题。显然它在每一步都以完全相同的错误失败。

我知道RetryTopicConfigurationBuilder::notRetryOn may be used to skip the retry attempts for particular exception types, but what if I want to use exactly the same list of exceptions as in ExceptionClassifier::configureDefaultClassifier?有没有一种方法可以在基本上不复制代码的情况下以编程方式访问此信息?

这个建议很好;它可能应该是默认行为(或至少是可选的)。

请在 GitHub 上提出功能请求。

这里有一个有点相关的讨论:https://github.com/spring-projects/spring-kafka/discussions/2101