tencent cloud

TDMQ for CKafka

Release Notes and Announcements
Release Notes
Broker Release Notes
Announcement
Product Introduction
Introduction and Selection of the TDMQ Product Series
What Is TDMQ for CKafka
Strengths
Scenarios
Technology Architecture
Product Series Introduction
Apache Kafka Version Support Description
Comparison with Apache Kafka
High Availability
Use Limits
Regions and AZs
Related Cloud Services
Billing
Billing Overview
Pricing
Billing Example
Changing from Postpaid by Hour to Monthly Subscription
Renewal
Viewing Consumption Details
Overdue Payments
Refund
Getting Started
Guide for Getting Started
Preparations
VPC Network Access
Public Domain Name Access
User Guide
Usage Process Guide
Configuring Account Permission
Creating Instance
Configuring Topic
Connecting Instance
Managing Messages
Managing Consumer Group
Managing Instance
Changing Instance Specification
Configuring Traffic Throttling
Configuring Elastic Scaling Policy
Configuring Advanced Features
Viewing Monitoring Data and Configuring Alarm Rules
Synchronizing Data Using CKafka Connector
Use Cases
Cluster Resource Assessment
Client Practical Tutorial
Log Integration
Open-Source Ecosystem Integration
Replacing Supporting Route (Old)
Migration Guide
Migration Solution Overview
Migrating Cluster Using Open-Source Tool
Troubleshooting
Topics
Clients
Messages
​​API Reference
History
Introduction
API Category
Making API Requests
Other APIs
ACL APIs
Instance APIs
Routing APIs
DataHub APIs
Topic APIs
Data Types
Error Codes
SDK Reference
SDK Overview
Java SDK
Python SDK
Go SDK
PHP SDK
C++ SDK
Node.js SDK
SDK for Connector
Security and Compliance
Permission Management
Network Security
Deletion Protection
Event Record
CloudAudit
FAQs
Instances
Topics
Consumer Groups
Client-Related
Network-Related
Monitoring
Messages
Agreements
CKafka Service Level Agreements
Contact Us
Glossary
문서TDMQ for CKafkaSDK ReferenceNode.js SDKSASL_PLAINTEXT Access in the Public Network

SASL_PLAINTEXT Access in the Public Network

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-01-05 15:16:59

Scenarios

This document uses the Node.js client as an example to describe how to use the SASL_PLAINTEXT method to access TDMQ for CKafka (CKafka) in the public network and send and receive messages.

Prerequisites

Operation Steps

Step 1: Preparations

1. Create an access point.
1.1 On the Instance List page, click the target instance ID to go to the instance details page.
1.2 Choose Basic Info > Access Method, and click Add a routing policy. In the pop-up window, choose Routing Type: Public Network Domain Name Access > Access Method: SASL_PLAINTEXT.

2. Create a role.
Select ACL Policy Management to go to the User Management page. On the displayed page, create a role and set the password.

3. Create a topic.
On the Topic List page in the console, create a topic (see Creating a Topic).
4. Configure the ACL policy.
Configure the topic read/write permissions for the created role by seeing Configuring Topic Read/Write Permissions.

Step 1: Installing C++ Dependency Libraries

1. Run the following command to switch to the yum repository configuration directory /etc/yum.repos.d/.
cd /etc/yum.repos.d/
2. Create the yum repository 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 C++ dependency libraries.
yum install librdkafka-devel

Step 2: Installing Node.js Dependency Libraries

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 Node.js dependency libraries.
npm install i --unsafe-perm node-rdkafka

Step 3: Preparing for 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 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.
bootstrap_servers
SASL access point. On the Basic Info page of the instance in the CKafka console, select the Access Mode module and obtain the access point from the Network column.
topic_name
Topic name. On the instance details page in the CKafka console, choose Topic List to create a topic name or obtain the desired topic name.
group_id
Consumer group ID. Define the group ID according to business requirements.

Step 4: Sending Messages

1. Write the message production program 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 running results.


4. On the Topic List page in the CKafka console, select the target topic, and choose More > Message Query to view the message just sent.

Step 5: Subscribing 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. Run the following command to consume messages.
node consumer.js
3. View the running results.


4. On the Consumer Group page in the CKafka console, select the target consumer group name, enter the topic name in the Topic Name area, and click View Details to view consumption details.


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백