74.python3 rocketmq

安装rocketmq包

./pip3 install rocketmq -i https://pypi.tuna.tsinghua.edu.cn/simple

生产者

[bin]$ cat p.py

# -*-coding:utf-8-*-

from rocketmq.client import Producer, Message

producer = Producer("producer_id")

#producer.set_namesrv_addr("10.188.14.196:9876")

producer.set_namesrv_addr("10.188.12.228:9876")

producer.set_group("producer_group")

print("start")

producer.start()

def success(sendres):

    print("success")

    print(sendres)

def error(error_obj):

    print("error")

    print(error_obj.error)

msg = Message("dt_orst_cmc")

msg.set_body("66666")

#print("send")

# 异步发送

#producer.send_async(msg, success, error)

# 同步发送

ret = producer.send_sync(msg)

print(ret)


消费者

[bin]$ cat c.py

# -*-coding:utf-8-*-

import time

from rocketmq.client import PushConsumer

def handle_message(msg):

    print(msg.id)

    print(msg.body)

consumer = PushConsumer("push_consumer_id")

consumer.set_namesrv_addr("10.188.12.228:9876")

consumer.set_group("push_consumer_group")

consumer.subscribe("dt_orst_cmc", handle_message)

print("start")

consumer.start()

while True:

    print("wait 5s")

    time.sleep(5)

#consumer.shutdown()

#print("end.")

你可能感兴趣的:(74.python3 rocketmq)