此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-stream 4.1.4spring-doc.cadn.net.cn

重试和死信处理

默认情况下,当您配置重试(例如maxAttemts) 和enableDlq在使用者绑定中,这些函数在 Binder 中执行,侦听器容器或 Kafka 使用者不参与。spring-doc.cadn.net.cn

在某些情况下,最好将此功能移动到侦听器容器,例如:spring-doc.cadn.net.cn

要配置将此功能从 Binder 移动到容器,请定义一个@Bean的类型ListenerContainerWithDlqAndRetryCustomizer. 该接口有以下几种方法:spring-doc.cadn.net.cn

/**
 * Configure the container.
 * @param container the container.
 * @param destinationName the destination name.
 * @param group the group.
 * @param dlqDestinationResolver a destination resolver for the dead letter topic (if
 * enableDlq).
 * @param backOff the backOff using retry properties (if configured).
 * @see #retryAndDlqInBinding(String, String)
 */
void configure(AbstractMessageListenerContainer<?, ?> container, String destinationName, String group,
        @Nullable BiFunction<ConsumerRecord<?, ?>, Exception, TopicPartition> dlqDestinationResolver,
        @Nullable BackOff backOff);

/**
 * Return false to move retries and DLQ from the binding to a customized error handler
 * using the retry metadata and/or a {@code DeadLetterPublishingRecoverer} when
 * configured via
 * {@link #configure(AbstractMessageListenerContainer, String, String, BiFunction, BackOff)}.
 * @param destinationName the destination name.
 * @param group the group.
 * @return false to disable retries and DLQ in the binding
 */
default boolean retryAndDlqInBinding(String destinationName, String group) {
    return true;
}

目标解析程序和BackOff是从 binding 属性创建的(如果已配置)。这KafkaTemplate使用 Configuration fromspring.kafka…​.性能。然后,您可以使用这些来创建自定义错误处理程序和死信发布者;例如:spring-doc.cadn.net.cn

@Bean
ListenerContainerWithDlqAndRetryCustomizer cust(KafkaTemplate<?, ?> template) {
    return new ListenerContainerWithDlqAndRetryCustomizer() {

        @Override
        public void configure(AbstractMessageListenerContainer<?, ?> container, String destinationName,
                String group,
                @Nullable BiFunction<ConsumerRecord<?, ?>, Exception, TopicPartition> dlqDestinationResolver,
                @Nullable BackOff backOff) {

            if (destinationName.equals("topicWithLongTotalRetryConfig")) {
                ConsumerRecordRecoverer dlpr = new DeadLetterPublishingRecoverer(template,
                        dlqDestinationResolver);
                container.setCommonErrorHandler(new DefaultErrorHandler(dlpr, backOff));
            }
        }

        @Override
        public boolean retryAndDlqInBinding(String destinationName, String group) {
            return !destinationName.contains("topicWithLongTotalRetryConfig");
        }

    };
}

现在,只需要一个重试延迟大于使用者的max.poll.interval.ms财产。spring-doc.cadn.net.cn

当使用多个 Binder 时,'ListenerContainerWithDlqAndRetryCustomizer' bean 会被 'DefaultBinderFactory' 覆盖。对于 bean 要申请,您需要使用 'BinderCustomizer' 来设置容器定制器(请参阅 [binder-customizer]):spring-doc.cadn.net.cn

@Bean
public BinderCustomizer binderCustomizer(ListenerContainerWithDlqAndRetryCustomizer containerCustomizer) {
    return (binder, binderName) -> {
        if (binder instanceof KafkaMessageChannelBinder kafkaMessageChannelBinder) {
            kafkaMessageChannelBinder.setContainerCustomizer(containerCustomizer);
        }
        else if (binder instanceof KStreamBinder) {
            ...
        }
        else if (binder instanceof RabbitMessageChannelBinder) {
            ...
        }
    };
}