You need to enable JavaScript to run this app.
Message Queue for Kafka

Message Queue for Kafka

Copy page
Download PDF
Connect instance
Connect to instance by using SASL_PLAINTEXT endpoint
Copy page
Download PDF
Connect to instance by using SASL_PLAINTEXT endpoint
This topic introduces the steps to connect to a Kafka instance via SASL_PLAINTEXT endpoint for message production and consumption.​
Background information​
Message Queue for Kafka provides SASL/PLAIN protocol for secure access, i.e., the SASL_PLAINTEXT endpoint. When connecting to the instance via SASL_PLAINTEXT endpoint, authentication with SASL username and password is required before accessing the Kafka instance. Message Queue for Kafka offers the PLAIN mechanism and SCRAM mechanism for access. You can create SASL users when you create the instance or create as needed after creating the instance, using different mechanisms to produce or consume messages accordingly.​
By default, you can access the Kafka instance within the VPC. After you enable public network access, you can also access the instance through the public network. Both network types support access via SASL_PLAINTEXT endpoint. If instance access in a public network environment requires SSL authentication, we recommend that you choose the SASL_SSL endpoint. For detailed access methods, refer to Connect to the instance using SASL_SSL endpoint.​
Prerequisites​
  • SASL_PLAINTEXT endpoint information, including connection address and port number, is obtained. For details, refer to View endpoint.​
  • If accessing the instance through the public network, it is recommended that the EIP bandwidth limit bound to the instance exceed the estimated peak public network traffic. For details, refer to Configure public network access.​
  • The JDK and configured environment variables are installed successfully. The open-source Kafka client, such as the Kafka 2.2.2 client, is downloaded.​
Configure authentication​
In the Kafka client package's ./config directory, modify the consumer.properties and producer.properties files.​
Add the following content to the files according to the SASL mechanism type. For detailed configuration file descriptions, refer to the configuration file.​
  • When using the PLAIN mechanism:​
  • Add the following to the consumer.properties and producer.properties files. Fill in the username and password with the PLAIN user's username and password.​
  • ​
    sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="{$plainusername}" password="{$plainpwxxx}";​
    sasl.mechanism=PLAIN​
    security.protocol=SASL_PLAINTEXT​
    ​
  • When using the SCRAM-SHA-256 mechanism:​
  • Add the following to the consumer.properties and producer.properties files. Fill in the username and password with the SCRAM user's username and password.​
  • ​
    sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="{$scramusername}" password="{$scrampw}";​
    sasl.mechanism=SCRAM-SHA-256​
    security.protocol=SASL_PLAINTEXT​
    ​
Generate message​
  1. Unzip the Kafka client files.​
  1. Open the terminal in the ./bin directory.​
  1. Execute the following command to start the producer and begin generating messages.​
  • ​
    bash kafka-console-producer.sh --broker-list ${SASL endpoint} --topic ${Topic name} --producer.config ../config/producer.properties​
    ​
  • ​
    Parameters​
    Description​
    SASL endpoint​
    Name and port of the SASL_PLAINTEXT endpoint that are obtained from the console instance details page.​
    Topic name​
    Topic name of Kafka instance.​
    ​
  1. Enter the message content to produce, and then press Enter to send this message to the Kafka instance.​
  • Each line entered after the crusor will be packaged into a message and sent to the Kafka instance. To stop producing, use the Ctrl+C command to exit message production.​
  • ​
    [root@kafkaecs bin]# bash kafka-console-producer.sh --broker-list kafka-cnngc7an0qp****.kafka.ivolces.com:9093 --topic mytopic --producer.config ../config/producer.properties​
    >Hello world!​
    >This is a Kafka message!​
    >^C[root@kafkaecs bin]#​
    ​
Consume messages​
  1. Open another terminal and run the following command to consume messages.​
  • To stop consuming, use the Ctrl+C command to exit message consumption.​
  • ​
    bash kafka-console-consumer.sh --bootstrap-server ${SASL endpoint} --topic ${Topic name} --group ${Consumer group name} --from-beginning --consumer.config ../config/consumer.properties​
    ​
  • ​
    Parameters​
    Description​
    SASL endpoint​
    Name and port of the SASL_PLAINTEXT endpoint that are obtained from the console instance details page.​
    Topic name​
    Topic name of Kafka instance.​
    Consumer group name​
    The name of custom consumer group.​
    If you have specified the consumer group name in the configuration file, ensure that the same name is used in the command line. Otherwise, consumption may fail.​
    ​
  1. After successful operation, the consumer can receive the messages sent by the producer, with the content matching the producer's messages.​
  • ​
    [root@kafka bin]# bash kafka-console-consumer.sh --bootstrap-server kafka-cnngc7an0qpv****.kafka.ivolces.com:9093 --topic mytopic --group grouptest --from-beginning --consumer.config ../config/consumer.properties​
    Hello world!​
    This is a Kafka message!​
    ^CProcessed a total of 2 messages​
    [root@kafka bin]#​
    ​
​
Last updated: 2025.08.08 18:39:48