为了本地开发和测试,我们可以建立一个伪集群。伪集群一样包含 Pulsar broker, ZooKeeper, BookKeeper.
生产环境
如果想运行一个完整的生产pulsar安装, 查看http://pulsar.apache.org/docs/en/deploy-bare-metal
手动安装Pulsar伪集群
系统要求:
Pulsar当前可以运行在Macos与linux系统,而且必须安装java8.
安装:
通过以下方法下载二进制包
从Apache官网的镜像下载:
- https://archive.apache.org/dist/pulsar/pulsar-2.2.0/apache-pulsar-2.2.0-bin.tar.gz
从Pulsar官网下载页下载
从Pulsar的github发布页下载:https://github.com/apache/pulsar/releases/tag/v2.2.0
使用:
$ wget https://archive.apache.org/dist/pulsar/pulsar-2.2.0/apache-pulsar-2.2.0-bin.tar.gzCopy复制代码
下载完成后解压到自定目录:
$ tar xvfz apache-pulsar-2.2.0-bin.tar.gz$ cd apache-pulsar-2.2.0复制代码
包内都包含哪些内容:
二进制包最初包含以下目录:
Directory | Contains |
---|---|
bin | Pulsar的命令行工具, 如 和 |
conf | Pulsar的配置文件, 包含配置 , , 等 |
examples | 一个关于的例子 |
lib | Pulsar所依赖的一些jar包 |
licenses | 一些许可文件 |
一旦运行Pulsar,一下文件夹会被创建:
Directory | Contains |
---|---|
data | ZooKeeper and BookKeeper 数据存储目录 |
instances | 需要的目录 |
logs | 安装时创建的一些日志 |
安装内置的连接器(connector):
从2.1.0-incubating开始,connector单独发布。如果想要使用需要单独下载。通过以下方式下载
从Apache镜像下载:
从Pulsar 下载页下载:http://pulsar.apache.org/download
从 Pulsar的github的发布页下载:https://github.com/apache/pulsar/releases/latest
使用 :
$ wget https://archive.apache.org/dist/pulsar/pulsar-2.2.0/apache-pulsar-io-connectors-2.2.0-bin.tar.gz复制代码
一旦下载完成,解压下载的压缩包,并把下载的东西拷贝到 pulsar 文件夹下的connectors下(如果没有此目录,可以直接创建):
/pulsar
/bin
/lib
/conf
/data
/connectors
$ tar xvfz /path/to/apache-pulsar-io-connectors-2.2.0-bin.tar.gz// you will find a directory named `apache-pulsar-io-connectors-2.2.0` in the pulsar directory// then copy the connectors$ cd apache-pulsar-io-connectors-2.2.0/connectors connectors$ ls connectorspulsar-io-aerospike-2.2.0.narpulsar-io-cassandra-2.2.0.narpulsar-io-kafka-2.2.0.narpulsar-io-kinesis-2.2.0.narpulsar-io-rabbitmq-2.2.0.narpulsar-io-twitter-2.2.0.nar...Copy复制代码
启动:
进入我们刚才解压pulsar/bin/ 文件夹下,执行如下的命令
$ bin/pulsar standalone复制代码
如果正常启动会看到类似下面的信息:
2017-06-01 14:46:29,192 - INFO - [main:WebSocketService@95] - Global Zookeeper cache started2017-06-01 14:46:29,192 - INFO - [main:AuthenticationService@61] - Authentication is disabled2017-06-01 14:46:29,192 - INFO - [main:WebSocketService@108] - Pulsar WebSocket Service startedCopy复制代码
使用Docker安装pulsar的伪集群
我们也可以通过docker安装一个pulsar的伪集群
docker run -it -p 80:80 -p 8080:8080 -p 6650:6650 apachepulsar/pulsar-standaloneCopy复制代码
几个端口的作用:
- 80: pulsar的仪表板
- 8080: pulsar通过http对外提供服务的端口
- 6650: pulsar通过二进制协议对完提供的端口
启动完成后,我们通过浏览器就可以访问 .
测试Pulsar的集群
Pulsar提供了一个命令行的 pulsar-client工具,下面的语句使用pulsar-client 往my-topic发送一条消息:
$ bin/pulsar-client produce my-topic --messages "hello-pulsar"复制代码
如果发送成功,我们会看到下面的一条消息
13:09:39.356 [main] INFO org.apache.pulsar.client.cli.PulsarClientTool - 1 messages successfully produced复制代码
不需要显式地创建新主题
也许你注意到了,我们发送消息之前并没有事前创建my-topic。如果我们往一个topic发送消息,如果topic事前并没有创建,Pulsar会自动为我们创建。
使用Pulsar的客户端
集群建立后我们可以通过Pulsar提供的客户端(java, python, c ++, go 等)与 Pulsar交互了
http://localhost:8080
pulsar://localhost:6650
客户端生产者的例子:
String localClusterUrl = "pulsar://localhost:6650";PulsarClient client = PulsarClient.builder().serviceURL(localClusterUrl).build();Producerproducer = client.newProducer().topic("my-topic").create();复制代码
生产者的例子:
import pulsarclient = pulsar.Client('pulsar://localhost:6650')producer = client.create_producer('my-topic')复制代码
生产者例子:
Client client("pulsar://localhost:6650");Producer producer;Result result = client.createProducer("my-topic", producer);if (result != ResultOk) { LOG_ERROR("Error creating producer: " << result); return -1;}复制代码