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()
相关推荐
aiotto资源分享1 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
利威尔xu2 小时前
Go Mutex 饥饿模式:正常模式等待者饿死,1ms 阈值自动切换崩溃
开发语言·后端·面试·golang·并发编程
梯度下降者2 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.3 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
乌暮3 小时前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis
不绝1913 小时前
Lua基础语法入门:注释、变量类型与字符串操作
开发语言·lua
the局外人4 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__4 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
Hhy_11074 小时前
《C++深度解构07》手撕 vector —— 从底层三指针架构到迭代器失效彻底吃透
c语言·开发语言·c++·学习·visual studio
风落青芜4 小时前
链式二叉树的实现与基本操作
c语言·开发语言·数据结构·算法