apache-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 配置中做到这一点。
解决方案
当您使用 aReplyingKafkaTemplate.sendAndReceive(ProducerRecord<K, V> record)
时,您可以指定一个KafkaHeaders.REPLY_TOPIC
标题。
在参考手册中查看更多信息:
有关发送回复的更多信息,请参阅名为“使用 @SendTo 转发侦听器结果”的部分;在这种情况下,我们使用默认标题
KafKaHeaders.REPLY_TOPIC
来指示回复的主题。
推荐阅读
- kubernetes - 如何将普罗米修斯适配器移动到另一个命名空间?
- verilog - “并发分配或输出端口连接的目标应该是网络类型”
- c# - 三元运算符的用法没有错误的情况
- flutter - 用倒色进行颤振绘制
- scala - 在 Scala 中序列化函数时避免捕获 `this`
- c++ - [[maybe_unused]] 与结构化绑定?
- c# - gRPC C#服务器端,等到客户端关闭连接?
- xamarin.forms - Xamarin Forms 4.4 + 中是否仍需要此 ExportRenderer
- html - 如何在 .ts 文件中以角度创建按钮并添加点击事件
- javascript - 正则表达式 - 在最少两个字符之后