python调用rocketmq的api_Python:Rocketmq消息队列使用

rocketmq可以与kafka等一起使用,用于实时消息处理。

安装rocketmq:

pip install rocketmq [-i https://pypi.tuna.tsinghua.edu.cn/simple]

生产消息producer:

from rocketmq.client importProducer, Messageimportjson

producer= Producer('PID-test')

producer.set_namesrv_addr('xxx.xxx.xxx.xxx:xxxxx') #rocketmq队列接口地址(服务器ip:port)

producer.start()

msg_body= {"id":"001","name":"test_mq","message":"abcdefg"}

ss= json.dumps(msg_body).encode('utf-8')

msg= Message('topic_name') #topic名称

msg.set_keys('xxxxxx')

msg.set_tags('xxxxxx')

msg.set_body(ss)#message body

retmq=producer.send_sync(msg)print(retmq.status, retmq.msg_id, retmq.offset)

producer.shutdown()

其中:

设置ip:port的位置:producer.set_namesrv_addr('xxx.xxx.xxx.xxx:xxxxx')

你可能感兴趣的:(python调用rocketmq的api_Python:Rocketmq消息队列使用)