总共3台服务器,在每台服务器上都安装Zookeeper、Pulsar broker、Bookeeper bookie
其中客户端使用包含了所有broker地址的URL与Pulsar集群通信。所有brokers和 bookies都依赖ZooKeeper
安装Java8:请参考centos7同时安装java8和openJdk11、windows同时安装java8和openJdk11
安装Zookeeper:请参考基于Centos7分布式安装Zookeeper3.6.3
[root@pulsar1 ~]# wget --no-check-certificate https://dlcdn.apache.org/pulsar/pulsar-2.9.1/apache-pulsar-2.9.1-bin.tar.gz
[root@pulsar1 ~]#
[root@pulsar1 ~]# tar -zxvf apache-pulsar-2.9.1-bin.tar.gz
[root@pulsar1 ~]#
[root@pulsar1 ~]# cd apache-pulsar-2.9.1
[root@pulsar1 apache-pulsar-2.9.1]#
[root@pulsar1 apache-pulsar-2.9.1]# ll instances/
总用量 10832
drwxr-xr-x. 2 root root 6 1月 22 2020 deps
-rw-r--r--. 1 root root 11083846 1月 22 2020 java-instance.jar
drwxr-xr-x. 4 root root 4096 1月 22 2020 python-instance
[root@pulsar1 apache-pulsar-2.9.1]#
其中instances目录下,都是为Pulsar Functions创建的组件
PULSAR_MEM=${PULSAR_MEM:-"-Xms512m -Xmx512m -XX:MaxDirectMemorySize=1g"}
zkServers=pulsar1:2181,pulsar2:2181,pulsar3:2181
zookeeperServers=pulsar1:2181,pulsar2:2181,pulsar3:2181
configurationStoreServers=pulsar1:2181,pulsar2:2181,pulsar3:2181
webServicePort=8086
clusterName=pulsar-cluster
# 不删除不活跃的topic
brokerDeleteInactiveTopicsEnabled=false
webServiceUrl=http://pulsar1:8086,pulsar2:8086,pulsar3:8086
brokerServiceUrl=pulsar://pulsar1:6650,pulsar2:6650,pulsar3:6650
暂不安装
暂不安装
暂不启用
将apache-pulsar-2.9.1目录分发到pulsar2和pulsar3服务器上
[root@pulsar1 ~]# scp -r apache-pulsar-2.9.1 root@pulsar2:/root
[root@pulsar1 ~]# scp -r apache-pulsar-2.9.1 root@pulsar3:/root
可以在任何一台pulsar服务器上进行初始化,这里我们在pulsar1上进行初始化
[root@pulsar1 apache-pulsar-2.9.1]#
[root@pulsar1 apache-pulsar-2.9.1]# bin/pulsar initialize-cluster-metadata \
> --cluster pulsar-cluster \
> --zookeeper pulsar1:2181 \
> --configuration-store pulsar1:2181 \
> --web-service-url http://pulsar1:8086,pulsar2:8086,pulsar3:8086 \
> --broker-service-url pulsar://pulsar1:6650,pulsar2:6650,pulsar3:6650
[root@pulsar1 apache-pulsar-2.9.1]#
参数说明如下:
通过客户端连接Zookeeper,可以看到已经初始化成功了
[zk: pulsar1:2181,pulsar2:2181,pulsar3:2181(CONNECTED) 0] ls /
[admin, bookies, ledgers, pulsar, stream, zookeeper]
[zk: pulsar1:2181,pulsar2:2181,pulsar3:2181(CONNECTED) 1]
produce和consume topic,最终会在zookeeper下创建如下目录
[admin, bookies, counters, ledgers, loadbalance, managed-ledgers, namespace, pulsar, schemas, stream]
[root@pulsar1 apache-pulsar-2.9.1]#
[root@pulsar1 apache-pulsar-2.9.1]# bin/pulsar-daemon start bookie
如果要停止Bookeeper,运行bin/pulsar-daemon stop bookie
即可
使用下列命令可以看到,当前服务器的Bookeeper已经启动成功。检测原理是在本地的bookie创建一个临时的BookKeeper ledger,往里面写一些entries,然后读取它,最后删除这个ledger
[root@pulsar1 apache-pulsar-2.9.1]#
[root@pulsar1 apache-pulsar-2.9.1]# bin/bookkeeper shell bookiesanity
2022-03-14T11:27:04,757+0800 [main] INFO org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand - Bookie sanity test succeeded
2022-03-14T11:27:04,757+0800 [main-EventThread] INFO org.apache.zookeeper.ClientCnxn - EventThread shut down for session: 0x10007a2d9530005
[root@pulsar1 apache-pulsar-2.9.1]#
也可以使用以下方法,在一台服务器上,验证所有的Bookeeper是否启动成功。其中数字3表示集群中Bookeeper的数量。检测原理是在集群上创建和bookie同等数量的ledger,并往里面写一些entries,然后读取它,最后删除这个ledger
[root@pulsar1 apache-pulsar-2.9.1]# bin/bookkeeper shell simpletest --ensemble 3 --writeQuorum 3 --ackQuorum 3 --numEntries 3
......省略部分......
2022-03-14T11:41:28,410+0800 [main] INFO org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand - 3 entries written to ledger 3
......省略部分......
[root@pulsar1 apache-pulsar-2.9.1]#
[root@pulsar1 apache-pulsar-2.9.1]# bin/pulsar-daemon start broker
如果要停止Broker,运行bin/pulsar-daemon stop broker
即可
public/default namespace会自动被创建。所有Pulsar的topic都在namespace中进行管理
等所有broker启动完毕,在一台服务器上检验所有broker是否启动成功
[root@pulsar1 apache-pulsar-2.9.1]# bin/pulsar-admin brokers list pulsar-cluster
"pulsar1:8086"
"pulsar2:8086"
"pulsar3:8086"
先在命令行启动consume进行消息的消费
[root@pulsar1 apache-pulsar-2.9.1]#
[root@pulsar1 apache-pulsar-2.9.1]# bin/pulsar-client consume \
> persistent://public/default/test \
> -n 100 \
> -s "consumer-test" \
> -t "Exclusive"
......省略部分......
2022-03-14T13:58:52,117+0800 [pulsar-client-io-1-1] INFO org.apache.pulsar.client.impl.ConsumerImpl - [persistent://public/default/test][consumer-test] Subscribed to topic on pulsar2/192.168.23.112:6650 -- consumer: 0
参数说明如下:
pulsar会自动在public/default下创建topic
然后在另一个窗口,启动produce进行消息的产生。
[root@pulsar1 apache-pulsar-2.9.1]# bin/pulsar-client produce \
> persistent://public/default/test \
> -n 1 \
> -m "Hello Pulsar"
......省略部分......
2022-03-14T14:06:38,192+0800 [main] INFO org.apache.pulsar.client.cli.PulsarClientTool - 1 messages successfully produced
[root@pulsar1 apache-pulsar-2.9.1]#
参数说明如下:
再次查看consume窗口,可以看到已经获取到了消息
2022-03-14T14:06:36,112+0800 [pulsar-client-io-1-1] INFO com.scurrilous.circe.checksum.Crc32cIntChecksum - SSE4.2 CRC32C provider initialized
----- got message -----
key:[null], properties:[], content:Hello Pulsar
2022-03-14T14:06:49,805+0800 [pulsar-timer-5-1] INFO org.apache.pulsar.client.impl.ConsumerStatsRecorderImpl - [persistent://public/default/test] [consumer-test] [86c21] Prefetched messages: 0 --- Consume throughput received: 0.02 msgs/s --- 0.00 Mbit/s --- Ack sent rate: 0.02 ack/s --- Failed messages: 0 --- batch messages: 0 ---Failed acks: 0
有的时候pulsar的consume不能消费topic的数据
解决办法:
producer.send(null)