【问题标题】:How to handle errors occurring during the processing of data in Kafka Streams如何处理 Kafka Streams 中数据处理过程中发生的错误
【发布时间】:2020-12-16 04:52:45
【问题描述】:

我正在使用 Spring Cloud Stream Kafka Streams 编写一个 Java 应用程序。这是我正在使用的函数方法 sn-p:

@Bean
public Function<KStream<String, String>, KStream<String, String>> process() {
    return input ->
        input.transform(
            () ->
                new Transformer<String, String, KeyValue<String, String>>() {

                  ProcessorContext context;

                  @Override
                  public void init(ProcessorContext context) {
                    this.context = context;
                  }

                  @Override
                  public void close() {}

                  @Override
                  public KeyValue<String, String> transform(String key, String value) {                       
                         String result = fetch_data_from_database(key, value);
                         return new KeyValue<>(key, result);
                  }
});

fetch_data_from_database() 可以抛出异常。

如果 fetch_from_database() 出现异常,我如何停止处理入站 KStream(偏移量不应被提交)并使用相同的偏移量数据重试处理?

【问题讨论】:

标签: apache-kafka apache-kafka-streams spring-cloud-stream


【解决方案1】:

在这种情况下,您需要自行重试逻辑。为此,您可以使用 Spring 的RetryTemplateThis answer 详细介绍了如何在 Kafka Streams 中使用 RetryTemplate。它不像您那样使用低级处理器 API,但它是相同的想法。将您的数据库调用包装在重试模板中,并根据您的要求自定义重试。任何上游处理都将暂停,直到重试次数用尽。

【讨论】:

  • RetryTemplate 存在于 kafka 处理之外,因此如果您继续重试超过轮询时间,则会发生 kafka 消费者重新平衡。
猜你喜欢
  • 2021-10-26
  • 1970-01-01
  • 2017-01-07
  • 2017-07-28
  • 1970-01-01
  • 2019-10-01
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多