Routing Type: Public Network Domain Name Access > Access Method: SASL_PLAINTEXT.

pip install kafka-python
producer.py.producer = KafkaProducer(bootstrap_servers = ['xx.xx.xx.xx:port'],api_version = (1, 1),## SASL_PLAINTEXT access in the public network.#security_protocol = "SASL_PLAINTEXT",sasl_mechanism = "PLAIN",sasl_plain_username = "instanceId#username",sasl_plain_password = "password",)message = "Hello World! Hello Ckafka!"msg = json.dumps(message, ensure_ascii=False).encode()producer.send('topic_name', value = msg)print("produce message " + message + " success.")producer.close()
Parameter | Description |
bootstrap_servers | Access network. On the Basic Info page of the instance in the console, select the Access Mode module and copy the network information from the Network column. |
sasl_plain_username | Username, in the format of instance ID + # + username. The instance ID can be obtained from the basic information on the instance details page in the CKafka console. Choose ACL Policy Management > User Management to create a user and set the username. |
sasl_plain_password | User password. On the instance details page in the CKafka console, choose ACL Policy Management > User Management to create a user and set the password. |
topic_name | Topic name. Copy the name on the Topic List page in the console. |

consumer = KafkaConsumer('topic_name',group_id = "group_id",bootstrap_servers = ['xx.xx.xx.xx:port'],api_version = (1,1),## SASL_PLAINTEXT access in the public network.#security_protocol = "SASL_PLAINTEXT",sasl_mechanism = 'PLAIN',sasl_plain_username = "instanceId#username",sasl_plain_password = "password",)for message in consumer:print ("Topic:[%s] Partition:[%d] Offset:[%d] Value:[%s]" %(message.topic, message.partition, message.offset, message.value))
Parameter | Description |
bootstrap_servers | Access network. On the Basic Info page of the instance in the console, select the Access Mode module and copy the network information from the Network column. |
group_id | Consumer group ID. Define the group ID according to business requirements. |
sasl_plain_username | Username, in the format of instance ID + # + username. The instance ID can be obtained from the basic information on the instance details page in the CKafka console. Choose ACL Policy Management > User Management to create a user and set the username. |
sasl_plain_password | User password. On the instance details page in the CKafka console, choose ACL Policy Management > User Management to create a user and set the password. |
topic_name | Topic name. Copy the name on the Topic List page in the console. |

Feedback