pulsar集群standalone模式搭建指引

导语 对于本地开发和测试,可以在计算机上以独立模式运行Pulsar。本文记录了一次在Linux服务器本地搭建pulsar集群standalone模式的全过程,遇到的问题和解决方案,以及消息收发的测试实例,以供需要者使用。

一、系统要求

1、MacOS/Linux
2、Java 8

二、使用二进制版本安装Pulsar

$ wget https://archive.apache.org/dist/pulsar/pulsar-2.4.0/apache-pulsar-2.4.0-bin.tar.gz
$ tar xvfz apache-pulsar-2.4.0-bin.tar.gz
$ cd apache-pulsar-2.4.0`

三、安装内置连接器

$ wget https://archive.apache.org/dist/pulsar/pulsar-2.4.0/connectors/{connector}-2.4.0.nar

该方法为pulsar官方文档,但执行该语句出现报错:无该文件。
以下是我的解决方法:

$ wget https://archive.apache.org/dist/pulsar/pulsar-2.4.0/connectors/pulsar-io-aerospike-2.4.0.nar
$ mkdir connectors
$ mv pulsar-io-aerospike-2.4.0.nar connectors

$ ls connectors
pulsar-io-aerospike-2.4.0.nar

四、安装分层存储卸载程序

$ wget https://archive.apache.org/dist/pulsar/pulsar-2.4.0/apache-pulsar-offloaders-2.4.0-bin.tar.gz
$ tar xvfz apache-pulsar-offloaders-2.4.0-bin.tar.gz

// you will find a directory named `apache-pulsar-offloaders-2.4.0` in the pulsar directory
// then copy the offloaders

$ mv apache-pulsar-offloaders-2.4.0/offloaders offloaders

$ ls offloaders
tiered-storage-jcloud-2.4.0.nar

五、配置/data/pulsar/conf/standalone.conf

# Zookeeper quorum connection string
zookeeperServers=你的IP:指定一个端口

注:pulsar官方文档上没有这一步,但当没有执行这一步时,第六步会报错。

六、启动pulsar standalone

$ bin/pulsar standalone

注:一定要在bin下启动。
如果日志信息太多,可通过jps -l检查服务是否启动。

七、使用pulsar standalone

7.1 consume a message

$ bin/pulsar-client consume my-topic -s "first-subscription"

成功信息:

18:24:47.138 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 1 messages successfully consumed

7.2 produce a message

$ bin/pulsar-client produce my-topic --messages "hello-pulsar"

成功信息:

18:24:12.977 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 1 messages successfully produced

八、消息收发测试

8.1 创建一个python生产者(可选择自己熟悉的语言)
主题为my-topic2,并向这个主题发送10条消息。

import pulsar
client = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer('my-topic2')
for i in range(10):
	producer.send(('Hello-%d' % i).encode('utf-8'))
client.close()

8.2 创建一个消费者
使用my-subscription订阅主题my-topic2,监听传入的消息,打印内容和到达信息的ID,并且向pulsar broker确认每条消息。

import pulsar
client = pulsar.Client('pulsar://localhost:6650')
consumer = client.subscribe('my-topic2','my-subscription')
while True:
	msg = consumer.receive()
	try:
	  print("Received message '{}' id='{}'".format(msg.data(),msg.message_id()))
	  consumer.acknowledge(msg)
	except:
	  consumer.negative_acknowledge(msg)
client.close()

测试成功:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 环境准备 - Pulsar集群 - Java 8 - Spring Boot 2.x - Pulsar Java Client 2. 添加依赖 在`pom.xml`中添加以下依赖: ```xml <dependency> <groupId>org.apache.pulsar</groupId> <artifactId>pulsar-client</artifactId> <version>${pulsar.version}</version> </dependency> <dependency> <groupId>org.apache.pulsar</groupId> <artifactId>pulsar-spring-boot-starter</artifactId> <version>${pulsar.version}</version> </dependency> ``` 其中`${pulsar.version}`为Pulsar的版本号。 3. 配置Pulsar连接信息 在`application.yml`中添加以下配置: ```yaml pulsar: serviceUrl: pulsar://localhost:6650 authPluginClassName: org.apache.pulsar.client.impl.auth.AuthenticationToken authParams: token:eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c ``` 其中`serviceUrl`为Pulsar集群连接地址,`authPluginClassName`和`authParams`为认证信息,可以根据实际情况进行修改。 4. 发送消息 在Spring Boot中可以使用`PulsarTemplate`来发送消息,示例代码如下: ```java import org.apache.pulsar.client.api.MessageId; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.concurrent.ListenableFutureCallback; import org.springframework.util.concurrent.ListenableFutureTask; import org.springframework.util.concurrent.ListenableFutureTaskAdapter; @Service public class PulsarProducerService { @Autowired private PulsarTemplate<String> pulsarTemplate; public void sendMessage(String topic, String message) { ListenableFutureTask<MessageId> future = new ListenableFutureTaskAdapter<>(() -> pulsarTemplate.send(topic, message)); future.addCallback(new ListenableFutureCallback<MessageId>() { @Override public void onFailure(Throwable ex) { System.out.println("Send message failed: " + ex.getMessage()); } @Override public void onSuccess(MessageId result) { System.out.println("Send message success: " + result); } }); future.run(); } } ``` 在上述代码中,`PulsarTemplate`使用泛型`<String>`,表示发送的消息为字符串类型。`sendMessage`方法接收两个参数,分别为消息的主题和内容。发送消息的过程中,使用`ListenableFuture`来处理异步回调。在回调函数中,可以根据发送结果进行相应的处理。 5. 接收消息 在Spring Boot中可以使用`PulsarConsumerFactory`来创建消费者,示例代码如下: ```java import org.apache.pulsar.client.api.Consumer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PulsarConsumerService { @Autowired private PulsarConsumerFactory pulsarConsumerFactory; public void receiveMessage(String topic, ConsumerMessageHandler<String> handler) throws Exception { Consumer<String> consumer = pulsarConsumerFactory.createConsumer(topic, "my-subscription", String.class); consumer.subscribe(); while (true) { String message = consumer.receive().getValue(); handler.handle(message); consumer.acknowledgeAsync(consumer.getLastMessageId()); } } } ``` 在上述代码中,`PulsarConsumerFactory`使用泛型`<String>`,表示接收消息的类型为字符串。`receiveMessage`方法接收两个参数,分别为消息的主题和消息处理器。在接收消息的过程中,使用`while`循环不断接收消息,并交给消息处理器进行处理。处理完成后,使用`acknowledgeAsync`方法对消息进行确认。 6. 运行测试 在完成上述步骤后,可以编写测试代码来测试消息的发送和接收。示例代码如下: ```java import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class PulsarApplicationTests { @Autowired private PulsarProducerService pulsarProducerService; @Autowired private PulsarConsumerService pulsarConsumerService; @Test void sendMessage() { pulsarProducerService.sendMessage("my-topic", "Hello, Pulsar!"); } @Test void receiveMessage() throws Exception { pulsarConsumerService.receiveMessage("my-topic", message -> { System.out.println("Received message: " + message); }); } } ``` 在运行测试代码后,可以在控制台中看到消息发送和接收的相关信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值