Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Pulsar broadcast in TDMQ

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to achieve Pulsar broadcast in TDMQ, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

As a relatively new member of the Apache community, Pulsar has received a lot of attention in the industry. The relatively incomplete documentation of the new product is also very understandable. Today, the customer asked how the broadcast was realized. I explained for a long time and found a lot of PPT to introduce the product. In the end, I couldn't find the "official" document to explain this matter. So I wrote this article for the convenience of copy/paste.

Pulsar subscription model classification

Several modes supported by Pulsar are as follows: exclusive mode / highly available mode / sharing mode / key-based sharing mode.

Pulsar broadcast mode

Pulsar's subscription model is quite different from that of many MQ. For example, for RabbitMQ/Kafka, the general consumer end (Consumer) directly connects with Topic, and then Consumer has a group concept to set offset in the configuration center to decide whether to share Topic data together or everyone receives the same data. In Pulsar's consumption subscription model, a Subscription logic is added, and Subscription's Type determines whether consumption is exclusive or shared.

Therefore, the broadcast mode can be implemented with different Subscription exclusive modes. The specific architecture can be found in the following figure:

Code implementation

1. Full-mesh shape creation Java project (for example: Springboot-this should be a relatively simple IDE integrated development component)

Key points of painting

Pulsar-client-api and tdmq-client need to be obtained from 2.6.0tdmq-client in Tencent's repo, and maven needs to be configured using the introduction link (gradle method is similar)

Introduction link: https://cloud.tencent.com/document/product/1179/44914

4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.3 com.examble.demo tdmq-demo 0.0.1-SNAPSHOT tdmq-demo demo project to test tdmq 1.8 org.springframework.boot spring-boot-starter-web com.tencent.tdmq tdmq-client 2.6.0 org.apache.pulsar pulsar-client- Api 2.6.0 org.springframework.boot spring-boot-starter-test test

Org.springframework.boot spring-boot-maven-plugin

two。 Create a Component to use Producer and Consumers globally

Here we create one Producer and three consumers with exclusive subscription (broadcast mode-we expect all three of them to receive the same message every time)

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 producer; public Consumer consumer01; public Consumer consumer02; public Consumer consumer03

Public Global () {

}

@ PostConstruct public void init () {try {client = PulsarClient.builder () .serviceUrl ("pulsar://:6000/") .listenerName ("custom://") .authentication (AuthenticationFactory.token (")) .build (); producer = client.newProducer () .topic (" persistent://// ") .create () Consumer01 = client.newConsumer () .subscriptionType (SubscriptionType.Exclusive) .topic ("persistent:////") .messageListener (new MessageListener () {

/ * / private static final long serialVersionUID = 1L

@ Override public void received (Consumer consumer, Message 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:////") .messageListener (new MessageListener ()) {

/ * / private static final long serialVersionUID = 1L

@ Override public void received (Consumer consumer, Message 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:////") .messageListener (new MessageListener ()) {

/ * / private static final long serialVersionUID = 1L

@ Override public void received (Consumer consumer, Message 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. Outermost test code and simple Message model

Public class MessageModel {

Private String messageText = null

Public String getMessageText () {return messageText;}

Public void setMessageText (String messageText) {this.messageText = messageText;}}

Run for a test, and sure enough, the three receive the same message together.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report