#作者:閆乾苓
文章目錄
- 1、二進制安裝部署Pulsar(standalone)
- 1.1 安裝配置JDK
- 1.2 下載解壓pulsar安裝包
- 1.3 啟動獨立模式的Pulsar 集群
- 1.4 創建主題測試
- 1.5 向主題寫入消息測試
- 1.6 從主題中讀取消息測試
- 2.docker安裝部署Pulsar(standalone)
- 2.1 使用docker 啟動Pulsar
- 2.2 使用客戶端API測試集群
1、二進制安裝部署Pulsar(standalone)
1.1 安裝配置JDK
根據官方文檔要求,需要安裝合適的 64 位 JRE/JDK 版本。請參考Pulsar Runtime Java 版本建議
組件 | Java 版本 |
---|---|
Broker | 17 |
Functions / IO | 17 |
CLI | 17 |
Java Client | 8 or 11 or 17 |
Openjdk17下載地址:
https://adoptium.net/zh-CN/temurin/releases/?variant=openjdk17
# 解壓下載的安裝包
[root@pulsar ~]# tar xf OpenJDK17U-jdk_x64_linux_hotspot_17.0.14_7.tar.gz -C /opt# 配置軟連接
[root@pulsar ~]# ln -s /opt/jdk-17.0.14+7/ /opt/openjdk-17# 配置PATH環境
[root@pulsar ~]# cat >> /etc/profile << EOF
export JAVA_HOME=/opt/openjdk-17
export PATH=\$JAVA_HOME/bin:\$PATH
EOF# 使PATH環境生效
[root@pulsar ~]# source /etc/profile# 驗證,能輸出正確版本為配置成功
[root@pulsar ~]# java -version
openjdk version "17.0.14" 2025-01-21
OpenJDK Runtime Environment Temurin-17.0.14+7 (build 17.0.14+7)
OpenJDK 64-Bit Server VM Temurin-17.0.14+7 (build 17.0.14+7, mixed mode, sharing)
1.2 下載解壓pulsar安裝包
# 下載安裝包
[root@pulsar ~]# wget https://archive.apache.org/dist/pulsar/pulsar-2.11.4/apache-pulsar-2.11.4-bin.tar.gz
# 解壓到/opt
[root@pulsar ~]# tar xf apache-pulsar-2.11.4-bin.tar.gz -C /opt/# 設置軟連接
[root@pulsar opt]# ln -s /opt/apache-pulsar-2.11.4/ /opt/pulsar
1.3 啟動獨立模式的Pulsar 集群
[root@pulsar ~]# cd /opt/pulsar/
[root@pulsar ~]# bin/pulsar standalone
Pulsar 集群啟動時,會創建以下目錄:
目錄 | 描述 |
---|---|
data | BookKeeper 和 RocksDB 創建的所有數據 |
Log | 所有服務器端日志 |
- 要將服務作為后臺進程運行,可以使用該bin/pulsar-daemon start standalone命令。
- 命名空間public/default是在啟動 Pulsar 集群時創建的。此命名空間用于開發目的。所有 Pulsar 主題均在命名空間內進行管理。
1.4 創建主題測試
Pulsar 將消息存儲在主題中。即使 Pulsar 可以在引用主題時自動創建主題,在使用主題之前明確創建主題也是一種很好的做法。
要創建新主題,請運行以下命令:
[root@pulsar pulsar]# bin/pulsar-admin topics create persistent://public/default/my-topic
1.5 向主題寫入消息測試
[root@pulsar pulsar]# bin/pulsar-client produce my-topic --messages 'Hello Pulsar!'
…
2025-04-14T11:30:37,582+0800 [main] INFO org.apache.pulsar.client.cli.PulsarClientTool - 1 messages successfully produced
1.6 從主題中讀取消息測試
[root@pulsar pulsar]# bin/pulsar-client consume my-topic -s 'my-subscription' -p Earliest -n 0
Earliest 表示從最早的未消費消息開始消費。-n配置消費的消息數量,0 表示永遠消費。
一個shell終端寫入,另外一個終端讀取:
----- got message -----
key:[null], properties:[], content:Hello Pulsar!-0
----- got message -----
key:[null], properties:[], content:Hello Pulsar!-1
----- got message -----
key:[null], properties:[], content:Hello Pulsar!-2
1.7 批量寫入消息
[root@pulsar pulsar]# bin/pulsar-client produce my-topic --messages “$(seq -s, -f ‘Message NO.%g’ 1 10)”
讀取的終端輸出如下:
----- got message -----
key:[null], properties:[], content:Message NO.1
…
----- got message -----
key:[null], properties:[], content:Message NO.10
1.8 停止Pulsar
使用bin/pulsar standalone啟動時,使用Ctrl-C停止。
使用bin/pulsar-daemon start standalone啟動時,使用一些命令停止集群:
[root@pulsar pulsar]# bin/pulsar-daemon stop standalone
2.docker安裝部署Pulsar(standalone)
2.1 使用docker 啟動Pulsar
運行以下命令在 Docker 容器內啟動 Pulsar
docker run -d \-p 6650:6650 -p 8080:8080 \--mount source=pulsardata,target=/pulsar/data \--mount source=pulsarconf,target=/pulsar/conf \--user=0 \apachepulsar/pulsar:2.11.4 bin/pulsar standalone
啟動后會創建2個volume
[root@pulsar pulsar]# docker volume ls
DRIVER VOLUME NAME
local pulsarconf
local pulsardata
執行健康檢查:
[root@pulsar pulsarconf]# docker exec -it d7a633a7b7d8 bash
root@d7a633a7b7d8:/pulsar# pwd
/pulsar
root@d7a633a7b7d8:/pulsar# bin/pulsar-admin brokers healthcheck
ok
2.2 使用客戶端API測試集群
Pulsar 提供多種客戶端庫,例如Java、Go、Python、C++。
下面的示例使用Python 客戶端 API開始使用 Pulsar。
直接從PyPI安裝 Pulsar Python 客戶端庫
pip install pulsar-client
創建consumer.py,創建消費者并訂閱主題:
import pulsarclient = pulsar.Client('pulsar://localhost:6650')
consumer = client.subscribe('my-topic', subscription_name='my-sub')while True:msg = consumer.receive()print("Received message: '%s'" % msg.data())consumer.acknowledge(msg)client.close()
創建producer.py啟動生產者來發送一些測試消息:
import pulsarclient = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer('my-topic')for i in range(10):producer.send(('hello-pulsar-%d' % i).encode('utf-8'))client.close()
執行2個py腳本,consumer.py輸出如下:
[root@pulsar docker_install]# python consumer.py
2025-04-14 17:26:52.515 INFO [140167888657472] Client:86 | Subscribing on Topic :my-topic
…
Received message: 'b'hello-pulsar-0''
Received message: 'b'hello-pulsar-1''
Received message: 'b'hello-pulsar-2''
Received message: 'b'hello-pulsar-3''
Received message: 'b'hello-pulsar-4''
Received message: 'b'hello-pulsar-5''
Received message: 'b'hello-pulsar-6''
Received message: 'b'hello-pulsar-7''
Received message: 'b'hello-pulsar-8''
Received message: 'b'hello-pulsar-9''
獲取topic的統計數據:
curl http://localhost:8080/admin/v2/persistent/public/default/my-topic/stats | python -m json.tool