tencent cloud

피드백

Public Network Access Through SASL_SSL

마지막 업데이트 시간:2024-11-07 15:40:42

    Overview

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

    Prerequisites

    Directions

    Step 1. Make preparations

    1. Create an access point.
    1.1 On the Instance List page in the CKafka console, click the target instance ID to enter the instance details page.
    1.2 In Basic Info > Access Mode, click Add a routing policy. In the pop-up window, select Route Type: Public domain name access, Access Mode: SASL_SSL.
    
    2. Create a role.
    On the User Management tab page under ++ACL Policy Management++, create a role and set a password.
    
    3. Create a topic.
    Create a topic on the Topic Management tab page as instructed in Creating a topic.
    4. Add the Python dependent library.
    Run the following command to install the dependent library:
    pip install kafka-python

    Step 2. Produce messages

    1. Modify the configuration parameters in the message production program producer.py.
    producer = KafkaProducer(
    bootstrap_servers = ['xx.xx.xx.xx:port'],
    api_version = (1, 1),
    
    #
    # Public network access through SASL_SSL
    #
    security_protocol = "SASL_SSL",
    sasl_mechanism = "PLAIN",
    sasl_plain_username = "instanceId#username",
    sasl_plain_password = "password",
    ssl_cafile = "CARoot.pem",
    ssl_check_hostname = False,
    )
    
    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 in Basic Info on the instance details page in the console.
    
    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 on the User Management tab page under ACL Policy Management.
    sasl_plain_password
    User password, which is set when the user is created on the User Management tab page under ACL Policy 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
    
    CARoot.pem
    Certificate path that is required when the access mode is SASL_SSL.
    2. Compile and run producer.py.
    3. View the execution result.
    
    4. On the Topic Management tab page on the instance details page in the CKafka console, select the target topic, and click More > Message Query to view the message just sent.
    

    Step 3. Consume messages

    1. Modify the configuration parameters in the message consumption program consumer.py.
    consumer = KafkaConsumer(
    'topic_name',
    group_id = "group_id",
    bootstrap_servers = ['xx.xx.xx.xx:port'],
    api_version = (1,1),
    
    #
    # Public network access through SASL_SSL
    #
    security_protocol = "SASL_SSL",
    sasl_mechanism = 'PLAIN',
    sasl_plain_username = "instanceId#username",
    sasl_plain_password = "password",
    ssl_cafile = "CARoot.pem",
    ssl_check_hostname = False,
    
    )
    
    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 in Basic Info on the instance details page in the console.
    
    group_id
    Consumer group ID, which can be customized based on business requirements.
    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 on the User Management tab page under ACL Policy Management.
    sasl_plain_password
    User password, which is set when the user is created on the User Management tab page under ACL Policy 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
    
    CARoot.pem
    Certificate path that is required when the access mode is SASL_SSL.
    2. Compile and run consumer.py.
    3. View the execution result.
    
    4. On the Consumer Group tab page in the CKafka console, select the corresponding consumer group name, enter the topic name, and click View Details to view the consumption details.
    
    
    문의하기

    고객의 업무에 전용 서비스를 제공해드립니다.

    기술 지원

    더 많은 도움이 필요하시면, 티켓을 통해 연락 바랍니다. 티켓 서비스는 연중무휴 24시간 제공됩니다.

    연중무휴 24시간 전화 지원