如何实现TDMQ中的Pulsar 广播

66次阅读
没有评论

共计 5964 个字符,预计需要花费 15 分钟才能阅读完成。

如何实现 TDMQ 中的 Pulsar 广播,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面丸趣 TV 小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Pulsar 作为 Apache 社区的相对新的成员,在业界受到非常大量的关注。新产品的文档相对不齐全也是非常能够理解的。今天客户问过来广播怎么实现的,我解释了半天,又找了很多介绍产品的 PPT,最终也没有找到“官方”的文档说明这个事情。于是我就写了这篇文章,方便大家  copy/paste。

Pulsar 订阅模型分类

Pulsar 支持的几种模式如下,依次是   独占模式  /  高可用模式  /  分享模式  /  基于键值   的分享模式。

如何实现 TDMQ 中的 Pulsar 广播
 

 

Pulsar 广播模式

Pulsar 的订阅模式和很多 MQ 不太一样。比如 RabbitMQ/Kafka 等,一般消费端(Consumer)是直接去对接 Topic 的,然后 Consumer 自己又有个组的概念在配置中心去设置 offset,以此来决定是一起分享 Topic 的数据,还是每个人都接收同样的数据。在 Pulsar 的消费订阅模型里,添加了一个 Subscription 的逻辑,Subscription 的 Type 决定了消费是独享还是分享。

于是广播模式可以用不同 Subscription 独享的模式来实现,具体架构可以参照下图:

如何实现 TDMQ 中的 Pulsar 广播
 

 

代码实现

1. Full-mesh 的形创建 Java 项目(比如:Springboot – 这个应该是相对简单的 IDE 集成开发组件)

画重点

pulsar-client-api 和  tdmq-client 需要 2.6.0tdmq-client 需要在腾讯的 repo 里才能拿到,需要使用介绍链接介绍的方式进行 maven 的配置(gradle 方法类似)

介绍链接: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 个一起接收一样的消息

如何实现 TDMQ 中的 Pulsar 广播   

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注丸趣 TV 行业资讯频道,感谢您对丸趣 TV 的支持。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-25发表,共计5964字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)