您好,登錄后才能下訂單哦!
如何實現TDMQ中的Pulsar 廣播,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
Pulsar訂閱模型分類
Pulsar 支持的幾種模式如下,依次是 獨占模式 / 高可用模式 / 分享模式 / 基于鍵值 的分享模式。
Pulsar 廣播模式
Pulsar 的訂閱模式和很多 MQ 不太一樣。比如 RabbitMQ/Kafka 等,一般消費端(Consumer)是直接去對接 Topic 的,然后 Consumer 自己又有個組的概念在配置中心去設置 offset,以此來決定是一起分享 Topic 的數據,還是每個人都接收同樣的數據。在 Pulsar 的消費訂閱模型里,添加了一個 Subscription 的邏輯,Subscription 的 Type 決定了消費是獨享還是分享。
于是廣播模式可以用不同 Subscription 獨享的模式來實現,具體架構可以參照下圖:
代碼實現
1. Full-mesh 的形創建 Java 項目(比如:Springboot - 這個應該是相對簡單的 IDE 集成開發組件)
畫重點
介紹鏈接:https://cloud.tencent.com/document/product/1179/44914
<?xml version="1.0" encoding="UTF-8"?>
<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;
@Component
@Scope(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() {
}
@PostConstruct
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;
@Override
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;
@Override
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;
@Override
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個一起接收一樣的消息
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。