apache-pulsar集群搭建,以及demo

本文详细介绍Apache Pulsar消息队列系统的集群部署过程,包括初始化元数据、部署BookKeeper与Brokers集群,并提供使用IDEA进行消费者与生产者开发的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、介绍

1、apache-pulsar介绍

2、pulsar的优缺点,以及适用场景

3、服务器环境

4、下载安装包

二、集群安装

1、初始化元数据到zookeeper

2、部署bookKeeper集群

3、部署brokers集群

三、IDEA开发demo

1、消费者

2、生产者


一、介绍

1、apache-pulsar介绍

参考:https://blog.youkuaiyun.com/u010869257/article/details/83211152

2、pulsar的优缺点,以及适用场景

3、服务器环境

  • JDK:1.8+
  • zookeeper:hadoop001:2181,hadoop002:2181,hadoop003:2181
  • Linux(centos7+):hadoop001,hadoop002,hadoop003

4、下载安装包

#解压(三台机器)
tar zxvf apache-pulsar-2.4.1-bin.tar.gz

#得到apache-pulsar-2.4.1文件,所有操作都在该文件下操作

二、集群安装

单机版解压即用即可,没有什么配置的东西

1、初始化元数据到zookeeper

在任意一个机器上操作就可以,只执行一次(zookeeper会自动同步的)。如果执行失败可能是端口占用问题,需要修改被占用的 端口(查看所有端口:ps aux)

# 初始化元数据到zk
./bin/pulsar initialize-cluster-metadata \
  --cluster pulsar-cluster \
  --zookeeper hadoop001:2181 \
  --configuration-store hadoop001:2181 \
  --web-service-url http://hadoop001:8080 \
  --web-service-url-tls https://hadoop001:8443 \
  --broker-service-url pulsar://hadoop001:6650 \
  --broker-service-url-tls pulsar+ssl://hadoop001:6651

2、部署bookKeeper集群

1)配置文件conf/bookkeeper.conf

#三台机器
vim conf/bookkeeper.conf
  advertisedAddress=hadoop001   # 配置本机ip
  zkServers=hadoop001:2181,hadoop002:2181,hadoop003:2181

2)启动bookKeeper

#三台机器上都启动 
./bin/pulsar-daemon start bookie

#  检验 :
./bin/bookkeeper shell bookiesanity

3、部署brokers集群

1)配置conf/broker.conf

#三台机器都配置
vim conf/broker.conf
    zookeeperServers=hadoop001:2181,hadoop002:2181,hadoop003:2181
    configurationStoreServers=hadoop001:2181,hadoop002:2181,hadoop003:2181
    advertisedAddress=hadoop001  # 服务器本机ip
    clusterName=pulsar-cluster   #集群名字

2)启动brokers集群(三台机器)

  • 开启:./bin/pulsar-daemon start broker
  • 关闭:./bin/pulsar-daemon stop broker
  • 查看:./bin/pulsar-admin brokers list pulsar-cluster(如果端口不一样修改:conf/client.conf)

三、IDEA开发demo

1、消费者

<dependency>
     <groupId>org.apache.pulsar</groupId>
     <artifactId>pulsar-client</artifactId>
     <version>2.4.1</version>
</dependency>

 
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.SubscriptionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName: MyConsumer
 * @Description: TODO
 * @Author: zikuang
 * @Data: 2019/10/17 14:32
 * @Version: 1.0
 **/
 
public class MyConsumer {
    private static final Logger log = LoggerFactory.getLogger(ProducerDemo.class);
    //hadoop001 ,hadoop002,hadoop003
    private static final String SERVER_URL = "pulsar://hadoop001:6650";

    public static void main(String[] args) throws Exception {
        // 构造Pulsar Client
        PulsarClient client = PulsarClient.builder()
                .serviceUrl(SERVER_URL)
                .enableTcpNoDelay(true)
                .build();
        Consumer consumer = client.newConsumer()
                .consumerName("my-consumer")
                .topic("my-topic")
                .subscriptionName("my-subscription")
                .ackTimeout(10, TimeUnit.SECONDS)
                .maxTotalReceiverQueueSizeAcrossPartitions(10)
                .subscriptionType(SubscriptionType.Exclusive)
                .subscribe();
        while (true) {
            // Wait for a message
            Message msg = consumer.receive();

            try {
                // Do something with the message
                System.out.printf("Message received: %s\n", new String(msg.getData()));

                // Acknowledge the message so that it can be deleted by the message broker
                consumer.acknowledge(msg);
            } catch (Exception e) {
                // Message failed to process, redeliver later
                consumer.negativeAcknowledge(msg);
            }
        }
    }
}

2、生产者

 

import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: ProducerDemo
 * @Description: TODO
 * @Author: zikuang
 * @Data: 2019/10/17 14:29
 * @Version: 1.0
 **/
public class ProducerDemo {
    private static final Logger log = LoggerFactory.getLogger(ProducerDemo.class);
    //hadoop001,hadoop002,hadoop003
    private static final String SERVER_URL = "pulsar://hadoop001:6650";
    public static void main(String[] args) throws Exception {
        // 构造Pulsar Client
        PulsarClient client = PulsarClient.builder()
                .serviceUrl(SERVER_URL)
                .enableTcpNoDelay(true)
                .build();
        // 构造生产者
        Producer<String> producer = client.newProducer(Schema.STRING)
                .producerName("my-producer")
                .topic("my-topic")
                .batchingMaxMessages(1024)
                .batchingMaxPublishDelay(10, TimeUnit.MILLISECONDS)
                .enableBatching(true)
                .blockIfQueueFull(true)
                .maxPendingMessages(512)
                .sendTimeout(10, TimeUnit.SECONDS)
                .blockIfQueueFull(true)
                .create();
        // 同步发送消息
        MessageId messageId = producer.send("Hello World");
        log.info("message id is {}",messageId);
        System.out.println(messageId.toString());
        // 异步发送消息
        CompletableFuture<MessageId> asyncMessageId = producer.sendAsync("This is a async message");
        // 阻塞线程,直到返回结果
        log.info("async message id is {}",asyncMessageId.get());

        
        producer.close();

        // 关闭licent的方式有两种,同步和异步
        // client.close();
        client.closeAsync();

    }
}

 

### Apache Pulsar 介绍 Apache Pulsar 是一种分布式消息传递平台,具有独特的分层架构设计。Pulsar 将计算与存储分离,使得其能够提供更高效的性能以及更好的可扩展性灵活性[^1]。 ### 安装配置指南 #### 准备工作环境 为了部署 Apache Pulsar 集群,需先准备好运行环境。通常情况下,在 Linux 或 macOS 上安装较为方便。对于 Windows 用户来说,则建议使用 Docker 来简化设置过程。 #### 下载并解压二进制包 可以从官方网站获取最新版本的 Pulsar 发行版,并将其下载到本地机器上。接着按照常规方式解开压缩文件夹即可完成初步准备工作[^5]。 ```bash wget https://downloads.apache.org/pulsar/pulsar-2.9.0/apache-pulsar-2.9.0-bin.tar.gz tar xvzf apache-pulsar-2.9.0-bin.tar.gz cd apache-pulsar-2.9.0 ``` #### 启动单节点集群模式 对于测试目的而言,可以采用最简单的单机多进程模拟集群的方式来进行快速体验: ```bash bin/pulsar standalone ``` 这一步骤将会自动创建一个简易版的消息队列服务实例供开发者调试学习之用。 ### 使用教程概览 当成功搭建好开发环境之后,就可以着手编写应用程序来连接至 Pulsar 并发送接收数据流了。具体操作流程如下所示: - 创建生产者对象用于向指定主题发布新消息; - 构建消费者实体订阅感兴趣的主题等待接收来自其他端点的数据项; - 利用管理工具对整个系统的状态进行监控维护; 上述功能均可以通过编程接口实现自动化处理逻辑,同时也支持命令行形式的手工干预选项[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值