在一个单一的弹簧微服务中,apache-kafka,spring-kafka"/>

首页 > 解决方案 > 多次回复Kafka模板在一个单一的弹簧微服务中

问题描述

我正在开发一个微服务,其中我需要使用 spring kafka ReplyingKafkaTemplate 查询两个不同的微服务。

当我用一个微服务尝试这个时,它通过提供一个如下的回复主题来正常工作

@Bean
public ReplyingKafkaTemplate<String, byte[], byte[]> replyKafkaTemplate(ProducerFactory<String, byte[]> pf,
        KafkaMessageListenerContainer<String, byte[]> container) {
    return new ReplyingKafkaTemplate<>(pf, container);

}

@Bean
public ConsumerFactory<String, byte[]> consumerFactory() {
    return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),
            new ByteArrayDeserializer());
}

@Bean
public KafkaMessageListenerContainer<String, byte[]> replyContainer(ConsumerFactory<String, byte[]> cf) {
    ContainerProperties containerProperties = new ContainerProperties(**queryesultTopic**);
    return new KafkaMessageListenerContainer<>(cf, containerProperties);
}

但现在我想要配置两个不同的主题。并想知道如何在 spring kafka 配置中做到这一点。

标签: apache-kafkaspring-kafka

解决方案


当您使用 aReplyingKafkaTemplate.sendAndReceive(ProducerRecord<K, V> record)时,您可以指定一个KafkaHeaders.REPLY_TOPIC标题。

在参考手册中查看更多信息:

有关发送回复的更多信息,请参阅名为“使用 @SendTo 转发侦听器结果”的部分;在这种情况下,我们使用默认标题KafKaHeaders.REPLY_TOPIC来指示回复的主题。


推荐阅读