关于mq:python3调用rocket-mq

45次阅读

共计 1006 个字符,预计需要花费 3 分钟才能阅读完成。

install

https://github.com/apache/roc…

    wget https://github.com/apache/rocketmq-client-cpp/releases/download/2.0.0/rocketmq-client-cpp-2.0.0-centos7.x86_64.rpm
    sudo rpm -ivh rocketmq-client-cpp-2.0.0-centos7.x86_64.rpm

Installation

pip install rocketmq-client-python

Producer

from rocketmq.client import Producer, Message

producer = Producer('test-litx')
producer.set_name_server_address('rxxrocketmq-namesrv.sit.dexxxxm:9876')
producer.start()

msg = Message('itworkspace')
msg.set_keys('status')
msg.set_tags('json')
msg.set_body('{"alertname":"test666666666","building":"t11111tttt"}')
ret = producer.send_sync(msg)
print(ret.status, ret.msg_id, ret.offset)
producer.shutdown()

PushConsumer

import time
from rocketmq.client import PushConsumer, ConsumeStatus

def callback(msg):
    print(msg.id, msg.body)
    return ConsumeStatus.CONSUME_SUCCESS

consumer = PushConsumer('itworkxxxce-consumer')
consumer.set_name_server_address('red-rocketmq-namesrv.sit.devops.xixxxu.com:9876')
consumer.subscribe('itwxxce', callback)
consumer.start()

time.sleep(3)

consumer.shutdown()

正文完
 0