python rocketmq-client-python 包的使用

  1. ubuntu安装
    这个包目前仅支持linux以及mac
bash 复制代码
wget https://github.com/apache/rocketmq-client-cpp/releases/download/2.0.0/rocketmq-client-cpp-2.0.0.amd64.deb
sudo dpkg -i rocketmq-client-cpp-2.0.0.amd64.deb
sudo pip3 install rocketmq-client-python
  1. 生产者
python 复制代码
from rocketmq.client import Producer, Message

groupName = 'ellis'
topicName = "ellis"
TAGS='ellis'
KEYS='ellis'
nameserver="192.168.214.134:9876"
# 初始化生产者,并设置生产组信息,组名称使用全称,例:rocketmq-xxx|namespace_python%group1
producer = Producer(groupName)
# 设置服务地址
producer.set_name_server_address(nameserver)
# # 设置权限(角色名和密钥)
# producer.set_session_credentials(
#     accessKey,  # 角色密钥
#     secretKey,  # 角色名称
#     ''
# )
# 启动生产者
producer.start()


# 组装消息   topic名称,在控制台 topic 页面复制。   
msg = Message(topicName)
# 设置keys
msg.set_keys(TAGS)
# 设置tags
msg.set_tags(KEYS)
# 消息内容
msg.set_body('This is a new message.')


# 发送同步消息
ret = producer.send_sync(msg)

print(ret.status, ret.msg_id, ret.offset)
# 资源释放
producer.shutdown()
  1. 根据tag订阅消费
python 复制代码
import time

from rocketmq.client import PushConsumer, ConsumeStatus


groupName = 'ellis7'
topicName = "ellis"
TAGS='ellis'
nameserver="192.168.214.134:9876"
# 消息处理回调
def callback(msg):
    # 模拟业务
    print('Received message. messageId: ', msg.id, ' body: ', msg.body)
    # 消费成功回复CONSUME_SUCCESS
    return ConsumeStatus.CONSUME_SUCCESS
    # 消费成功回复消息状态
    # return ConsumeStatus.RECONSUME_LATER




# 初始化消费者,并设置消费者组信息 
consumer = PushConsumer(groupName)
consumer.set_group(groupName)
# 设置服务地址
consumer.set_name_server_address(nameserver)
# 设置权限(角色名和密钥)
# consumer.set_session_credentials(
#     accessKey,	 # 角色密钥
#     secretKey,   # 角色名称
#     ''
# )
# 订阅topic
consumer.set_instance_name(groupName)
consumer.subscribe(topicName, callback,TAGS)
# 启动消费者
consumer.start()


while True:
    time.sleep(3600)
# 资源释放
consumer.shutdown()
相关推荐
yanyanwenmeng几秒前
matlab基础
开发语言·算法·matlab
拉玛干6 分钟前
社团周报系统可行性研究-web后端框架对比-springboot,django,gin
数据库·python·spring·golang
末央&16 分钟前
【C++】内存管理
java·开发语言·c++
不是仙人的闲人20 分钟前
Qt日志输出及QsLog日志库
开发语言·数据库·qt
八了个戒26 分钟前
【TypeScript入坑】TypeScript 的复杂类型「Interface 接口、class类、Enum枚举、Generics泛型、类型断言」
开发语言·前端·javascript·面试·typescript
梦想科研社34 分钟前
【无人机设计与控制】四旋翼无人机轨迹跟踪及避障Matlab代码
开发语言·matlab·无人机
Yan-英杰35 分钟前
Encountered error while trying to install package.> lxml
开发语言·python·pandas·pip·issue
RS&38 分钟前
python学习笔记
笔记·python·学习
AI原吾39 分钟前
解锁自动化新境界:KeymouseGo,让键盘和鼠标动起来!
运维·python·自动化·计算机外设·keymousego
卡卡_R-Python39 分钟前
海洋气象编程工具-Python
开发语言·python