librocketmq first.'64bit','ELF' (such as the x86_64 architecture) is used on an ARM-based macOS system, errors occur.pip install rocketmq-client-python
from rocketmq.client import Producer, Message # Initialize the producer and set the producer group information. Use the full group name, for example, rocketmq-xxx|namespace_python%group1. During message production, the client does not need to be re-created. producer = Producer(groupName) # Set the service address. producer.set_name_server_address(nameserver) # Set permissions (role name and token). producer.set_session_credentials( accessKey, # Role token secretKey, # Role name '' ) # Start the producer. producer.start() # Assemble messages. # Concatenate the topic with the namespace. msg = Message(namespace + '%' + topicName) # Set keys. msg.set_keys(TAGS) # Set tags. msg.set_tags(KEYS) # Message content msg.set_body('This is a new message.') # Send synchronous messages. ret = producer.send_sync(msg) print(ret.status, ret.msg_id, ret.offset) # Release resources. producer.shutdown()
Parameter | Description |
groupName | Producer group name. You can copy the name from the Group Management page in the console. 4.x virtual cluster/exclusive cluster: Concatenate the namespace name in the format of full namespace name%group name, such as MQ_INSTxxx_aaa%GroupTest.4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the group name. |
nameserver | Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console. |
secretKey | Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console. |
accessKey | Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console. |
namespace | Namespace name. You can copy the name from the Namespace page in the console. If you are using a 4.x general cluster or a 5.x cluster, specify the cluster ID for this parameter. |
topicName | Topic name. You can copy the name from the Topic Management page in the console. |
TAGS | Tags associated with the message. |
KEYS | Sets the message business key. |
import timefrom rocketmq.client import PushConsumer, ConsumeStatus# Message processing callback.def callback(msg):# Simulate the business logic.print('Received message. messageId: ', msg.id, ' body: ', msg.body)# If consumption is successful, return CONSUME_SUCCESS.return ConsumeStatus.CONSUME_SUCCESS# Return message status after successful consumption.# return ConsumeStatus.RECONSUME_LATER# Initialize the consumer and set consumer group information.consumer = PushConsumer(namespace + '%' + groupName)# Set the service address.consumer.set_name_server_address(nameserver)# Set permissions (role name and token).consumer.set_session_credentials(accessKey, # Role token.secretKey, # Role name.'')# Subscribe to a topic.consumer.subscribe(namespace + '%' + topicName, callback, TAGS)print(' [Consumer] Waiting for messages.')# Start the consumer.consumer.start()while True:time.sleep(3600)# Release resources.consumer.shutdown()
Parameter | Description |
namespace | Namespace name. You can copy the name from the Namespace page in the console. If you are using a 4.x general cluster or a 5.x cluster, specify the cluster ID for this parameter. |
groupName | Producer group name. You can copy the name from the Group Management page in the console. |
nameserver | Cluster access address. You can obtain the access address from the Access Information module on the cluster basic information page in the console. |
secretKey | Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console. |
accessKey | Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console. |
topicName | Topic name. You can copy the name from the Topic Management page in the console. |
フィードバック