Kafka的Springboot云流

时间:2018-12-10 16:51:11

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

我正在尝试使用带有Kafka的Springboot cloud Stream设置一个项目。我设法构建了一个简单的示例,其中,侦听器从某个主题获取消息,并在对其进行处理之后,将输出发送到另一个主题。

我的侦听器和频道的配置如下:

@Component
public class FileEventListener {
    private FileEventProcessorService fileEventProcessorService;

    @Autowired
    public FileEventListener(FileEventProcessorService fileEventProcessorService) {
        this.fileEventProcessorService = fileEventProcessorService;
    }

    @StreamListener(target = FileEventStreams.INPUT)
    public void handleLine(@Payload(required = false) String jsonData) {
        this.fileEventProcessorService.process(jsonData);
    }
}

public interface FileEventStreams {
    String INPUT = "file_events";
    String OUTPUT = "raw_lines";

    @Input(INPUT)
    SubscribableChannel inboundFileEventChannel();

    @Output(OUTPUT)
    MessageChannel outboundRawLinesChannel();
}

此示例的问题在于,当服务启动时,它不检查主题中已经存在的消息,而仅处理启动后发送的那些消息。我对Springboot流和kafka还是很陌生,但就我所读的内容而言,此行为可能与我正在使用SubscribableChannel的事实相对应。例如,我尝试使用QueueChannel来查看其工作原理,但发现以下异常:

Error creating bean with name ... nested exception is java.lang.IllegalStateException: No factory found for binding target type: org.springframework.integration.channel.QueueChannel among registered factories: channelFactory,messageSourceFactory

所以,我的问题是:

  1. 如果我想在应用程序启动后处理该主题中存在的所有消息(并且消息也仅由一个使用者处理),那么我走对了吗?
  2. 即使QueueChannel不是实现1.中说明的行为的正确选择。)为了能够使用这种类型的通道,我还必须添加什么内容?

谢谢!

1 个答案:

答案 0 :(得分:1)

  1. 添加spring.cloud.stream.bindings.file_events.group=foo

    • 匿名组仅从主题结尾开始消耗,默认情况下,与组的绑定从开头开始消耗。
  2. 您不能使用PollableChannel进行绑定,它必须是SubscribableChannel