Public Domain Name Access

Last updated: 2021-08-10 17:34:08

    Overview

    This document describes how to access CKafka to send/receive messages with the SDK for Python over the public network.

    Prerequisites

    Directions

    Step 1. Add the Python dependency library

    Run the following command to install:

    pip install kafka-python
    

    Step 2. Produce a message

    1. Modify the configuration parameters in the message production program producer.py.
    #coding:utf8
    from kafka import KafkaProducer
    import json
    producer = KafkaProducer(
    bootstrap_servers = ['$domainName:$port'],
    ssl_check_hostname = False,
    security_protocol = "SASL_PLAINTEXT",
    sasl_mechanism = "PLAIN",
    sasl_plain_username = "$instance_id#$username",
    sasl_plain_password = "$password",
    api_version = (0,10,0)
    )
    message = "Hello World! Hello Ckafka!"
    msg = json.dumps(message).encode()
    producer.send('topic_name',value = msg)
    print("produce message " + message + " success.");
    producer.close()
    
    Parameter Description
    bootstrap_servers Accessed network, which can be copied from the Network column in the Access Mode section on the Instance Details page in the console.
    img
    sasl_plain_username Username in the format of instance ID + # + username. The instance ID can be obtained in Basic Info on the Instance Details page in the CKafka console, and the username is set when the user is created in User Management.
    sasl_plain_password User password, which is set when the user is created in User Management on the Instance Details page in the CKafka console.
    topic_name Topic name, which can be copied from the Topic Management page in the console.
    img
    1. Compile and run producer.py.
    2. View the operation result.
    3. On the Topic Management page in the CKafka console, select the corresponding topic and click More > Message Query to view the just sent message.

    Step 3. Consume the message

    1. Modify the configuration parameters in the message consumption program consumer.py.
    #coding:utf8
    from kafka import KafkaConsumer
    consumer = KafkaConsumer(
    '$topic_name',
    group_id = "$group_id",
    bootstrap_servers = ['$domainName:$port'],
    security_protocol = "SASL_PLAINTEXT",
    sasl_mechanism = 'PLAIN',
    sasl_plain_username = "$instance_id#$username",
    sasl_plain_password = "$password",
    api_version = (0,10,0)
    )
    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 Accessed network, which can be copied from the Network column in the Access Mode section on the Instance Details page in the console.
    img
    group_id Consumer group ID, which can be customized according to the business needs.
    sasl_plain_username Username in the format of instance ID + # + username. The instance ID can be obtained in Basic Info on the Instance Details page in the CKafka console, and the username is set when the user is created in User Management.
    sasl_plain_password User password, which is set when the user is created in User Management on the Instance Details page in the CKafka console.
    topic_name Topic name, which can be copied from the Topic Management page in the console.
    img
    1. Compile and run consumer.py.

    2. View the operation result.

    3. On the Consumer Group page in the CKafka console, select the corresponding consumer group, enter the topic name in Topic Name, and click Query Details to view the consumption details.