【问题标题】:rest controller to return records in kafka via spring kafka休息控制器通过spring kafka返回kafka中的记录
【发布时间】:2018-05-03 11:16:16
【问题描述】:

对于我的演示应用程序,我必须创建一个休息控制器来返回 kafka 队列中的消息。我已阅读 spring-kafka 参考指南并实现了消费者配置并创建了如下 bean

@Configuration
@EnableKafka
public class ConsumerConfiguration {

    @Value("${kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props = new HashMap<>();
        // list of host:port pairs used for establishing the initial connections to the Kakfa cluster
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
        // allows a pool of processes to divide the work of consuming and processing records
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "trx");

        return props;
    }

    @Bean
    public ConsumerFactory<String, Transaction> transactionConsumerFactory() {
        return new DefaultKafkaConsumerFactory<>(
                consumerConfigs(),new StringDeserializer(),new JsonDeserializer<>(Transaction.class)
        );
    }

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, Transaction>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, Transaction> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(transactionConsumerFactory());

        return factory;
    }

    @Bean
    public Consumer consumer() {
        return new Consumer();
    }

}

以及像下面这样的另一个类消费者

public class Consumer {

    private CountDownLatch latch = new CountDownLatch(1);

    public CountDownLatch getLatch() {
        return latch;
    }

    @KafkaListener(topics = "${kafka.topic.name}")
    public void receive(Transaction transaction) {
        latch.countDown();
    }
}

我现在如何实现逻辑,以便在每次在控制器上命中时从消费者那里检索事务。

提前致谢。

【问题讨论】:

    标签: spring-boot apache-kafka kafka-consumer-api spring-kafka spring-web


    【解决方案1】:

    好吧,@KafkaListener 产生独立的长期进程,将记录从 Kafka 流式传输到回调。由于您正在谈论 REST GET 事件,因此您别无选择,除非从 ConsumerFactory 获取 KafkaConsumer 并从控制器方法手动调用其 poll()

    【讨论】:

    • 感谢 Artem 的回复,能否请您给我一些相同的指南或文档。
    • 谢谢,我在看代码,好像没有使用spring-kafka,直接使用kafka api。我说的对吗?
    • ???这就是我的回答的意思。 Spring Kafka 不提供 poll API。您必须直接使用KafkaConsumet。尽管您从 Spring Kafka ConsumerFactory 中获得了自动配置属性的好处
    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2019-09-20
    • 1970-01-01
    • 2021-06-15
    • 2022-01-12
    • 1970-01-01
    相关资源
    最近更新 更多