Spring Boot Kafka消费者

时间:2019-07-01 16:31:32

标签: apache-kafka spring-integration spring-kafka

使用spring集成Kafka dsl,我想知道为什么监听器没有收到消息?但是相同的应用程序如果我用KafkaListener注释的方法替换spring集成DSL,则可以很好地使用消息。 DSL我会缺少什么?

不使用的DSL代码:

@Configuration
@EnableKafka
class KafkaConfig {
    //consumer factory provided by Spring boot
    @Bean
    IntegrationFlow inboundKafkaEventFlow(ConsumerFactory consumerFactory) {
        IntegrationFlows
                .from(Kafka
                .messageDrivenChannelAdapter(consumerFactory, "kafkaTopic")
                .configureListenerContainer({ c -> c.groupId('kafka-consumer-staging') })
                .id("kafkaTopicListener").autoStartup(true)
        )

                .channel("logChannel")
                .get()
    }
}

logChannel(或我使用的任何其他渠道)不反映入站消息。

如果使用普通侦听器,则可以使用上面的代码代替上面的代码。

@Component
class KafkaConsumer {
    @KafkaListener(topics = ['kafkaTopic'], groupId = 'kafka-consumer-staging')
    void inboundKafkaEvent(String message) {
        log.debug("message is {}", message)
    }
}

两种方法都为Kafka使用者使用相同的application.properties。

1 个答案:

答案 0 :(得分:1)

您缺少使用Spring Integration的事实,但是尚未在应用程序中启用它。不过,您不需要为Kafka这么做,因为您不会将@KafkaListener用作它。因此,要启用Spring Integration基础结构,您需要在@EnableIntegration类上添加@Configurationhttps://docs.spring.io/spring-integration/docs/5.1.6.RELEASE/reference/html/#configuration-enable-integration

相关问题