@SentTo 如何将消息发送到相关主题? [英] How @SentTo sends the message to related topic?

查看:32
本文介绍了@SentTo 如何将消息发送到相关主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rest 控制器中使用 ReplyingKafkaTemplate 来返回同步响应.我也在设置标题 REPLY_TOPIC.对于监听器微服务部分,

<预><代码>@KafkaListener(topics = "${kafka.topic.request-topic}")@发给公共模型监听(模型 请求)抛出 InterruptedException {SumModel 模型 = request.getRequest();int sum = model.getNumber1() + model.getNumber2();SumResp resp = new SumResp(sum);request.setReply(resp);request.setAdditionalProperty("sum", sum);退货请求;}

我的问题是,@sentTo 注释如何设法将消息发布到标题中提供的主题.应该有一个 KafkaTemplate bean 还是不需要它?因为我们只是在不使用任何 KafkaTemplate 或 ReplyingKafkaTemplate bean 的情况下返回消息.

如果没有任何 kafka 模板,它可以工作还是需要 KafkaTemplate 或 ReplyingKafkaTemplate bean?

谢谢

解决方案

是的,使用模板发送回复...

Assert.state(replyTopic == null || this.replyTemplate != null,需要 KafkaTemplate 来支持回复");

如果应用程序上下文中只有一个模板,Spring Boot 会自动将模板连接到侦听器容器工厂.

map.from(this.replyTemplate).to(factory::setReplyTemplate);

如果上下文中没有模板,Boot 也会自动配置模板.

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

解决方案

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.

这篇关于@SentTo 如何将消息发送到相关主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆