1.java连接pulsar服务
是什么
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包下
1.java连接pulsar服务的更多相关文章
- Java连接MQTT服务-wss方式
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Java连接MQTT服务-ws方式
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Java连接MQTT服务-tcp方式
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Java 连接 Memcached 服务
原文:http://www.runoob.com/memcached/java-memcached.html mac下安装和配置Memcached:http://www.pchou.info/open ...
- Memcached常用语法与java连接服务
memcached常用语法及java使用方式 Author:SimpleWu Memcached 存储命令 Memcached set 命令用于将 value(数据值) 存储在指定的 key(键) 中 ...
- Java连接池详解
于共享资源,有一个很著名的设计模式:资源池(Resource Pool).该模式正是为了解决资源的频繁分配﹑释放所造成的问题.为解决我们的问题,可以采用数据库连接池技术.数据库连接池的基本思想就是为数 ...
- PHP学习笔记——PHP脚本和JAVA连接mysql数据库
环境 开发包:appserv-win32-2.5.10 服务器:Apache2.2 数据库:phpMyAdmin 语言:php5,java 平台:windows 10 java驱动:mysql-con ...
- java 微信公众服务平台 下发 模板消息
java 微信公众服务平台 下发 模板消息 (一).部分截图 (二).部分代码 (一).部分截图: (二).部分代码: //此处 给用户微信发消息... Map<String,String> ...
- 【JDBC】Java 连接 MySQL 基本过程以及封装数据库工具类
一. 常用的JDBC API 1. DriverManager类 : 数据库管理类,用于管理一组JDBC驱动程序的基本服务.应用程序和数据库之间可以通过此类建立连接.常用的静态方法如下 static ...
随机推荐
- 使用Xamarin开发移动应用示例——数独游戏(六)使用数据库
项目代码可以从Github下载:https://github.com/zhenl/ZL.Shudu .代码随项目进度更新. 现在我们希望为应用增加更多的功能,比如记录每个完成的游戏,可以让用户自己添加 ...
- UCB DS100 讲义《数据科学的原理与技巧》校对活动正式启动 | ApacheCN
贡献指南:https://github.com/apachecn/ds100-textbook-zh/blob/master/CONTRIBUTING.md 整体进度:https://github.c ...
- 微服务架构 | 11.1 整合 Seata AT 模式实现分布式事务
目录 前言 1. Seata 基础知识 1.1 Seata 的 AT 模式 1.2 Seata AT 模式的工作流程 1.3 Seata 服务端的存储模式 1.4 Seata 与 Spring Clo ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile
前言 IDEA(2020)引入Maven进行依赖管理,无法从私服上下载jar包 报如下错误 Failed to execute goal org.apache.maven.plugins:maven- ...
- Idea中新建package包,却变成了Directory
问题描述 今天在IdeaJava工程src下新建了一个名字叫包implements的包,右键在里面新建类时,却发现根本就没有Class这个选项,然后发现implements这个包的图标也和其他包的图标 ...
- 如何完整删除DISK DRILL
前两天装了DISK DRILL 右上角出现一个温度提示的图标 现在把DISK DRILL卸载了 但右上角的温度提示图标仍然存在 请问如何删除? 打开系统偏好设置----用户与群----管理员(点 ...
- 3.Flink实时项目之流程分析及环境搭建
1. 流程分析 前面已经将日志数据(ods_base_log)及业务数据(ods_base_db_m)发送到kafka,作为ods层,接下来要做的就是通过flink消费kafka 的ods数据,进行简 ...
- centos安装MySQL问题
使用sudo yum install mysql-server出现没有可用软件包 mysql-server. 先 执行 wget http://repo.mysql.com/mysql-communi ...
- 基于GDAL库,读取.nc文件(以海洋表温数据为例)C++版
对于做海洋数据处理的同学,会经常遇到nc格式的文件,nc文件的格式全称是NetCDF,具体的详细解释请查询官网[https://www.unidata.ucar.edu/software/netcdf ...
- 【论文考古】知识蒸馏 Distilling the Knowledge in a Neural Network
论文内容 G. Hinton, O. Vinyals, and J. Dean, "Distilling the Knowledge in a Neural Network." 2 ...