简体   繁体   English

如何在 2.5.4 版本中发布 Spring Kafka DLQ

[英]How to publish Spring Kafka DLQ in 2.5.4 version

need your help and guidance on this.需要您的帮助和指导。

I was using 2.2.X version spring-kafka in my current project.我在当前项目中使用的是 2.2.X 版本的 spring-kafka。

The error handling that I created looks like this:我创建的错误处理如下所示:

@Bean("kafkaConsumer")
public ConcurrentKafkaListenerContainerFactory<String, Map<String, Object>> eventKafkaConsumer() {
    ConcurrentKafkaListenerContainerFactory<String, Map<String, Object>> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setErrorHandler(new SeekToCurrentErrorHandler(createDeadLetterPublishingRecoverer(), 3));
    return factory;
}

public DeadLetterPublishingRecoverer createDeadLetterPublishingRecoverer() {
    return new DeadLetterPublishingRecoverer(getEventKafkaTemplate(),
            (record, ex) -> new TopicPartition("topic-undelivered", -1));
}

And then I upgraded all my project dependency version, such as spring-boot and the spring-kafka into the latest one: 2.5.4 RELEASE然后我将我所有的项目依赖版本,例如 spring-boot 和 spring-kafka 升级到最新版本: 2.5.4 RELEASE

I found that some of the methods were deprecated and changed.我发现其中一些方法已被弃用和更改。

SeekToCurrentErrorHandler SeekToCurrentErrorHandler

SeekToCurrentErrorHandler errorHandler =
new SeekToCurrentErrorHandler((record, exception) -> {
    // recover after 3 failures, woth no back off - e.g. send to a dead-letter topic
}, new FixedBackOff(0L, 2L));

My question is, how to produce the DLQ with these configurations:我的问题是,如何使用这些配置生成 DLQ:

EDITED已编辑

@Bean("kafkaConsumer")
public ConcurrentKafkaListenerContainerFactory<String, Map<String, Object>> kafkaConsumer() {
    ConcurrentKafkaListenerContainerFactory<String, Map<String, Object>> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.setConcurrency(consumerConcurrencyCount);
    factory.setErrorHandler(errorHandler());
    return factory;
}

public SeekToCurrentErrorHandler errorHandler() {
    return new SeekToCurrentErrorHandler(
            deadLetterPublishingRecoverer(),
            new FixedBackOff(0L, 2L)
    );
}

public DeadLetterPublishingRecoverer deadLetterPublishingRecoverer() {
    return new DeadLetterPublishingRecoverer(
            getEventKafkaTemplate(),
            (record, ex) -> {
                if (ex.getCause() instanceof BusinessException || ex.getCause() instanceof TechnicalException) {
                    return new TopicPartition("topic-undelivered", -1);
                }

                return new TopicPartition("topic-fail", -1);
            });
}

public KafkaOperations<String, Object> getEventKafkaTemplate() { // producer to DLQ
    return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(producerConfigs()));
}

This configurations work, thanks to Gary!感谢 Gary,此配置有效!

Thanks in advance提前致谢

It's not clear what you mean by不清楚你的意思是什么

The problem is, in the documentation, it's still using the old method, which is deprecated for 2.5.X version问题是,在文档中,它仍在使用旧方法,该方法已被 2.5.X 版本弃用

The KafkaOperations is an interface that the KafkaTemplate implements; KafkaOperationsKafkaTemplate实现的接口; the only change you need to make is to change the maxAttempts to a BackOff ...您需要做的唯一更改是将maxAttempts更改为BackOff ...

@Bean("kafkaConsumer")
public ConcurrentKafkaListenerContainerFactory<String, Map<String, Object>> eventKafkaConsumer() {
    ConcurrentKafkaListenerContainerFactory<String, Map<String, Object>> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setErrorHandler(new SeekToCurrentErrorHandler(createDeadLetterPublishingRecoverer(), new FixedBackOff(0, 2L));
    return factory;
}

public DeadLetterPublishingRecoverer createDeadLetterPublishingRecoverer() {
    return new DeadLetterPublishingRecoverer(getEventKafkaTemplate(),
            (record, ex) -> new TopicPartition("topic-undelivered", -1));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM