此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring for Apache Kafka 3.2.1Spring中文文档

此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring for Apache Kafka 3.2.1Spring中文文档

重试主题和 DLT 的命名方式是,在主主题后加上提供的或默认值,并追加该主题的延迟或索引。Spring中文文档

“my-topic” → “my-topic-retry-0”, “my-topic-retry-1”, ..., “my-topic-dlt”Spring中文文档

“my-other-topic” → “my-topic-myRetrySuffix-1000”, “my-topic-myRetrySuffix-2000”, ..., “my-topic-myDltSuffix”Spring中文文档

默认行为是为每次尝试创建单独的重试主题,并附加一个索引值:retry-0、retry-1、...、retry-n。 因此,默认情况下,重试主题数为配置的减去 1。maxAttempts
默认行为是为每次尝试创建单独的重试主题,并附加一个索引值:retry-0、retry-1、...、retry-n。 因此,默认情况下,重试主题数为配置的减去 1。maxAttempts

重试主题和 DLT 后缀

您可以指定重试和 DLT 主题将使用的后缀。Spring中文文档

@RetryableTopic(retryTopicSuffix = "-my-retry-suffix", dltTopicSuffix = "-my-dlt-suffix")
@KafkaListener(topics = "my-annotated-topic")
public void processMessage(MyPojo message) {
    // ... message processing
}
@Bean
public RetryTopicConfiguration myRetryTopic(KafkaTemplate<String, MyOtherPojo> template) {
    return RetryTopicConfigurationBuilder
            .newInstance()
            .retryTopicSuffix("-my-retry-suffix")
            .dltTopicSuffix("-my-dlt-suffix")
            .create(template);
}
默认后缀为“-retry”和“-dlt”,分别用于重试主题和 dlt。
默认后缀为“-retry”和“-dlt”,分别用于重试主题和 dlt。

追加主题的索引或延迟

您可以在后缀后附加主题的索引或延迟值。Spring中文文档

@RetryableTopic(topicSuffixingStrategy = TopicSuffixingStrategy.SUFFIX_WITH_INDEX_VALUE)
@KafkaListener(topics = "my-annotated-topic")
public void processMessage(MyPojo message) {
    // ... message processing
}
@Bean
public RetryTopicConfiguration myRetryTopic(KafkaTemplate<String, MyPojo> template) {
    return RetryTopicConfigurationBuilder
            .newInstance()
            .suffixTopicsWithIndexValues()
            .create(template);
    }
默认行为是以延迟值为后缀,但具有多个主题的固定延迟配置除外,在这种情况下,主题的后缀为主题的索引。
默认行为是以延迟值为后缀,但具有多个主题的固定延迟配置除外,在这种情况下,主题的后缀为主题的索引。

固定延迟重试的单个主题

如果您使用的是固定延迟策略,例如 或者,则可以使用单个主题来完成非阻塞重试。 本主题将以提供的或默认的后缀为后缀,并且不会附加索引或延迟值。FixedBackOffPolicyNoBackOffPolicySpring中文文档

以前的现在已弃用,可以替换为 。FixedDelayStrategySameIntervalTopicReuseStrategy
@RetryableTopic(backoff = @Backoff(2_000), fixedDelayTopicStrategy = FixedDelayStrategy.SINGLE_TOPIC)
@KafkaListener(topics = "my-annotated-topic")
public void processMessage(MyPojo message) {
    // ... message processing
}
@Bean
public RetryTopicConfiguration myRetryTopic(KafkaTemplate<String, MyPojo> template) {
    return RetryTopicConfigurationBuilder
            .newInstance()
            .fixedBackOff(3_000)
            .maxAttempts(5)
            .useSingleTopicForFixedDelays()
            .create(template);
}
默认行为是为每次尝试创建单独的重试主题,并附加其索引值:retry-0、retry-1、...
以前的现在已弃用,可以替换为 。FixedDelayStrategySameIntervalTopicReuseStrategy
默认行为是为每次尝试创建单独的重试主题,并附加其索引值:retry-0、retry-1、...

maxInterval 指数延迟的单个主题

如果使用指数退避策略 (),则可以使用单个重试主题来完成延迟为 的尝试的非阻塞重试。ExponentialBackOffPolicymaxIntervalSpring中文文档

此“最终”重试主题将以提供的或默认的后缀为后缀,并附加索引或值。maxIntervalSpring中文文档

通过选择使用单个主题进行延迟重试,配置长时间保持重试的指数重试策略可能会变得更加可行,因为在这种方法中,您不需要大量的主题。maxInterval

从 3.2 开始,默认行为是以相同的时间间隔重用重试主题,使用指数退避时,重试主题以延迟值为后缀,最后一次重试主题以相同的时间间隔重用(对应于延迟)。maxIntervalSpring中文文档

例如,当使用 、 和 配置指数退避时,为了继续尝试一小时,需要配置为 229,默认情况下,所需的重试主题为:initialInterval=1_000multiplier=2maxInterval=16_000maxAttemptsSpring中文文档

当使用重试主题数等于配置的减 1 的策略时,以附加索引为后缀的最后一个重试主题(对应于延迟)为:maxAttemptsmaxIntervalSpring中文文档

如果需要多个主题,则可以使用以下配置来完成。Spring中文文档

@RetryableTopic(attempts = 230,
    backoff = @Backoff(delay = 1_000, multiplier = 2, maxDelay = 16_000),
    sameIntervalTopicReuseStrategy = SameIntervalTopicReuseStrategy.MULTIPLE_TOPICS)
@KafkaListener(topics = "my-annotated-topic")
public void processMessage(MyPojo message) {
    // ... message processing
}
@Bean
public RetryTopicConfiguration myRetryTopic(KafkaTemplate<String, MyPojo> template) {
    return RetryTopicConfigurationBuilder
            .newInstance()
            .exponentialBackoff(1_000, 2, 16_000)
            .maxAttempts(230)
            .useSingleTopicForSameIntervals()
            .create(template);
}
通过选择使用单个主题进行延迟重试,配置长时间保持重试的指数重试策略可能会变得更加可行,因为在这种方法中,您不需要大量的主题。maxInterval

自定义命名策略

更复杂的命名策略可以通过注册实现 的 Bean 来实现。 默认实现是,可以通过以下方式注册不同的实现:RetryTopicNamesProviderFactorySuffixingRetryTopicNamesProviderFactorySpring中文文档

@Override
protected RetryTopicComponentFactory createComponentFactory() {
    return new RetryTopicComponentFactory() {
        @Override
        public RetryTopicNamesProviderFactory retryTopicNamesProviderFactory() {
            return new CustomRetryTopicNamesProviderFactory();
        }
    };
}

例如,除了标准后缀之外,以下实现还为 retry/dlt 主题名称添加了前缀:Spring中文文档

public class CustomRetryTopicNamesProviderFactory implements RetryTopicNamesProviderFactory {

    @Override
    public RetryTopicNamesProvider createRetryTopicNamesProvider(
                DestinationTopic.Properties properties) {

        if (properties.isMainEndpoint()) {
            return new SuffixingRetryTopicNamesProvider(properties);
        }
        else {
            return new SuffixingRetryTopicNamesProvider(properties) {

                @Override
                public String getTopicName(String topic) {
                    return "my-prefix-" + super.getTopicName(topic);
                }

            };
        }
    }

}