Pulsar本地單機(偽)叢集 (裸機安裝與docker方式安裝) 2.2.0
為了本地開發和測試,我們可以建立一個偽叢集。偽叢集一樣包含 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官網下載頁下載ofollow,noindex">pulsar.apache.org/download/
-
從Pulsar的github釋出頁下載:https://github.com/apache/pulsar/releases/tag/v2.2.0
-
使用wget" target="_blank" rel="nofollow,noindex">wget :
$ wget https://archive.apache.org/dist/pulsar/pulsar-2.2.0/apache-pulsar-2.2.0-bin.tar.gz Copy複製程式碼
下載完成後解壓到自定目錄:
$ tar xvfz apache-pulsar-2.2.0-bin.tar.gz $ cd apache-pulsar-2.2.0 複製程式碼
包內都包含哪些內容:
二進位制包最初包含以下目錄:
Directory | Contains |
---|---|
bin
|
Pulsar的命令列工具, 如
pulsar
和
pulsar-admin
|
conf
|
Pulsar的配置檔案, 包含配置broker configuration,ZooKeeper configuration , 等 |
examples
|
一個關於Pulsar Functions的例子 |
lib
|
Pulsar所依賴的一些jar包 |
licenses
|
一些許可檔案 |
一旦執行Pulsar,一下資料夾會被建立:
Directory | Contains |
---|---|
data
|
ZooKeeper and BookKeeper 資料儲存目錄 |
instances
|
Pulsar Functions需要的目錄 |
logs
|
安裝時建立的一些日誌 |
安裝內建的聯結器(connector):
從2.1.0-incubating開始,connector單獨釋出。如果想要使用需要單獨下載。通過以下方式下載
-
從Apache映象下載:
-
從Pulsar 下載頁下載:http://pulsar.apache.org/download
-
從 Pulsar的github的釋出頁下載:https://github.com/apache/pulsar/releases/latest
-
使用wget :
$ 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 connectors pulsar-io-aerospike-2.2.0.nar pulsar-io-cassandra-2.2.0.nar pulsar-io-kafka-2.2.0.nar pulsar-io-kinesis-2.2.0.nar pulsar-io-rabbitmq-2.2.0.nar pulsar-io-twitter-2.2.0.nar ... Copy複製程式碼
啟動:
進入我們剛才解壓pulsar/bin/ 資料夾下,執行如下的命令
$ bin/pulsar standalone 複製程式碼
如果正常啟動會看到類似下面的資訊:
2017-06-01 14:46:29,192 - INFO- [main:Socket/">WebSocketService@95] - Global Zookeeper cache started 2017-06-01 14:46:29,192 - INFO- [main:AuthenticationService@61] - Authentication is disabled 2017-06-01 14:46:29,192 - INFO- [main:WebSocketService@108] - Pulsar WebSocket Service started Copy複製程式碼
使用Docker安裝pulsar的偽叢集
我們也可以通過docker安裝一個pulsar的偽叢集
docker run -it -p 80:80 -p 8080:8080 -p 6650:6650 apachepulsar/pulsar-standalone Copy複製程式碼
幾個埠的作用:
- 80: pulsar的儀表板
- 8080: pulsar通過http對外提供服務的埠
- 6650: pulsar通過二進位制協議對完提供的埠
啟動完成後,我們通過瀏覽器就可以訪問http://localhost .
測試Pulsar的叢集
Pulsar提供了一個命令列的 pulsar-client工具,下面的語句使用pulsar-client 往my-topic傳送一條訊息:
$ bin/pulsar-client produce my-topic --messages "hello-pulsar"複製程式碼
如果傳送成功,我們會看到下面的一條訊息
13:09:39.356 [main] INFOorg.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
Java客戶端生產者的例子:
String localClusterUrl = "pulsar://localhost:6650"; PulsarClient client = PulsarClient.builder().serviceURL(localClusterUrl).build(); Producer<byte[]> producer = client.newProducer().topic("my-topic").create(); 複製程式碼
Python 生產者的例子:
import pulsar client = pulsar.Client('pulsar://localhost:6650') producer = client.create_producer('my-topic') 複製程式碼
C++生產者例子:
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; }複製程式碼