导语 对于本地开发和测试,可以在计算机上以独立模式运行Pulsar。本文记录了一次在Linux服务器本地搭建pulsar集群standalone模式的全过程,遇到的问题和解决方案,以及消息收发的测试实例,以供需要者使用。
一、系统要求
1、MacOS/Linux
2、Java 8
二、使用二进制版本安装Pulsar
$ wget https://archive.apache.org/dist/pulsar/pulsar-2.4.0/apache-pulsar-2.4.0-bin.tar.gz
$ tar xvfz apache-pulsar-2.4.0-bin.tar.gz
$ cd apache-pulsar-2.4.0`
三、安装内置连接器
$ wget https://archive.apache.org/dist/pulsar/pulsar-2.4.0/connectors/{connector}-2.4.0.nar
该方法为pulsar官方文档,但执行该语句出现报错:无该文件。
以下是我的解决方法:
$ wget https://archive.apache.org/dist/pulsar/pulsar-2.4.0/connectors/pulsar-io-aerospike-2.4.0.nar
$ mkdir connectors
$ mv pulsar-io-aerospike-2.4.0.nar connectors
$ ls connectors
pulsar-io-aerospike-2.4.0.nar
四、安装分层存储卸载程序
$ wget https://archive.apache.org/dist/pulsar/pulsar-2.4.0/apache-pulsar-offloaders-2.4.0-bin.tar.gz
$ tar xvfz apache-pulsar-offloaders-2.4.0-bin.tar.gz
// you will find a directory named `apache-pulsar-offloaders-2.4.0` in the pulsar directory
// then copy the offloaders
$ mv apache-pulsar-offloaders-2.4.0/offloaders offloaders
$ ls offloaders
tiered-storage-jcloud-2.4.0.nar
五、配置/data/pulsar/conf/standalone.conf
# Zookeeper quorum connection string
zookeeperServers=你的IP:指定一个端口
注:pulsar官方文档上没有这一步,但当没有执行这一步时,第六步会报错。
六、启动pulsar standalone
$ bin/pulsar standalone
注:一定要在bin下启动。
如果日志信息太多,可通过jps -l检查服务是否启动。
七、使用pulsar standalone
7.1 consume a message
$ bin/pulsar-client consume my-topic -s "first-subscription"
成功信息:
18:24:47.138 [main] INFO org.apache.pulsar.client.cli.PulsarClientTool - 1 messages successfully consumed
7.2 produce a message
$ bin/pulsar-client produce my-topic --messages "hello-pulsar"
成功信息:
18:24:12.977 [main] INFO org.apache.pulsar.client.cli.PulsarClientTool - 1 messages successfully produced
八、消息收发测试
8.1 创建一个python生产者(可选择自己熟悉的语言)
主题为my-topic2,并向这个主题发送10条消息。
import pulsar
client = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer('my-topic2')
for i in range(10):
producer.send(('Hello-%d' % i).encode('utf-8'))
client.close()
8.2 创建一个消费者
使用my-subscription订阅主题my-topic2,监听传入的消息,打印内容和到达信息的ID,并且向pulsar broker确认每条消息。
import pulsar
client = pulsar.Client('pulsar://localhost:6650')
consumer = client.subscribe('my-topic2','my-subscription')
while True:
msg = consumer.receive()
try:
print("Received message '{}' id='{}'".format(msg.data(),msg.message_id()))
consumer.acknowledge(msg)
except:
consumer.negative_acknowledge(msg)
client.close()
测试成功: