问题
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;
}
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.
Without any kafka template does it work or does it require KafkaTemplate or ReplyingKafkaTemplate beans?
Thanks
回答1:
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 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.
来源:https://stackoverflow.com/questions/56402921/how-sentto-sends-the-message-to-related-topic