关于apache:博文推荐|零经验玩转隔离策略多个-Pulsar-集群

22次阅读

共计 21718 个字符,预计需要花费 55 分钟才能阅读完成。

舒适提醒:本文是「Pulsar 傻瓜手册」,手把手为零教训小白排坑避雷。只有依照指引操作,成功率 100% 喔!

本文是「Pulsar 隔离策略系列」的第 2 篇,该系列的第 1 篇博客 —「深度解析如何在 Pulsar 中实现隔离」— 重点介绍了 Pulsar 隔离策略以及如何通过以下 3 种形式实现资源隔离:

  • 多个 Pulsar 集群
  • 共享 BookKeeper 集群
  • 单个 Pulsar 集群

本文将具体解说第 1 种形式,即如何在「多个 Pulsar 集群」的环境中,玩转 Pulsar 隔离策略,并为以下操作提供具体步骤:

  • 验证数据隔离。
  • 同步并迁徙集群数据。
  • 扩缩容节点。

筹备环境

本文以在 macOS(版本 11.2.3,内存 8G)上操作为示例。

如下图所示,本文部署 2 个 Pulsar 集群,每个集群提供以下服务:

  • 1 个 ZooKeeper 节点
  • 1 个 bookie 节点
  • 1 个 broker 节点

软件要求

Java 8

环境详情

下文行将部署 2 个 Pulsar 集群,组件详情如下表所示。

部署筹备

  1. 下载 Pulsar 并解压。本文以装置 Pulsar 2.7.0 为例。
  2. 在本地任意地位,依照以下目录构造,创立相应的空文件夹。
|-separate-clusters
    |-configuration-store
        |-zk1
    |-cluster1
        |-zk1
        |-bk1
        |-broker1
    |-cluster2
        |-zk1
        |-bk1
        |-broker1
  1. 复制解压后 Pulsar 文件夹中的内容至上一步创立的每个文件夹中。
  2. 启动 configuration store。

Configuration store 为 Pulsar 实例提供 跨集群 配置管理和工作协调。Pulsar 集群 1 和 Pulsar 集群 2 共享 configuration store。

cd configuration-store/zk1
bin/pulsar-daemon start configuration-store

部署 Pulsar 集群 1

  1. 启动 local ZooKeeper。为每个 Pulsar 集群部署 1 个 local ZooKeeper,它负责为该集群治理配置和协调工作。
cd cluster1/zk1
bin/pulsar-daemon start zookeeper
  1. 初始化元数据。设置好 configuration store 和 local ZooKeeper 后,你须要向 ZooKeeper 写入元数据。
cd cluster1/zk1
bin/pulsar initialize-cluster-metadata \
  --cluster cluster1 \
  --zookeeper localhost:2181 \
  --configuration-store localhost:2184 \
  --web-service-url http://localhost:8080/ \
  --web-service-url-tls https://localhost:8443/ \
  --broker-service-url pulsar://localhost:6650/ \
  --broker-service-url-tls pulsar+ssl://localhost:6651/
  1. 部署 BookKeeper。BookKeeper 为 Pulsar 提供音讯长久化存储。每个 Pulsar broker 领有 bookie。BookKeeper 集群和 Pulsar 集群共享 local ZooKeeper。

1). 配置 bookie。

更改 cluster1/bk1/conf/bookkeeper.conf 文件中以下配置项的值。

allowLoopback=true
prometheusStatsHttpPort=8002
httpServerPort=8002

2). 启动 bookie。

cd cluster1/bk1
bin/pulsar-daemon start bookie

验证是否胜利启动 bookie。

bin/bookkeeper shell bookiesanity

输入

Bookie sanity test succeeded
  1. 部署 broker。

1)配置 broker。

更改 cluster1/broker1/conf/broker.conf 文件中以下配置项的值。

zookeeperServers=127.0.0.1:2181
configurationStoreServers=127.0.0.1:2184
clusterName=cluster1
managedLedgerDefaultEnsembleSize=1
managedLedgerDefaultWriteQuorum=1
managedLedgerDefaultAckQuorum=1

2)启动 broker。

cd cluster1/broker1
bin/pulsar-daemon start broker

部署 Pulsar 集群 2

  1. 部署 local ZooKeeper。

1)配置 local ZooKeeper。

  • 更改 cluster2/zk1/conf/zookeeper.conf 文件中以下配置项的值。
clientPort=2186admin.serverPort=9992
  • 将以下配置项增加至 cluster2/zk1/conf/pulsar_env.sh 文件。
OPTS="-Dstats_server_port=8011"

2)启动 local ZooKeeper。

cd cluster2/zk1
bin/pulsar-daemon start zookeeper

2. 初始化元数据。

bin/pulsar initialize-cluster-metadata \
  --cluster cluster2 \
  --zookeeper localhost:2186 \
  --configuration-store localhost:2184 \
  --web-service-url http://localhost:8081/ \
  --web-service-url-tls https://localhost:8444/ \
  --broker-service-url pulsar://localhost:6660/ \
  --broker-service-url-tls pulsar+ssl://localhost:6661/
  1. 部署 BookKeeper。

1)配置 bookie。

更改 cluster2/bk1/conf/bookkeeper.conf 文件中以下配置项的值。

bookiePort=3182
zkServers=localhost:2186
allowLoopback=true
prometheusStatsHttpPort=8003
httpServerPort=8003

2)启动 bookie。

cd cluster2/bk1
bin/pulsar-daemon start bookie

验证是否胜利启动 bookie。

bin/bookkeeper shell bookiesanity

输入

Bookie sanity test succeeded

5. 部署 broker。

1)配置 broker。

  • 更改 cluster2/broker1/conf/broker.conf 文件中以下配置项的值。
clusterName=cluster2
zookeeperServers=127.0.0.1:2186
configurationStoreServers=127.0.0.1:2184
brokerServicePort=6660
webServicePort=8081
managedLedgerDefaultEnsembleSize=1
managedLedgerDefaultWriteQuorum=1
managedLedgerDefaultAckQuorum=1

•更改 cluster2/broker1/conf/client.conf 文件中以下配置项的值。

webServiceUrl=http://localhost:8081/
brokerServiceUrl=pulsar://localhost:6660/

2)启动 broker。

cd cluster2/broker1
bin/pulsar-daemon start broker

验证数据隔离

本章验证 2 个 Pulsar 集群中的数据是否隔离。

  1. 创立 namespace1,并将 namespace1 调配给 cluster1。

提醒:namespace 的命名规定是 /。更多对于 namespace 的信息,参阅这里。

cd cluster1/broker1
bin/pulsar-admin namespaces create -c cluster1 public/namespace1

验证后果

bin/pulsar-admin namespaces list public

输入

"public/default"
"public/namespace1"

2. 设置 namespace1 的音讯保留策略。

留神:如果不设置音讯保留策略且 topic 未被订阅,一段时间后,topic 的数据会被主动清理。

bin/pulsar-admin namespaces set-retention -s 100M -t 3d public/namespace1

3. 在 namespace1 创立 topic1,并应用写入 1000 条数据。

提醒:pulsar-client 是发送和生产数据的命令行工具。更多对于 Pulsar 命令行工具的信息,参阅这里。

bin/pulsar-client produce -m 'hello c1 to c2' -n 1000 public/namespace1/topic1
09:56:34.504 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 1000 messages successfully produced

验证后果

bin/pulsar-admin --admin-url http://localhost:8080 topics stats-internal public/namespace1/topic1

输入

entriesAddedCounter 显示减少了 1000 条数据。

{"entriesAddedC{"entriesAddedCounter": 1000,"numberOfEntries": 1000,"totalSize": 65616,"currentLedgerEntries": 1000,"currentLedgerSize": 65616,"lastLedgerCreatedTimestamp":"2021-04-22T10:24:00.582+08:00","waitingCursorsCount": 0,"pendingAddEntriesCount": 0,"lastConfirmedEntry":"4:999","state":"LedgerOpened","ledgers": [ {"ledgerId": 4,"entries": 0,"size": 0,"offloaded" : false} ],
 "cursors" : { },
 "compactedLedger" : {
   "ledgerId" : -1,
   "entries" : -1,
   "size" : -1,
   "offloaded" : false
 }
}ounter": 1000,"numberOfEntries": 1000,"totalSize": 65616,"currentLedgerEntries": 1000,"currentLedgerSize": 65616,"lastLedgerCreatedTimestamp":"2021-04-22T10:24:00.582+08:00","waitingCursorsCount": 0,"pendingAddEntriesCount": 0,"lastConfirmedEntry":"4:999","state":"LedgerOpened","ledgers": [{"ledgerId": 4,"entries": 0,"size": 0,"offloaded": false} ],"cursors": {},"compactedLedger": {"ledgerId": -1,"entries": -1,"size": -1,"offloaded" : false}}

4. 通过 cluster2(localhost:8081)查看 public/namespace1/topic1 的数据。

bin/pulsar-admin --admin-url http://localhost:8081 topics stats-internal public/namespace1/topic1

输入

查看失败。打印信息显示 public/namespace1 仅调配至 cluster1,未调配至 cluster 2。此时验证了数据已隔离。

Namespace missing local cluster name in clusters list: local_cluster=cluster2 ns=public/namespace1 clusters=[cluster1]
Reason: Namespace missing local cluster name in clusters list: local_cluster=cluster2 ns=public/namespace1 clusters=[cluster1]

5. 在 cluster2 中,向 public/namespace1/topic1 写入数据。

cd cluster2/broker1
bin/pulsar-client produce -m 'hello c1 to c2' -n 1000 public/namespace1/topic1

输入

结果显示写入音讯数量为 0,操作失败,因为 namespace1 仅调配至 cluster1,未调配至 cluster 2。此时验证了数据已隔离。

12:09:50.005 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 0 messages successfully produced

迁徙数据

以下步骤接着前文「验证数据隔离」持续操作。

确认数据已隔离后,本章讲述如何同步(应用跨地区复制性能)并迁徙集群数据。

  1. 调配 namespace1 至 cluster2,即增加 cluster2 至 namesapce1 的 cluster 列表。

该步骤启用跨地区复制性能,同步 cluster1 和 cluster2 的数据。

bin/pulsar-admin namespaces set-clusters --clusters cluster1,cluster2 public/namespace1

验证后果

bin/pulsar-admin namespaces get-clusters public/namespace1

输入

"cluster1"
"cluster2"

2. 查看 cluster2 是否有 topic1。

bin/pulsar-admin --admin-url http://localhost:8081 topics stats-internal public/namespace1/topic1

输入

结果显示 cluster2 的 topic1 有 1000 条数据,阐明 cluster1 的 topic1 数据已胜利复制至 cluster2。

{
  "entriesAddedCounter" : 1000,
  "numberOfEntries" : 1000,
  "totalSize" : 75616,
  "currentLedgerEntries" : 1000,
  "currentLedgerSize" : 75616,
  "lastLedgerCreatedTimestamp" : "2021-04-23T12:02:52.929+08:00",
  "waitingCursorsCount" : 1,
  "pendingAddEntriesCount" : 0,
  "lastConfirmedEntry" : "1:999",
  "state" : "LedgerOpened",
  "ledgers" : [ {
    "ledgerId" : 1,
    "entries" : 0,
    "size" : 0,
    "offloaded" : false
  } ],
  "cursors" : {
    "pulsar.repl.cluster1" : {
      "markDeletePosition" : "1:999",
      "readPosition" : "1:1000",
      "waitingReadOp" : true,
      "pendingReadOps" : 0,
      "messagesConsumedCounter" : 1000,
      "cursorLedger" : 2,
      "cursorLedgerLastEntry" : 2,
      "individuallyDeletedMessages" : "[]",
      "lastLedgerSwitchTimestamp" : "2021-04-23T12:02:53.248+08:00",
      "state" : "Open",
      "numberOfEntriesSinceFirstNotAckedMessage" : 1,
      "totalNonContiguousDeletedMessagesRange" : 0,
      "properties" : {}}
  },
  "compactedLedger" : {
    "ledgerId" : -1,
    "entries" : -1,
    "size" : -1,
    "offloaded" : false
  }
}

3. 迁徙 cluster1 的 producer 和 consumer 至 cluster2。

PulsarClient pulsarClient1 = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build();
// migrate the client to cluster2 pulsar://localhost:6660
PulsarClient pulsarClient2 = PulsarClient.builder().serviceUrl("pulsar://localhost:6660").build();

4. 从 namespace1 的 cluster 列表中移除 cluster1。

bin/pulsar-admin namespaces set-clusters --clusters cluster2 public/namespace1

5. 查看 cluster1 的 topic1 的数据。

cd cluster1/broker1
bin/pulsar-admin --admin-url http://localhost:8080 topics stats-internal public/namespace1/topic1

输入

结果显示数据为空,阐明数据已从 cluster1 的 topic1 中胜利移除。

{
  "entriesAddedCounter" : 0,
  "numberOfEntries" : 0,
  "totalSize" : 0,
  "currentLedgerEntries" : 0,
  "currentLedgerSize" : 0,
  "lastLedgerCreatedTimestamp" : "2021-04-23T15:20:08.1+08:00",
  "waitingCursorsCount" : 1,
  "pendingAddEntriesCount" : 0,
  "lastConfirmedEntry" : "3:-1",
  "state" : "LedgerOpened",
  "ledgers" : [ {
    "ledgerId" : 3,
    "entries" : 0,
    "size" : 0,
    "offloaded" : false
  } ],
  "cursors" : {
    "pulsar.repl.cluster2" : {
      "markDeletePosition" : "3:-1",
      "readPosition" : "3:0",
      "waitingReadOp" : true,
      "pendingReadOps" : 0,
      "messagesConsumedCounter" : 0,
      "cursorLedger" : 4,
      "cursorLedgerLastEntry" : 0,
      "individuallyDeletedMessages" : "[]",
      "lastLedgerSwitchTimestamp" : "2021-04-23T15:20:08.122+08:00",
      "state" : "Open",
      "numberOfEntriesSinceFirstNotAckedMessage" : 1,
      "totalNonContiguousDeletedMessagesRange" : 0,
      "properties" : {}}
  },
  "compactedLedger" : {
    "ledgerId" : -1,
    "entries" : -1,
    "size" : -1,
    "offloaded" : false
  }
}

至此,咱们已将 cluster1 的 topic1 数据胜利复制至 cluster2,之后移除了 cluster1 的 topic1 数据。

扩缩容节点

本章讲述如何扩缩容 broker 和 bookie 节点。

Broker

减少 broker 节点

本示例在 cluster1/broker1 创立 2 个 partitioned topic,再减少 2 个 broker 节点。之后卸载 partitioned topic 数据,并查看数据在 3 个 broker 之间的分配情况。

  1. 查看 cluster1 的 broker 信息。
cd/cluster1/broker1
bin/pulsar-admin brokers list cluster1

输入

后果阐明以后 cluster1 只有 broker1。

"192.168.0.105:8080"
  1. 在 cluster1/broker1 上创立 2 个 partitioned topic。

为 partitioned-topic1 创立 6 个分区,为 partitioned-topic2 创立 7 个分区。

bin/pulsar-admin topics create-partitioned-topic -p 6 public/namespace1/partitioned-topic1
bin/pulsar-admin topics create-partitioned-topic -p 7 public/namespace1/partitioned-topic2

查看后果。

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic1

输入

结果显示,partitioned-topic1 所有数据属于 broker1。

"persistent://public/namespace1/partitioned-topic1-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-2    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-3    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-5    pulsar://192.168.0.105:6650"

输出

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic2

输入

结果显示,partitioned-topic2 所有数据属于 broker1。

"persistent://public/namespace1/partitioned-topic2-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-2    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-3    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-5    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-6    pulsar://192.168.0.105:6650"
  1. 新增 2 个 broker 节点:broker2 和 broker3。

1)部署筹备。在 cluster1 文件夹中创立 broker2 和 broker3 文件夹,复制解压后 Pulsar 文件夹中的内容至 broker2 和 broker3 文件夹。

|-separate-clusters
    |-configuration-store
        |-zk1
    |-cluster1
        |-zk1
        |-bk1
        |-broker1
        |-broker2
        |-broker3
    |-cluster2
        |-zk1
        |-bk1
        |-broker1

2)部署 broker。

a). 配置 broker。

b). 启动 broker。

c). 查看 cluster1 中已启动的 broker。

bin/pulsar-admin brokers list cluster1

输入

"192.168.0.105:8080" // broker1
"192.168.0.105:8082" // broker2
"192.168.0.105:8083" // broker3
  1. 卸载 namespace 1 中 partitioned-topic1 的数据。
bin/pulsar-admin namespaces unload public/namespace1

验证后果。

1). 查看 partitioned-topic1 的数据分配情况。

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic1

输入

结果显示,partitioned-topic1 的数据平均分配在 broker1、broker2 和 broker3 上。

"persistent://public/namespace1/partitioned-topic1-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-1    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic1-partition-2    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic1-partition-3    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic1-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-5    pulsar://192.168.0.105:6653"

2). 查看 partitioned-topic2 的数据分配情况。

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic2

结果显示,partitioned-topic2 的数据平均分配在 broker1、broker2 和 broker3 上。

输入

"persistent://public/namespace1/partitioned-topic2-partition-0    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic2-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-2    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic2-partition-3    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic2-partition-4    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic2-partition-5    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-6    pulsar://192.168.0.105:6653"

缩小 broker 节点

以下步骤接着前文「如何减少 broker 节点」持续操作。

本示例在 cluster1 中缩小 1 个 broker 节点,并查看 partitioned topic 数据在其余 2 个 broker 之间的分配情况。

  1. 缩小 1 个 broker 节点,即进行 broker3。
cd/cluster1/broker3
bin/pulsar-daemon stop broker

验证后果。

bin/pulsar-admin brokers list cluster1

输入

结果显示,以后 cluster1 仅启动了 broker1 和 broker2。

"192.168.0.105:8080" // broker1
"192.168.0.105:8082" // broker2
  1. 查看 partitioned-topic1 数据的分配情况。
bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic1

输入

结果显示,partitioned-topic1 数据平均分配至 broker1 和 broker2,即原属于 broker3 的数据已被从新平均分配至 broker1 和 broker2。

"persistent://public/namespace1/partitioned-topic1-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-2    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic1-partition-3    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic1-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-5    pulsar://192.168.0.105:6650"

同理,partitioned-topic2 的数据也被平均分配至 broker1 和 broker2。

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic2

输入

"persistent://public/namespace1/partitioned-topic2-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-2    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic2-partition-3    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic2-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-5    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-6    pulsar://192.168.0.105:6652"

Bookie

减少 bookie 节点

本示例在 cluster1/bookkeeper 1 已有 bookie1,减少 2 个 bookie 节点后,向 topic1 写入数据,并查看数据是否保留了多个正本。

  1. 查看 cluster1 的 bookie 信息。
cd cluster1/bk1
bin/bookkeeper shell listbookies -rw -h

输入

后果阐明以后 cluster1 只有 bookie1。

12:31:34.933 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - ReadWrite Bookies :
12:31:34.946 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3181, IP:192.168.0.105, Port:3181, Hostname:192.168.0.105

2. 容许 3 个 bookie 节点服务。

更改 cluster1/broker1/conf/broker.conf 文件中以下配置项的值。

managedLedgerDefaultEnsembleSize=3 // 指定 bookie 节点服务的数量
managedLedgerDefaultWriteQuorum=3 // 指定数据正本写入的数量
managedLedgerDefaultAckQuorum=2  // 指定数据胜利写入几个正本后,数据才算写入胜利

3. 重启 broker1,使配置失效。

cd cluster1/broker1
bin/pulsar-daemon stop broker
bin/pulsar-daemon start broker

4. 设置 public/default 的音讯保留策略。

留神:如果不设置音讯保留策略且 topic 未被订阅,一段时间后,topic 的数据会被主动清理。

cd cluster1/broker1
bin/pulsar-admin namespaces set-retention -s 100M -t 3d public/default

5. 在 public/default 创立 topic1,并写入 100 条数据。

bin/pulsar-client produce -m 'hello' -n 100 topic1

输入

结果显示 bookie 节点数量有余导致数据写入失败。

···
12:40:38.886 [pulsar-client-io-1-1] WARN  org.apache.pulsar.client.impl.ClientCnx - [id: 0x56f92aff, L:/192.168.0.105:53069 - R:/192.168.0.105:6650] Received error from server: org.apache.bookkeeper.mledger.ManagedLedgerException: Not enough non-faulty bookies available
...
12:40:38.886 [main] ERROR org.apache.pulsar.client.cli.PulsarClientTool - Error while producing messages
…
12:40:38.890 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 0 messages successfully produced
  1. 新增 2 个 bookie 节点:bookie2 和 bookie3。

1). 部署筹备。

在 cluster1 中新增 bk2 和 bk3 文件夹,复制解压后 Pulsar 文件夹中的内容至 bk2 和 bk3 文件夹。

|-separate-clusters
    |-configuration-store
        |-zk1
    |-cluster1
        |-zk1
        |-bk1
        |-bk2
        |-bk3
        |-broker1
    |-cluster2
        |-zk1
        |-bk1
        |-broker1

2). 部署 bookie。

a). 配置 bookie。

b). 启动 bookie。

c). 查看 cluster1 中已启动的 bookie。

bin/bookkeeper shell listbookies -rw -h

输入

结果显示 cluster 1 中已启动 3 个 bookie:

  • bookie1:192.168.0.105:3181
  • bookie2:192.168.0.105:3183
  • bookie3:192.168.0.105:3184
12:12:47.574 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3183, IP:192.168.0.105, Port:3183, Hostname:192.168.0.105 
12:12:47.575 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3184, IP:192.168.0.105, Port:3184, Hostname:192.168.0.105
12:12:47.576 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3181, IP:192.168.0.105, Port:3181, Hostname:192.168.0.105 

7. 设置 public/default 的音讯保留策略。

留神:如果不设置音讯保留策略且 topic 未被订阅,一段时间后,topic 的数据会被主动清理。

cd cluster1/broker1bin/pulsar-admin namespaces set-retention -s 100M -t 3d public/default
  1. 在 public/default 创立 topic1,并写入 100 条数据。
bin/pulsar-client produce -m 'hello' -n 100 topic1

输入

结果显示数据写入胜利。

...
12:17:40.222 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 100 messages successfully produced
  1. 查看 topic1 的信息。
bin/pulsar-admin topics stats-internal topic1

输入

结果显示 ledgerId 5 保留了 topic1 的数据。

{
  "entriesAddedCounter" : 100,
  "numberOfEntries" : 100,
  "totalSize" : 5500,
  "currentLedgerEntries" : 100,
  "currentLedgerSize" : 5500,
  "lastLedgerCreatedTimestamp" : "2021-05-11T12:17:38.881+08:00",
  "waitingCursorsCount" : 0,
  "pendingAddEntriesCount" : 0,
  "lastConfirmedEntry" : "5:99",
  "state" : "LedgerOpened",
  "ledgers" : [ {
    "ledgerId" : 5,
    "entries" : 0,
    "size" : 0,
    "offloaded" : false
  } ],
  "cursors" : { },
  "compactedLedger" : {
    "ledgerId" : -1,
    "entries" : -1,
    "size" : -1,
    "offloaded" : false
  }
}
  1. 查看 ledgerid 5 存储在哪些 bookie 节点上。
bin/bookkeeper shell ledgermetadata -ledgerid 5

输入

结果显示正如前文所配置,ledgerid 5 存储在 bookie1(3181)、bookie2(3183)和 bookie3(3184)上。

...
12:23:17.705 [main] INFO  org.apache.bookkeeper.tools.cli.commands.client.LedgerMetaDataCommand - ledgerID: 5
12:23:17.714 [main] INFO  org.apache.bookkeeper.tools.cli.commands.client.LedgerMetaDataCommand - LedgerMetadata{formatVersion=3, ensembleSize=3, writeQuorumSize=3, ackQuorumSize=2, state=OPEN, digestType=CRC32C, password=base64:, ensembles={0=[192.168.0.105:3184, 192.168.0.105:3181, 192.168.0.105:3183]}, customMetadata={component=base64:bWFuYWdlZC1sZWRnZXI=, pulsar/managed-ledger=base64:cHVibGljL2RlZmF1bHQvcGVyc2lzdGVudC90b3BpYzE=, application=base64:cHVsc2Fy}}
...

缩小 bookie 节点

以下步骤接着前文「如何减少 bookie 节点」持续操作。

本示例在 cluster1 中缩小 2 个 bookie 节点,再向 topic2 写入数据,并查看数据保留在哪些节点。

  1. 容许 1 个 bookie 节点服务。

更改 cluster1/broker1/conf/broker.conf 文件中以下配置项的值。

managedLedgerDefaultEnsembleSize=1 // 指定 bookie 节点服务的数量
managedLedgerDefaultWriteQuorum=1 // 指定数据正本写入的数量
managedLedgerDefaultAckQuorum=1  // 指定数据胜利写入几个正本后,数据才算写入胜利

2. 重启 broker1,使配置失效。

cd cluster1/broker1
bin/pulsar-daemon stop broker
bin/pulsar-daemon start broker
  1. 查看 cluster1 的 bookie 信息。
cd cluster1/bk1
bin/bookkeeper shell listbookies -rw -h

输入

后果阐明以后 cluster1 已启动了 bookie1(3181)、bookie2(3183)和 bookie3(3184)。

...
15:47:41.370 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - ReadWrite Bookies :
15:47:41.382 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3183, IP:192.168.0.105, Port:3183, Hostname:192.168.0.105
15:47:41.383 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3184, IP:192.168.0.105, Port:3184, Hostname:192.168.0.105
15:47:41.384 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3181, IP:192.168.0.105, Port:3181, Hostname:192.168.0.105
…
  1. 缩小 2 个 bookie 节点,即进行 bookie2 和 bookie3。

提醒:更多对于如何缩小 bookie 的信息,参阅这里。

cd cluster1/bk2
bin/bookkeeper shell listunderreplicated
bin/pulsar-daemon stop bookie
bin/bookkeeper shell decommissionbookie
cd cluster1/bk3
bin/bookkeeper shell listunderreplicated
bin/pulsar-daemon stop bookie
bin/bookkeeper shell decommissionbookie

5. 查看 cluster1 的 bookie 信息。

cd cluster1/bk1
bin/bookkeeper shell listbookies -rw -hcd cluster1/bk1bin/bookkeeper shell listbookies -rw -h

输入

后果阐明以后 cluster1 仅启动了 bookie1(3181)。

…
16:05:28.690 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - ReadWrite Bookies :
16:05:28.700 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3181, IP:192.168.0.105, Port:3181, Hostname:192.168.0.105
...
  1. 设置 public/default 的音讯保留策略。

留神:如果不设置音讯保留策略且没有订阅,一段时间后,数据会被主动清理。

cd cluster1/broker1
bin/pulsar-admin namespaces set-retention -s 100M -t 3d public/default
  1. 在 public/default 创立 topic2,并写入 100 条数据。
bin/pulsar-client produce -m 'hello' -n 100 topic2

输入

结果显示数据写入胜利。

…
16:06:59.448 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 100 messages successfully produced
  1. 查看 topic2 的信息。
bin/pulsar-admin topics stats-internal topic2

输入

结果显示 ledgerId 7 保留了 topic 2 的数据。

{
  "entriesAddedCounter" : 100,
  "numberOfEntries" : 100,
  "totalSize" : 5400,
  "currentLedgerEntries" : 100,
  "currentLedgerSize" : 5400,
  "lastLedgerCreatedTimestamp" : "2021-05-11T16:06:59.058+08:00",
  "waitingCursorsCount" : 0,
  "pendingAddEntriesCount" : 0,
  "lastConfirmedEntry" : "7:99",
  "state" : "LedgerOpened",
  "ledgers" : [ {
    "ledgerId" : 7,
    "entries" : 0,
    "size" : 0,
    "offloaded" : false
  } ],
  "cursors" : { },
  "compactedLedger" : {
    "ledgerId" : -1,
    "entries" : -1,
    "size" : -1,
    "offloaded" : false
  }
}
  1. 查看 ledgerid 7 存储在哪些 bookie 节点上。
bin/bookkeeper shell ledgermetadata -ledgerid 7

输入

结果显示 ledgerid 7 存储在 bookie1(3181)上。

...
16:11:28.843 [main] INFO  org.apache.bookkeeper.tools.cli.commands.client.LedgerMetaDataCommand - ledgerID: 7
16:11:28.846 [main] INFO  org.apache.bookkeeper.tools.cli.commands.client.LedgerMetaDataCommand - LedgerMetadata{formatVersion=3, ensembleSize=1, writeQuorumSize=1, ackQuorumSize=1, state=OPEN, digestType=CRC32C, password=base64:, ensembles={0=[192.168.0.105:3181]}, customMetadata={component=base64:bWFuYWdlZC1sZWRnZXI=, pulsar/managed-ledger=base64:cHVibGljL2RlZmF1bHQvcGVyc2lzdGVudC90b3BpYzM=, application=base64:cHVsc2Fy}}
…

总结

本文是「Pulsar 隔离策略系列」的第 2 篇,解说了如何在「多个 Pulsar 集群」的环境中验证数据隔离、同步集群数据和扩缩容节点。

本系列的下一篇博客将详细分析如何在「共享 BookKeeper 集群」的环境中玩转 Pulsar 隔离策略,同样也会是「Pulsar 傻瓜手册」喔!敬请期待!

更多信息

如果你对「Pulsar 隔离策略系列」感兴趣,欢送查阅更多相干资源:

  • 根底篇:Pulsar 隔离策略 – 官网文档
  • 高阶篇:深度解析如何在 Pulsar 中实现隔离

点击链接,获取 Apache Pulsar 硬核干货材料!

正文完
 0