點擊關注公眾號,回復“1024”獲取2TB學習資源!
最近這個 Apache Pulsar 訊息中間件非常的火,號稱下一代訊息中件,今天,就一起來看看它到底有多牛逼?
概述
Apache Pulsar 是一個使用 Apache Bookkeeper 提供持久化的 pub/sub 訊息平臺,是一個用于服務端到服務端的訊息中間件,最初由Yahoo開發并在2016年開源,目前正在Apache基金會下范訓,它可以提供如下特性:
跨地域復制
多租戶
零資料丟失
零Rebalancing時間
統一的佇列和流模型
高可擴展性
高吞吐量
Pulsar Proxy
函式
架構
Pulsar 使用分層結構,將存盤機制與 broker 隔離開來,此體系結構為 Pulsar 提供以下好處:
獨立擴展broker
獨立擴展存盤(Bookies)
更容易容器化Zookeeper, Broker and Bookies
ZooKeeper提供集群的配置和狀態存盤
在 Pulsar 集群中,一個或多個代理處理和負載均衡來自生產者的傳入訊息,將訊息分派給消費者,與 Pulsar 配置存盤通信以處理各種協調任務,將訊息存盤在 BookKeeper 實體(又名 bookies)中,依賴特定于集群的 ZooKeeper 集群任務等等,
由一個或多個 bookie 組成的 BookKeeper 集群處理訊息的持久存盤,
特定于該集群的 ZooKeeper 集群處理 Pulsar 集群之間的協調任務,
更多關于 Pulsar 的架構介紹請參閱:https://pulsar.apache.org/docs/en/concepts-architecture-overview/
四種訂閱模式
Pulsar 中有四種訂閱模式:exclusive、shared、failover和key_shared,這些模式如下圖所示,
詳細介紹參閱:https://pulsar.apache.org/docs/en/concepts-messaging/
性能優于 Kafka
Pulsar 表現最出色的就是性能,Pulsar 的速度比 Kafka 快得多,與 Kafka 相比,Pulsar 的速度提升了 2.5 倍,延遲降低了 40%,
資料來源:https://streaml.io/pdf/Gigaom-Benchmarking-Streaming-Platforms.pdf
注:對比是針對 1 個磁區的 1 個主題,其中包含 100 位元組訊息,Pulsar 每秒可發送 220,000+ 條訊息,
安裝
二進制版本安裝 Pulsar
#下載官方二進制包
[root@centos7 ~]# wget https://archive.apache.org/dist/pulsar/pulsar-2.8.0/apache-pulsar-2.8.0-bin.tar.gz
#解壓
[root@centos7 ~]# tar zxf apache-pulsar-2.8.0-bin.tar.gz
[root@centos7 ~]# cd apache-pulsar-2.8.0
[root@centos7 apache-pulsar-2.8.0]# ll
total 72
drwxr-xr-x 3 root root 225 Jan 22 2020 bin
drwxr-xr-x 5 root root 4096 Jan 22 2020 conf
drwxr-xr-x 3 root root 132 Jul 6 11:47 examples
drwxr-xr-x 4 root root 66 Jul 6 11:47 instances
drwxr-xr-x 3 root root 16384 Jul 6 11:47 lib
-rw-r--r-- 1 root root 31639 Jan 22 2020 LICENSE
drwxr-xr-x 2 root root 4096 Jan 22 2020 licenses
-rw-r--r-- 1 root root 6612 Jan 22 2020 NOTICE
-rw-r--r-- 1 root root 1269 Jan 22 2020 README
#bin目錄下就有直接啟動的命令
Docker安裝(重點介紹)
[root@centos7 ~]# docker run -it \
-p 6650:6650 \
-p 8080:8080 \
--mount source=pulsardata,target=/pulsar/data \
--mount source=pulsarconf,target=/pulsar/conf \
apachepulsar/pulsar:2.8.0 \
bin/pulsar standalone
http協議訪問使用8080埠,pulsar協議(Java、Python等客戶端)訪問使用6650埠,
官方提供的可視化工具 Pulsar Manager,可以對多個Pulsar進行可視化管理,https://pulsar.apache.org/docs/en/administration-pulsar-manager/
[root@centos7 ~]# docker pull apachepulsar/pulsar-manager:v0.2.0
[root@centos7 ~]# docker run -it \
-p 9527:9527 -p 7750:7750 \
-e SPRING_CONFIGURATION_FILE=/pulsar-manager/pulsar-manager/application.properties \
apachepulsar/pulsar-manager:v0.2.0
設定管理員用戶與密碼
[root@centos7 ~]# CSRF_TOKEN=$(curl http://localhost:7750/pulsar-manager/csrf-token)
curl \
-H 'X-XSRF-TOKEN: $CSRF_TOKEN' \
-H 'Cookie: XSRF-TOKEN=$CSRF_TOKEN;' \
-H "Content-Type: application/json" \
-X PUT http://localhost:7750/pulsar-manager/users/superuser \
-d '{"name": "admin", "password": "admin123", "description": "test", "email": "mingongge@test.org"}'
{"message":"Add super user success, please login"}
瀏覽器直接輸入 http://server_ip:9527 登錄如下
輸入剛剛創建的用戶與密碼,配置管理的服務端
串列
Toptic串列
Toptic詳細資訊
客戶端配置
Java客戶端
下面是一個使用共享訂閱的 Java 消費者配置示例:
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.SubscriptionType;
String SERVICE_URL = "pulsar://localhost:6650";
String TOPIC = "persistent://public/default/mq-topic-1";
String subscription = "sub-1";
PulsarClient client = PulsarClient.builder()
.serviceUrl(SERVICE_URL)
.build();
Consumer consumer = client.newConsumer()
.topic(TOPIC)
.subscriptionName(subscription)
.subscriptionType(SubscriptionType.Shared)
// If you'd like to restrict the receiver queue size
.receiverQueueSize(10)
.subscribe();
Python客戶端
下面是一個使用共享訂閱的 Python 消費者配置示例:
from pulsar import Client, ConsumerType
SERVICE_URL = "pulsar://localhost:6650"
TOPIC = "persistent://public/default/mq-topic-1"
SUBSCRIPTION = "sub-1"
client = Client(SERVICE_URL)
consumer = client.subscribe(
TOPIC,
SUBSCRIPTION,
# If you'd like to restrict the receiver queue size
receiver_queue_size=10,
consumer_type=ConsumerType.Shared)
C++ 客戶端
下面是一個使用共享訂閱的 C++ 消費者配置示例:
#include <pulsar/Client.h>
std::string serviceUrl = "pulsar://localhost:6650";
std::string topic = "persistent://public/defaultmq-topic-1";
std::string subscription = "sub-1";
Client client(serviceUrl);
ConsumerConfiguration consumerConfig;
consumerConfig.setConsumerType(ConsumerType.ConsumerShared);
// If you'd like to restrict the receiver queue size
consumerConfig.setReceiverQueueSize(10);
Consumer consumer;
Result result = client.subscribe(topic, subscription, consumerConfig, consumer);
更多配置及操作指南,官方的檔案寫的都很清楚,官方檔案:https://pulsar.apache.org/docs/
總結
Plusar 作為下一代分布式訊息佇列,擁有非常多吸引人的特性,也彌補了一些其他競品的短板,例如地域復制、多租戶、擴展性、讀寫隔離等等,
我的新書:《 Linux系統運維指南 》已出版
推薦閱讀 點擊標題可跳轉
摸魚神器!在命令列中玩斗地主
停服、資料永久洗掉!經營 3 年的產品黃了
徹底搞懂 Nginx 五大應用場景!出去吹牛逼再也不擔心了
干掉 Swagger-ui !試試這個新工具
牛X!這款高顏值 Markdown 神器真強大~
Java 必會的工具庫,讓你的代碼量減少90%
Java 全堆疊知識體系( PDF 可下載)

隨手在看、轉發是最大的支持!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290825.html
標籤:其他
上一篇:2021年保研夏令營經驗貼
