Kafka主题作为聊天室标识符

发布于 2025-02-10 01:09:59 字数 214 浏览 2 评论 0原文

我对一个可能有可能的聊天室的消息传递聊天应用程序感兴趣。 我试图了解Kafka主题是否可以用作Kafka概念观点的聊天室标识符?

由于聊天室通常是在运行时创建的,因此意味着

  • 应该在运行时创建Kafka主题。是否可以?
  • 用户应该能够在创建新主题(聊天室)。是否可以?
  • 会有很多聊天室。 Kafka有大量主题是不好的还是可以的?

I am interested in a messaging chat app where different chatroom can be possible.
I try to understand if Kafka topics can be used as chatroom identifiers from Kafka conceptual point of view?

As chatrooms are normally created in a runtime then it means that

  • Kafka topics should be created in a runtime. Is it possible?
  • Users should be able to subscribe to the new topics (chatrooms) as soon as they are created. Is it possible?
  • There will be a lot of chatrooms. Is it bad or ok for Kafka to have huge number of topics?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2025-02-17 01:09:59

Kafka主题应在运行时创建。可能吗?

是的。 kafka adminclient 用于管理功能,包括列表,创建和删除主题。

我们可以使用肌肉图创建新主题:

Properties props = new Properties();
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
AdminClient admin = AdminClient.create(props);


CreateTopicsResult newTopic = admin.createTopics(Collection.singletonList(new NewTopic(TOPIC_NAME, NUM_PARTITIONS, REP_FACTOR)));

admin.close(Duration.ofSeconds(30));

上述代码来自Kafka:“权威指南”第二版,第5章以编程方式管理Apache Kafka,它讨论了很多有关adminclient的讨论。

对于Spring-Kafka,提供 createModifyTopics()方法。

用户应该在创建新主题(聊天室)时订阅。可能吗?

是的。我们可以在.subscribe()中使用正则表达式:

表达式可以匹配多个主题名称,如果某人创建一个具有匹配名称的新主题,那么重新平衡将几乎立即发生,消费者将开始从新主题中消费。

例如,要订阅所有测试主题,我们可以致电

consumer.subscribe(Pattern.compile("test.*"));

会有很多聊天室。 Kafka有大量主题是不好的还是可以的?

没关系:我可以在kafka群集中有100万个主题吗?

Kafka topics should be created in a runtime. Is it possible?

Yes. Kafka AdminClient provides a programmatic API for administrative functionality, including listing, creating and deleting topics.

We can use createTopics to create new topics:

Properties props = new Properties();
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
AdminClient admin = AdminClient.create(props);


CreateTopicsResult newTopic = admin.createTopics(Collection.singletonList(new NewTopic(TOPIC_NAME, NUM_PARTITIONS, REP_FACTOR)));

admin.close(Duration.ofSeconds(30));

The above code comes from Kafka: The Definitive Guide 2nd Edition, Chapter 5 Managing Apache Kafka Programmatically, which discusses a lot about AdminClient.

For spring-kafka, the KafkaAdmin provides createOrModifyTopics() method.

Users should be able to subscribe to the new topics (chatrooms) as soon as they are created. Is it possible?

Yes. We can use regular expressions in .subscribe():

The expression can match multiple topic names, and if someone creates a new topic with a name that matches, a rebalance will happen almost immediately and the consumers will start consuming from the new topic.

For example, to subscribe to all test topics, we can call

consumer.subscribe(Pattern.compile("test.*"));

There will be a lot of chatrooms. Is it bad or ok for Kafka to have huge number of topics?

It's okay: Can I have 100s of thousands of topics in a Kafka Cluster?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文