Spring Kafka并发与spring-integration

时间:2018-05-08 14:02:41

标签: java spring-integration spring-kafka

我无法判断我是否遗漏了一些明显的东西,或者spring-integration-kafka:3.0.1中有一个错误,试图让多个消费者为某个主题运行。该场景是一个包含10个分区的单个Kafka主题,以及一个监听它的springboot-app。相关配置是:

application.yml:

spring:
  kafka:
    consumer:
      group-id: test-consumer
      auto-offset-reset: earliest
    listener:
      concurrency: 4

配置:

@Configuration
@EnableIntegration
@IntegrationComponentScan("com.test")
public class MessageConfig {
    @Bean
    public MessageChannel testReceiveChannel() {
        return MessageChannels.direct().get();
    }

    @Bean
    public IntegrationFlow testReceiveFlow(@Qualifier("kafkaConsumerFactory") final ConsumerFactory<?, ?> kafkaConsumer, final MessageChannel testReceiveChannel) {
        return IntegrationFlows
                .from(Kafka.messageDrivenChannelAdapter(kafkaConsumer, ListenerMode.record, "test-topic"))
                .transform(new JsonToObjectTransformer(EventMessage.class))
                .channel(testReceiveChannel)
                .get();
    }
}

监听器:

@Component
public class EventListener {
    private static final Logger LOG = LoggerFactory.getLogger(EventListener.class);

    @ServiceActivator(inputChannel = "testReceiveChannel")
    public void processMessage(final EventMessage message) {
        LOG.info("Got message {} on {}", message.getValue(), Thread.currentThread().getName());
    }
}

启动时,我只能在所有10个分区上监听1个容器。我可以看到ConcurrentKafkaListenerContainerFactory上设置了正确的并发值,但似乎从未调用initializeContainer方法(如果我理解正确的话,它会将其应用于实际的消费者) )。我可能会看到完全错误的事情。

关于我忽略的事情的任何想法?

1 个答案:

答案 0 :(得分:1)

Spring Boot KafkaProperties(例如spring.kafka.listener.concurrency = 4)和上述ConcurrentKafkaListenerContainerFactory适用于@KafkaListener组件。根本没有与Spring Integration有关。至少是自动的。

您需要手动执行此操作:

Kafka.messageDrivenChannelAdapter(kafkaConsumer, ListenerMode.record, "test-topic")
      .configureListenerContainer(c ->
                        c.concurrency(this.kafkaProperties.getListener().getConcurrency()))