【发布时间】:2021-07-12 23:08:21
【问题描述】:
将 spring-cloud Hoxton.SR12 版本中的 spring-cloud-stream 与 Kafka Binder 一起使用。 启动版本:2.5.2
问题陈述:
-
我想通过将它们推送到毒丸主题而不重试来处理反序列化错误。
-
通过重试然后推送到parkingLot主题来处理任何其他异常。
-
不要重试 ValidationException
这是我目前的错误处理代码:
@Configuration
@Slf4j
public class ErrorHandlingConfig {
@Value("${errorHandling.parkingLotDestination}")
private String parkingLotDestination;
@Value("${errorHandling.retryAttempts}")
private long retryAttempts;
@Value("${errorHandling.retryIntervalMillis}")
private long retryIntervalMillis;
@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<byte[], byte[]>> customizer(SeekToCurrentErrorHandler errorHandler) {
return (container, dest, group) -> {
container.setErrorHandler(errorHandler);
};
}
@Bean
public SeekToCurrentErrorHandler errorHandler(DeadLetterPublishingRecoverer parkingLotPublisher) {
SeekToCurrentErrorHandler seekToCurrentErrorHandler = new SeekToCurrentErrorHandler(parkingLotPublisher, new FixedBackOff(retryIntervalMillis, retryAttempts));
seekToCurrentErrorHandler.addNotRetryableExceptions(ValidationException.class);
return seekToCurrentErrorHandler;
}
@Bean
public DeadLetterPublishingRecoverer parkingLotPublisher(KafkaOperations bytesTemplate) {
DeadLetterPublishingRecoverer deadLetterPublishingRecoverer = new DeadLetterPublishingRecoverer(bytesTemplate, (cr, e) -> new TopicPartition(parkingLotDestination, cr.partition()));
deadLetterPublishingRecoverer.setHeadersFunction((cr, e) -> cr.headers());
return deadLetterPublishingRecoverer;
}
}
我认为到目前为止我所拥有的应该涵盖被推送到停车场的可重试异常。我现在如何添加代码以将失败的反序列化事件推送到毒主题?
由于无法发送到自定义 dlqName 的突出问题,我想在活页夹/绑定配置之外和容器级别执行此操作。
我可以使用 ErrorHandlingDeserializer 并在其上调用 setFailedDeserializationFunction(),其中包含将消息发送到毒主题的函数。我应该使用 Source 绑定还是原始 KafkaOperations 来执行此操作?我还需要弄清楚如何将这个 ErrorHandingDeserialiser 挂接到 ConsumerFactory。
【问题讨论】:
标签: spring-kafka spring-cloud-stream spring-cloud-stream-binder-kafka