admin管理员组文章数量:1516870
目录
1、Partitioned topics 分区主题
普通主题仅由单个代理(broker)提供服务,这限制了主题的最大吞吐量。分区主题是一种特殊类型的主题,由多个 broker 处理,因此可以有更高的吞吐量。
一个分区主题实际上由 N 个内部主题实现(其中N是分区数) 。当消息发布到分区主题时,每条消息都会路由到其中一个代理上,Pulsar 会自动处理跨代理的分区分布。
下图说明了这一点:
Topic1 主题有五个分区( P0 到 P4 ),分为三个代理(brokers)。因为分区比 brokers 多,所以其中两个 brokers 需要处理两个分区,而第三个 brokers 只需要处理一个分区(Pulsar 会自动处理分区的分布)。
此主题的消息将广播给两个消费者。 决定了消息应该发布到哪一个分区,而订阅类型决定了消息应该发布给哪一个消费者 。
大多数情况下,可以分别决定路由策略和订阅类型。通常,分区或路由策略根据吞吐量来决定,而订阅类型根据应用程序的应用场景指定。
对于订阅类型来说,分区主题和普通主题没有什么区别,因为分区主题仅决定生产者如何发布消息、消费者如何处理和确认消息。
分区主题需要通过 显式创建。创建主题时可以指定分区数。
(1)Routing modes 路由模式
消息发布到分区主题时,必须指定路由模式 。路由模式决定了消息应该发布到那个分区(即哪个内部主题)。
有三种消息路由模式可用:
| Mode | Description |
|---|---|
| If no key is provided, the producer will publish messages across all partitions in round-robin fashion to achieve maximum throughput. Please note that round-robin is not done per individual message but rather it's set to the same boundary of batching delay, to ensure batching is effective. While if a key is specified on the message, the partitioned producer will hash the key and assign message to a particular partition. This is the default mode. // 如果没有提供Key,生产者将以循环方式跨所有分区发布消息,以实现最大吞吐量。请注意,循环方式不是针对单个消息进行的,而是设置和批量处理相同的边界,确保该批量处理有效。如果在消息上指定了Key,则分区生产者会对该Key进行Hash计算后,并将消息分配给特定分区。 |
单独分区 | If no key is provided, the producer will randomly pick one single partition and publish all the messages into that partition. While if a key is specified on the message, the partitioned producer will hash the key and assign message to a particular partition. // 如果没有提供 Key,生产者将随机选择一个分区,并将所有消息发布到该分区中。如果在消息上指定了Key,则分区生产者会对该Key进行Hash计算后,并将消息分配给特定分区。 |
| Use custom message router implementation that will be called to determine the partition for a particular message. User can create a custom routing mode by using the and implementing the interface. // 使用自定义的消息路由策略将特定的消息发布到分区。用户可以使用Java客户端并实现MessageRouter接口来创建自定义路由模式。 |
(2)Ordering guarantee 排序保证
消息的顺序与消息路由模式和 Message Key 有关。通常,用户会希望按 Key 分区来保证排序。
如果有一条消息附加了 Key,那么在使用单独分区(SinglePartition)或循环分区(RoundRobinPartition)模式时,消息将根据 ProducerBuilder 中 HashingScheme 指定的哈希策略路由到相应的分区。 // 根据 key 排序,或者根据生产者排序
| Ordering guarantee | Description | Routing Mode and Key |
|---|---|---|
| Per-key-partition | All the messages with the same key will be in order and be placed in same partition. // 所有具有相同 key 的消息将按顺序排列并放置在同一分区中。 |
Use either
// 提供每条消息的key |
| Per-producer | All the messages from the same producer will be in order. // 来自同一生产者的所有消息将按顺序排列。 |
Use
// 使用单独分区,不提供消息的 key |
(3) Hashing scheme( Hash 策略)
是一个枚举类,表示为一条特定的消息选择分区时可用的标准哈希函数集。
有两种类型的标准哈希函数可用:JavaStringHash 和 Murmur3_32Hash。producer 使用的默认哈希函数是 JavaStringHash。请注意,当生产者来自多种不同的语言客户端时,JavaStringHash 将不再有用,在这种情况下,建议使用 Murmur3_32Hash。
2、Non-persistent topics 非持久性主题
默认情况下,Pulsar 将所有未确认的消息持久存储在多个 BookKeeper(存储节点)上。因此,持久主题上的消息数据可以在代理(broker)重新启动和消费者故障切换后继续存在。
然而,Pulsar 也支持 。这些主题上的消息不会持久化到磁盘,而只存在于内存中。当使用非持久性主题时, 停止 Pulsar 代理或断开主题订阅者的连接,意味着该(非持久性)主题上所有传输中的消息都会丢失 ,意味着这些消息丢失客户端也可能会看到。
非持久性主题的名称格式如下所示(请注意名称中的非持久性):
non-persistent://tenant/namespace/topic
更多非持久性主题的详细描述,请单击 。
非持久性主题消去除了消息持久化的步骤,所以在一些情况下会比持久化消息更加快一点,但是这样做,会毁损失一部分 pulsar 的核心优势。
3、System topic 系统主题
系统主题是 Pulsar 内部使用的预定义主题 。它可以是持久性主题,也可以是非持久性主题。
系统主题用于实现已经确定的功能并用来消除对第三方组件的依赖 ,比如,事务、心跳检测、主题策略和资源分组服务。系统主题可以使这些功能的实现变得更简单、可靠和灵活。以心跳检测(heartbeat detections)为例,您可以充分的利用系统主题来进行心跳检测,让内部的 producer/reader 可以去指定的心跳命名空间下 produce/consume 消息,通过这种方式可以检测当前的服务是否仍处于活动状态。
不同的命名空间,有不同的系统主题。下表概述了每个命名空间中的可用系统主题:
| Namespace | TopicName | Domain | Count | Usage |
|---|---|---|---|---|
| pulsar/system |
transaction_coordinator_assign_${id}
| Persistent | Default 16 | Transaction coordinator |
| pulsar/system |
_transaction_log${tc_id}
| Persistent | Default 16 | Transaction log |
| pulsar/system |
resource-usage
| Non-persistent | Default 4 | Resource group service |
| host/port |
heartbeat
| Persistent | 1 | Heartbeat detection 心跳检测 |
| User-defined-ns | Persistent | Default 4 | Topic events | |
| User-defined-ns |
__transaction_buffer_snapshot
| Persistent | One per namespace | Transaction buffer snapshots |
| User-defined-ns |
${topicName}__transaction_pending_ack
| Persistent | One per every topic subscription acknowledged with transactions | Acknowledgements with transactions |
注意事项
- 系统主题无法被用户创建 。
- 默认情况下,系统主题是被禁用的,如果你想要启用系统主题,可以在 conf / broker.conf 或者 conf / standalone.conf 文件中进行如下配置:
systemTopicEnabled=true topicLevelPoliciesEnabled=true
版权声明:本文标题:从零开始:Pulsar中分区主题、非持久及系统主题设定教程 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/biancheng/1770647591a3256979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论