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()
相关推荐
一根甜苦瓜5 分钟前
Go语言Slice的一道骚题
开发语言·后端·golang
驰羽13 分钟前
[GO]Go语言泛型详解
开发语言·golang·xcode
NPE~13 分钟前
[手写系列]Go手写db — — 第五版(实现数据库操作模块)
开发语言·数据库·后端·golang·教程·手写系列·手写数据库
润 下15 分钟前
C语言——深入解析C语言指针:从基础到实践从入门到精通(二)
c语言·开发语言·经验分享·笔记·学习·程序人生
王中阳Go21 分钟前
Python 的 PyPy 能追上 Go 的性能吗?
后端·python·go
Goboy30 分钟前
控制仙术流程 - 抉择与循环的艺术
后端·python
布伦鸽32 分钟前
C# WPF DataGrid使用Observable<Observable<object>类型作为数据源
开发语言·c#·wpf
麦麦大数据42 分钟前
F024 vue+flask电影知识图谱推荐系统vue+neo4j +python实现
vue.js·python·flask·知识图谱·推荐算法·电影推荐
say_fall1 小时前
精通C语言(4.四种动态内存有关函数)
c语言·开发语言
AI小云1 小时前
【Python与AI基础】Python编程基础:读写CSV文件
人工智能·python