tencent cloud

Feedback

Public Network Access Through SASL_PLAINTEXT

Last updated: 2024-01-09 15:00:32

    Overview

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

    Prerequisites

    You have installed GCC.
    You have installed Node.js.
    You have downloaded the demo.

    Directions

    Step 1. Install the C++ dependent library

    1. Run the following command to switch to the yum source configuration directory /etc/yum.repos.d/.
    cd /etc/yum.repos.d/
    2. Create the yum source configuration file confluent.repo.
    [Confluent.dist]
    name=Confluent repository (dist)
    baseurl=https://packages.confluent.io/rpm/5.1/7
    gpgcheck=1
    gpgkey=https://packages.confluent.io/rpm/5.1/archive.key
    enabled=1
    [Confluent]
    name=Confluent repository
    baseurl=https://packages.confluent.io/rpm/5.1
    gpgcheck=1
    gpgkey=https://packages.confluent.io/rpm/5.1/archive.key
    enabled=1
    3. Run the following command to install the C++ dependent library.
    yum install librdkafka-devel

    Step 2. Install the Node.js dependent library

    1. Run the following command to specify the OpenSSL header file path for the preprocessor.
    export CPPFLAGS=-I/usr/local/opt/openssl/include
    2. Run the following command to specify the OpenSSL library path for the connector.
    export LDFLAGS=-L/usr/local/opt/openssl/lib
    3. Run the following command to install the Node.js dependent library.
    npm install i --unsafe-perm node-rdkafka

    Step 3. Prepare configurations

    Create the CKafka configuration file setting.js.
    module.exports = {
    'sasl_plain_username': 'ckafka-xxxxxxx#ckafkademo',
    'sasl_plain_password': 'ckafkademo123',
    'bootstrap_servers': ["xxx.ckafka.tencentcloudmq.com:6018"],
    'topic_name': 'xxx',
    'group_id': 'xxx'
    }
    Parameter
    Description
    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 ACL Policy Management > User Management.
    sasl_plain_password
    User password, which is set when the user is created in ACL Policy Management > User Management on the instance details page in the CKafka console.
    bootstrap_servers
    SASL access point. You can obtain it in Basic Info >Access Mode on the instance details page in the CKafka console.
    
    topic_name
    Topic name, which can be created and obtained in Topic Management on the instance details page in the CKafka console.
    
    group_id
    Consumer group ID, which can be customized as needed.

    Step 4. Send messages

    1. Write a message production program named producer.js.
    const Kafka = require('node-rdkafka');
    const config = require('./setting');
    console.log("features:" + Kafka.features);
    console.log(Kafka.librdkafkaVersion);
    
    var producer = new Kafka.Producer({
    'api.version.request': 'true',
    'bootstrap.servers': config['bootstrap_servers'],
    'dr_cb': true,
    'dr_msg_cb': true,
    'security.protocol' : 'SASL_PLAINTEXT',
    'sasl.mechanisms' : 'PLAIN',
    'sasl.username' : config['sasl_plain_username'],
    'sasl.password' : config['sasl_plain_password']
    });
    
    var connected = false
    
    producer.setPollInterval(100);
    
    producer.connect();
    
    producer.on('ready', function() {
    connected = true
    console.log("connect ok")
    
    });
    
    function produce() {
    try {
    producer.produce(
    config['topic_name'],
    new Buffer('Hello CKafka SASL'),
    null,
    Date.now()
    );
    } catch (err) {
    console.error('Error occurred when sending message(s)');
    console.error(err);
    }
    }
    
    producer.on("disconnected", function() {
    connected = false;
    producer.connect();
    })
    
    producer.on('event.log', function(event) {
    console.log("event.log", event);
    });
    
    producer.on("error", function(error) {
    console.log("error:" + error);
    });
    
    producer.on('delivery-report', function(err, report) {
    console.log("delivery-report: producer ok");
    });
    // Any errors we encounter, including connection errors
    producer.on('event.error', function(err) {
    console.error('event.error:' + err);
    })
    
    setInterval(produce,1000,"Interval");
    2. Run the following command to send messages.
    node producer.js
    3. View the execution result.
    
    
    
    4. On the Topic Management tab 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 5. Subscribe to messages

    1. Create the message consumption program consumer.js.
    consumer.on('event.log', function(event) {
    console.log("event.log", event);
    });
    
    consumer.on('error', function(error) {
    console.log("error:" + error);
    });
    
    consumer.on('event', function(event) {
    console.log("event:" + event);
    });const Kafka = require('node-rdkafka');
    const config = require('./setting');
    console.log(Kafka.features);
    console.log(Kafka.librdkafkaVersion);
    console.log(config)
    
    var consumer = new Kafka.KafkaConsumer({
    'api.version.request': 'true',
    'bootstrap.servers': config['bootstrap_servers'],
    'security.protocol' : 'SASL_PLAINTEXT',
    'sasl.mechanisms' : 'PLAIN',
    'message.max.bytes': 32000,
    'fetch.message.max.bytes': 32000,
    'max.partition.fetch.bytes': 32000,
    'sasl.username' : config['sasl_plain_username'],
    'sasl.password' : config['sasl_plain_password'],
    'group.id' : config['group_id']
    });
    
    consumer.connect();
    
    consumer.on('ready', function() {
    console.log("connect ok");
    consumer.subscribe([config['topic_name']]);
    consumer.consume();
    })
    
    consumer.on('data', function(data) {
    console.log(data);
    });
    2. Execute the following command to send messages.
    node consumer.js
    3. View the execution result.
    
    
    
    4. ‌On the Consumer Group page in the Ckafka console‌, click the triangle icon on the left of the target consumer group name, enter the topic name in the search box, and click View Details to view the consumption details.
    
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support