@SentTo如何将消息发送到相关主题? [英] How @SentTo sends the message to related topic?
问题描述
我在Rest控制器中使用ReplyingKafkaTemplate返回同步响应.我还要设置标题REPLY_TOPIC.对于侦听器微服务部分,
I am using ReplyingKafkaTemplate in my Rest controller to return the synchronous response. I am also setting header REPLY_TOPIC. For listener microservice part,
@KafkaListener(topics = "${kafka.topic.request-topic}")
@SendTo
public Model listen(Model<SumModel,SumResp> request) throws InterruptedException {
SumModel model = request.getRequest();
int sum = model.getNumber1() + model.getNumber2();
SumResp resp = new SumResp(sum);
request.setReply(resp);
request.setAdditionalProperty("sum", sum);
return request;
}
我的问题是,@sentTo批注如何将消息发布到标题中提供的主题.应该有一个KafkaTemplate bean还是不需要它?由于我们只是在不使用任何KafkaTemplate或ReplyingKafkaTemplate bean的情况下返回消息.
My question is, How @sentTo annotation manage to publish the message to the topic provided in the header. There should be a KafkaTemplate bean or it does not need it? As we are just returning the message without using any KafkaTemplate or ReplyingKafkaTemplate bean.
没有任何kafka模板,它可以工作还是需要KafkaTemplate或ReplyingKafkaTemplate bean?
Without any kafka template does it work or does it require KafkaTemplate or ReplyingKafkaTemplate beans?
谢谢
推荐答案
是的,使用模板发送回复...
Yes, a template is used to send the reply...
Assert.state(replyTopic == null || this.replyTemplate != null,
"a KafkaTemplate is required to support replies");
如果应用程序上下文中只有一个模板,Spring Boot会自动将模板连接到侦听器容器工厂.
Spring Boot automatically wires a template into the listener container factory if there is exactly one in the application context.
map.from(this.replyTemplate).to(factory::setReplyTemplate);
如果上下文中还没有模板,启动也会自动配置模板.
Boot also auto configures a template if there is not one already in the context.
这篇关于@SentTo如何将消息发送到相关主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!