導讀css
Pulsar 做爲 Apache 社區的相對新的成員,在業界受到很是大量的關注。新產品的文檔相對不齊全也是很是可以理解的。今天客戶問過來廣播怎麼實現的,我解釋了半天,又找了不少介紹產品的 PPT,最終也沒有找到「官方」的文檔說明這個事情。因而我就寫了這篇文章,方便你們 copy/paste 。java
做者介紹
web
徐爲spring
騰訊雲微服務團隊高級解決方案構架師apache
畢業於歐盟 Erasmus Mundus IMMIT,得到經濟和IT管理碩士學位api
自2006年以來,曾就任於SonyEricsson、SAP等多家公司,歷任軟件開發工程師,數據開發工程師,解決方案架構師微信
Pulsar訂閱模型分類架構
Pulsar 原文支持的幾種模式以下,依次是 獨佔模式 / 高可用模式 / 分享模式 / 基於鍵值 的分享模式。app
若是這幾個模式尚未理解的,能夠去官網先看一下,我我的以爲看過應該是能夠理解的:maven
https://pulsar.apache.org/docs/en/concepts-messaging/#subscriptions
Pulsar 廣播模式
Pulsar 的訂閱模式和不少 MQ 不太同樣。好比 RabbitMQ/Kafka 等,通常消費端(Consumer)是直接去對接 Topic 的,而後 Consumer 本身又有個組的概念在配置中心去設置 offset,以此來決定是一塊兒分享 Topic 的數據,仍是每一個人都接收一樣的數據。在 Pulsar 的消費訂閱模型裏,添加了一個 Subscription 的邏輯,Subscription 的 Type 決定了消費是獨享仍是分享。
因而廣播模式能夠用不一樣 Subscription 獨享的模式來實現,具體架構能夠參照下圖:
代碼實現
1. Full-mesh 的形建立 Java 項目(好比:Springboot - 這個應該是相對簡單的 IDE 集成開發組件)
畫重點
-
pulsar-client-api 和 tdmq-client 須要2.6.0 -
tdmq-client 須要在騰訊的repo裏才能拿到,須要使用介紹連接介紹的方式進行maven的配置(gradle方法相似) 介紹連接:https://cloud.tencent.com/document/product/1179/44914
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.examble.demo</groupId> <artifactId>tdmq-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>tdmq-demo</name> <description>demo project to test tdmq</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.tencent.tdmq</groupId> <artifactId>tdmq-client</artifactId> <version>2.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.pulsar/pulsar-client-api --> <dependency> <groupId>org.apache.pulsar</groupId> <artifactId>pulsar-client-api</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
2. 建立一個 Component 用來全局使用 Producer 和 Consumers
這裏建立了1個 Producer 和3個擁有 exclusive subscription 的 consumers(廣播模式 - 咱們期待他們3個每次都收到同樣的信息)
package com.example.demo.tdmq.instance;
import javax.annotation.PostConstruct;
import org.apache.pulsar.client.api.AuthenticationFactory;import org.apache.pulsar.client.api.Consumer;import org.apache.pulsar.client.api.Message;import org.apache.pulsar.client.api.MessageListener;import org.apache.pulsar.client.api.Producer;import org.apache.pulsar.client.api.PulsarClient;import org.apache.pulsar.client.api.PulsarClientException;import org.apache.pulsar.client.api.SubscriptionType;import org.springframework.beans.factory.config.ConfigurableBeanFactory;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;
(ConfigurableBeanFactory.SCOPE_SINGLETON)public class Global { PulsarClient client; public Producer<byte[]> producer; public Consumer<byte[]> consumer01; public Consumer<byte[]> consumer02; public Consumer<byte[]> consumer03;
public Global() {
}
public void init() { try { client = PulsarClient.builder().serviceUrl("pulsar://<Your TDMQ Pulsar Service URL>:6000/") .listenerName("custom:<TDMQ Pulsar Instance ID>/<TDMQ VPC ID>/<TDMQ Subnet ID>") .authentication(AuthenticationFactory.token( "<Your Credential Token from TDMQ>")) .build(); producer = client.newProducer().topic("persistent://<TDMQ Pulsar Instance ID>/<your name space>/<your topic>").create(); consumer01 = client.newConsumer().subscriptionType(SubscriptionType.Exclusive) .topic("persistent://<TDMQ Pulsar Instance ID>/<your name space>/<your topic>") .messageListener(new MessageListener<byte[]>() {
/** * */ private static final long serialVersionUID = 1L;
public void received(Consumer<byte[]> consumer, Message<byte[]> msg) { System.out.println("Consumer01" + " - " + System.currentTimeMillis() + " - " + new String(msg.getData())); try { consumer.acknowledge(msg); } catch (PulsarClientException e) { // TODO Auto-generated catch block e.printStackTrace(); }
} }).subscriptionName("my-subscription01").subscribe(); consumer02 = client.newConsumer().subscriptionType(SubscriptionType.Exclusive) .topic("persistent://<TDMQ Pulsar Instance ID>/<your name space>/<your topic>") .messageListener(new MessageListener<byte[]>() {
/** * */ private static final long serialVersionUID = 1L;
public void received(Consumer<byte[]> consumer, Message<byte[]> msg) { System.out.println("Consumer02" + " - " + System.currentTimeMillis() + " - " + new String(msg.getData())); try { consumer.acknowledge(msg); } catch (PulsarClientException e) { // TODO Auto-generated catch block e.printStackTrace(); }
} }).subscriptionName("my-subscription02").subscribe(); consumer03 = client.newConsumer().subscriptionType(SubscriptionType.Exclusive) .topic("persistent://<TDMQ Pulsar Instance ID>/<your name space>/<your topic>") .messageListener(new MessageListener<byte[]>() {
/** * */ private static final long serialVersionUID = 1L;
public void received(Consumer<byte[]> consumer, Message<byte[]> msg) { System.out.println("Consumer03" + " - " + System.currentTimeMillis() + " - " + new String(msg.getData())); try { consumer.acknowledge(msg); } catch (PulsarClientException e) { // TODO Auto-generated catch block e.printStackTrace(); }
} }).subscriptionName("my-subscription03").subscribe();
} catch (PulsarClientException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
}
3. 最外層的測試代碼和簡單的 Message 模型
public class MessageModel {
private String messageText = null;
public String getMessageText() { return messageText; }
public void setMessageText(String messageText) { this.messageText = messageText; }}
跑起來測試一下,果真3個一塊兒接收同樣的消息
話很少說,趕忙跑起來玩玩吧!
有相關需求的讀者歡迎留言告訴咱們你的想法!
往期
推薦
掃描下方二維碼關注本公衆號,
瞭解更多微服務、消息隊列的相關信息!
解鎖超多鵝廠周邊!
本文分享自微信公衆號 - 騰訊雲中間件(gh_6ea1bc2dd5fd)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。