使用Spring Cloud Stream将错误发送到Kafka重试主题

时间:2019-06-10 14:21:10

标签: spring-kafka spring-cloud-stream spring-cloud-function

我正在将Spring Cloud Stream与Spring Cloud Function和Kafka活页夹一起使用。我的目标很简单:我想阅读输入主题,执行一些处理,然后发布到输出主题。如果出现问题,我想将消息发布到错误主题。通过阅读文档,Dead Letter Queue支持似乎将提供我需要的错误主题功能。我的代码如下:

@SpringBootApplication
@EnableBinding(Processor.class)
public class Application {

    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
@Configuration
public class AppConfig {

    @Bean
    public ServiceImpl service() {
        return new ServiceImpl();
    }

    @Bean
    public Function<String, String> execute(ServiceImpl service) {
        return service::doBusinessLogic;
    }
}
public class ServiceImpl {

    public String doBusinessLogic(String message) {
        System.out.println("Handling message");
        if (message.startsWith("foo")) {
            throw new RuntimeException("This is a test");
        }
        return message;
    }

}
spring:
  cloud:
    stream:
      kafka:
        binder:
          brokers: localhost:9091
          autoCreateTopics: false
        bindings:
          input:
            consumer:
              enableDlq: true
              dlqName: myErrorTopic

      bindings:
        input:
          destination: myInputTopic
          group: myConsumerGroup
        output:
          destination: myOutputTopic

      function:
        definition: execute

当我执行此应用程序并将其发布到输入主题时,该服务正确接收到该消息,并引发RuntimeException。我在日志中可以看到该消息已重试(RetryTemplate的默认配置),但是一旦重试用尽,该消息就不会发布到错误主题中。我在这里做什么错了?

0 个答案:

没有答案