1.java连接pulsar服务
2024-10-18 02:55:46
是什么
Pulsar 是一个用于服务器到服务器的消息系统,具有多租户、高性能等优势。详见
安装
本文主练习怎么用,本地搭建了一个单机版,无非就是wget、tar、start这些命令,详见
Java客户端
1.引入GAV
<!-- https://mvnrepository.com/artifact/org.apache.pulsar/pulsar-client -->
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client</artifactId>
<version>2.8.0</version>
</dependency>
2.创建配置项
用于连接Pulsar等配置
Yml配置
pulsar:
url: 10.20.30.228:6650
# url: 10.20.30.228:6650,10.20.30.228:6651 #集群配置
新增配置类
package com.project.pulsar.conf; import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class PulsarConf {
@Value("${pulsar.url}")
String url; @Bean
public PulsarClient pulsarFactory(){
PulsarClient client = null;
try {
client = PulsarClient.builder()
.serviceUrl("pulsar://"+url)
.build();
} catch (PulsarClientException e) {
}
return client;
}
}
3.验证测试
通过简单生产-消费测试配置是否正常
创建BaseController
注意,subscriptionName要保证唯一
package com.project.pulsar.base; import com.project.pulsar.conf.PulsarConf;
import org.apache.pulsar.client.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.HashMap;
import java.util.Map; @RestController
public class BaseController {
@Autowired
PulsarConf pulsarConf; /**
* 生产消息
* @param msg
* @throws PulsarClientException
*/
@GetMapping("/base/sendMsg")
public MessageId sendMsg(String msg) throws PulsarClientException {
PulsarClient pulsarFactory = pulsarConf.pulsarFactory(); Producer<byte[]> producer1 = pulsarFactory.newProducer()
.topic("my-topic") .create();
// 然后你就可以发送消息到指定的broker 和topic上:
return producer1.send(msg.getBytes());
} /**
* 手动执行获取消息
* @throws PulsarClientException
*/
@GetMapping("/base/comsumer")
public void comsumerByArtificial() throws PulsarClientException {
PulsarClient pulsarFactory = pulsarConf.pulsarFactory();
Consumer<byte[]> consumer = pulsarFactory.newConsumer()
.topic("my-topic")
.subscriptionName("my-subscription")
.subscribe();
Message<byte[]> receive = consumer.receive();
System.out.println(new String(receive.getData()));
consumer.acknowledge(receive);//确认消息被消费
consumer.close();
} /**
* 自动监听消费消息
* @throws PulsarClientException
*/
@Bean
public void comsumerByListener() throws PulsarClientException {
MessageListener myMessageListener = (consumer, msg) -> {
try {
System.out.println("Message received: " + new String(msg.getData()));
consumer.acknowledge(msg);
} catch (Exception e) {
consumer.negativeAcknowledge(msg);
}
};
PulsarClient pulsarFactory = pulsarConf.pulsarFactory();
pulsarFactory.newConsumer()
.topic("my-topic")
.subscriptionName("my-subscriptionByListener")
.messageListener(myMessageListener)
.subscribe();
} }生产消息
[127.0.0.1:9999/base/sendMsg?msg=Hello RB](http://127.0.0.1:9999/base/sendMsg?msg=Hello RB)
消费消息
- 在生产后,如果采用监听模式,会自动消费
- 在生产后,如果采用手动模式,执行127.0.0.1:9999/base/comsumer会被消费,如队列中无消费,则会阻塞等待
其他及代码下载
- topic不用显式创建,当消息发送或消费者建立连接时,如未创建会自动创建
- 代码见此Base包下
最新文章
- javascript中通过匿名函数进行事件绑定
- Hibernate(八)__级联操作、struts+hibernate+接口编程架构
- Oracle 常用操作【02】数据库特性
- MVC5-10 ModleBinder那点事
- Hadoop中几个基本命令行命令
- angularJS ng-grid 配置
- SpringMVC序列化Long转成String
- mysql 的数据类型
- storm的安装配置
- SystemParametersInfo
- git config --global core.excludesfile配置gitignore全局文件
- linux文件属性
- MVC验证06-自定义错误信息
- CSS 中的内联元素、块级元素、display的各个属性的特点
- 【原创】Linux基础之sudo
- css实现div左侧突出一个带边框的三角形
- mint linux 18.3 遇到“已安装的 post-installation 脚本 返回了错误号 127 ”问题的解决
- slecte下拉框的多选操作及获取值的 变化
- Python基础-TypeError:takes 2 positional arguments but 3 were given
- python基础易错题