librocketmq first.'64bit','ELF' (x86_64 architecture) may cause errors on macOS systems with ARM chips.librocketmq 2.0.0 or later as instructed in Install librocketmq.rocketmq-client-python.pip install rocketmq-client-python
from rocketmq.client import Producer, Message# Initialize the producer and set the producer group information. Be sure to use the full name of the group, such as `rocketmq-xxx|namespace_python%group1`.producer = Producer(groupName)# Set the service addressproducer.set_name_server_address(nameserver)# Set permissions (role name and token)producer.set_session_credentials(accessKey, # Role tokensecretKey, # Role name'')# Start the producerproducer.start()# Assemble messages. The topic name can be copied on the **Topic** page in the console.msg = Message(topicName)# Set keysmsg.set_keys(TAGS)# Set tagsmsg.set_tags(KEYS)# Message contentmsg.set_body('This is a new message.')# Send messages in sync moderet = producer.send_sync(msg)print(ret.status, ret.msg_id, ret.offset)# Release resourcesproducer.shutdown()
Parameter | Description |
groupName | Copy the producer group name from the Group Management page on the console. 4.x virtual cluster/dedicated cluster: Concatenate the namespace name here, format namespace full name%group name, such as MQ_INSTxxx_aaa%GroupTest.4.x generic cluster/5.x cluster: No need to concatenate here, just fill in the Group name. ![]() |
nameserver | Get the cluster access address from the access information module on the console cluster basic information page. ![]() |
secretKey | Role name, copied from the SecretKey column on the Role Management page in the console. |
accessKey | Role token, copied from the AccessKey column on the Role Management page in the console. ![]() |
namespace | Copy the namespace name from the namespace page in the console. If you use a 4.x generic cluster or 5.x cluster, fill in the Cluster ID here. ![]() |
topicName | Topic name, which can be copied on the Topic management page in the console. ![]() |
TAGS | A parameter used to set the message tag. |
KEYS | A parameter used to set the message key. |
import timefrom rocketmq.client import PushConsumer, ConsumeStatus# Message processing callbackdef callback(msg):# Simulate the business processing logicprint('Received message. messageId: ', msg.id, ' body: ', msg.body)# Return CONSUME_SUCCESS if the consumption is successfulreturn ConsumeStatus.CONSUME_SUCCESS# Return the consumption status if the consumption is successful# return ConsumeStatus.RECONSUME_LATER# Initialize the consumer and set the consumer group informationconsumer = PushConsumer(groupName)# Set the service addressconsumer.set_name_server_address(nameserver)# Set permissions (role name and token)consumer.set_session_credentials(accessKey, # Role tokensecretKey, # Role name'')# Subscribe to a topicconsumer.subscribe(topicName, callback, TAGS)print(' [Consumer] Waiting for messages.')# Start the consumerconsumer.start()while True:time.sleep(3600)# Release resourcesconsumer.shutdown()
Parameter | Description |
namespace | Copy the namespace name from the namespace page in the console. If you use a 4.x generic cluster or 5.x cluster, fill in the Cluster ID here. |
groupName | Copy the name of consumer group from the Group Management page on the console. ![]() |
nameserver | Get the cluster access address from the access information module on the console cluster basic information page. ![]() |
secretKey | Role name, copied from the SecretKey column on the Role Management page in the console. |
accessKey | Role token, copied from the AccessKey column on the Role Management page in the console. ![]() |
topicName | Copy the Topic name from the Topic management page in the console. ![]() |

masukan