- 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
- 生产者
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()
- 根据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()