This topic provides sample code for sending and receiving messages using the ApsaraMQ for RocketMQ 5.x software development kit (SDK) for Python.
Message type | Sample code for sending messages | Sample code for subscribing to messages | |
| None | ||
None | |||
Parameters
Parameter | Example value | Description |
endpoints | rmq-cn-xxx.{regionId}.rmq.aliyuncs.com:8080 | The endpoint of the ApsaraMQ for RocketMQ instance. For information about how to obtain the endpoint, see Get the instance endpoint.
|
InstanceId | rmq-cn-xxx | The ID of the ApsaraMQ for RocketMQ instance. |
topic | normal_test | The topic to which messages are sent or from which messages are consumed in the ApsaraMQ for RocketMQ instance. You must create the topic on the ApsaraMQ for RocketMQ instance in advance. For more information, see Create a topic. |
consumer-group | GID_test | The consumer group that is used by consumers to consume messages in the ApsaraMQ for RocketMQ instance. You must create the consumer group on the ApsaraMQ for RocketMQ instance in advance. For more information, see Create a consumer group. |
ak | 1XVg0hzgKm****** | The username of the ApsaraMQ for RocketMQ instance. If you access the instance over the Internet, you must specify the username. If you access the instance in a VPC, you need to specify the username only if the instance is a serverless instance and the authentication-free in VPCs feature is disabled for the instance. For information about how to obtain the username, see Get the instance username and password. |
sk | ijSt8rEc45****** | The password of the ApsaraMQ for RocketMQ instance. If you access the instance over the Internet, you must specify the password. If you access the instance in a VPC, you need to specify the password only if the instance is a serverless instance and the authentication-free in VPCs feature is disabled for the instance. For information about how to obtain the password, see Get the instance username and password. |
Public network access for serverless instances
To access a serverless ApsaraMQ for RocketMQ instance over the public network, add the following content to your messaging code:
Replace InstanceId with the ID of your ApsaraMQ for RocketMQ instance.
config = ClientConfiguration(endpoints, credentials, "InstanceId")