【问题标题】:How to set RecordInterceptor to ConcurrentKafkaListenerContainerFactory如何将 RecordInterceptor 设置为 ConcurrentKafkaListenerContainerFactory
【发布时间】:2019-10-28 09:41:07
【问题描述】:

我使用的是 Spring Kafka 2.2.7,我已经配置了 @EnableKafkakafkaListenerContainerFactory 并使用 @KafkaListener 来消费消息,一切都按预期工作。

我想添加一个RecordInterceptor 来记录所有使用的消息,但发现很难配置它。 documentation 声明 RecordInterceptor 可以在容器上设置,但是我不确定如何获取容器的实例。

从 2.2.7 版本开始,可以在监听容器中添加 RecordInterceptor;它将在调用侦听器之前调用,以允许检查或修改记录。

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, Bytes> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, Bytes> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(createConsumerFactory());
        factory.setConcurrency(consumerCount);
        return factory;
    }

我浏览了 Spring 文档但没有找到解决方案,这似乎是一件简单的事情,但也许我错过了一些东西。

我们将不胜感激。

提前致谢。

【问题讨论】:

    标签: java apache-kafka spring-kafka


    【解决方案1】:

    有一个方法setRecordInterceptor,因为2.2.7

    factory.setRecordInterceptor(new RecordInterceptor);
    

    另外一个信息RecordInterceptor 不适用于批处理监听器

    从 2.2.7 版本开始,可以在监听容器中添加 RecordInterceptor;它将在调用允许检查或修改记录的侦听器之前调用。如果拦截器返回 null,则不调用侦听器。 当侦听器是批处理侦听器时,不会调用拦截器。

    【讨论】:

    • 感谢您指出这一点。我知道我错过了一些愚蠢的东西,但它更愚蠢!当我检查工厂是否有这种方法时,我在 2.2.3 上,显然它不存在。升级后忘记检查了。
    • setRecordInterceptor 的行为是否类似于 kafka 消费者配置中的 ConsumerConfig.MAX_POLL_RECORDS_CONFIG=1?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2012-03-16
    • 1970-01-01
    • 2018-06-01
    • 2016-11-01
    • 2012-08-10
    • 2012-08-18
    相关资源
    最近更新 更多