【问题标题】:How @SentTo sends the message to related topic?@SentTo 如何将消息发送到相关主题?
【发布时间】:2019-10-17 13:35:05
【问题描述】:

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


 @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 的情况下返回消息。

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

谢谢

【问题讨论】:

    标签: apache-kafka kafka-consumer-api kafka-producer-api spring-kafka


    【解决方案1】:

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

    Assert.state(replyTopic == null || this.replyTemplate != null,
            "a KafkaTemplate is required to support replies");
    

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

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

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

    【讨论】:

    • 不要对旧答案提出新问题;如果没有看到您的设置,就不可能推测问题。问一个显示代码和配置的新问题。
    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多