【问题标题】:How to call both Kafkalistener with same Topic in spring boot?如何在 Spring Boot 中同时调用具有相同主题的 Kafkalistener?
【发布时间】:2020-05-19 13:09:36
【问题描述】:

我想调用两个具有相同主题配置的不同方法。

假设我有一个 Consumer 项目,它有两个不同的类,具有相同的 kafkalistener 方法。

A 类方法 1:

@KafkaListener(topics = "vijay", groupId = "group_id")
    public void consumeMethodOne(String jsonString) {
        System.out.println("ConsumerPrice1-->"+jsonString);
    }

B 类方法 2:

@KafkaListener(topics = "vijay", groupId = "group_id")
    public void consumeMethodTwo(String jsonString) {
        System.out.println("ConsumerPrice1-->"+jsonString);
    }

还有我的制片人:

@Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    public void send(String value) {
        kafkaTemplate.send("vijay", "This is testing producer");
    }

当我运行这个程序时,只有一个方法执行所以如果我想执行这两种方法,因为它们有相同的主题,那么我该如何设置?

【问题讨论】:

  • 你的意思是你想消费同一个话题两次?
  • 使用相同主题的帖子:stackoverflow.com/questions/59731673/…
  • @Deadpool .. 是的..
  • 您的用例是什么?你为什么要这样做?
  • @JosephRajeevMotha : 我有两个不同的

标签: java spring spring-boot apache-kafka


【解决方案1】:

消费者组是对主题消费者进行负载平衡的一种方式。如果您在同一组中有两个消费者(在您的情况下为“group_id”),那么一次只有一个消费者会消费一条消息。 通常,这就像消费者的应用程序 ID,用于扩展消费者。

但是,如果您希望两个消费者都使用相同的消息,您可以通过给他们两个不同的组 ID 来做到这一点。

@KafkaListener(topics = "vijay", groupId = "group_id2")
    public void consumeMethodTwo(String jsonString) {
        System.out.println("ConsumerPrice1-->"+jsonString);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2021-07-06
    相关资源
    最近更新 更多