Getting Started with RocketMQ to Produce and Consume Messages
This document takes the example of creating a RocketMQ instance with SSL enabled and accessing it on a client for message production and consumption to get you quickly started with Distributed Message Service (DMS) for RocketMQ.
Procedure
- Step 1: Preparations
Before creating a RocketMQ instance, an authenticated Huawei Cloud account with balance, users with required permissions, a set of Virtual Private Cloud (VPC) and subnet, a security group, an Elastic Cloud Server (ECS), and an environment should be prepared.
- Step 2: Create a RocketMQ Instance
Enable SSL, disable ACL, and configure the created VPC and subnet, and security group.
- Step 3: Create a Topic
After an instance is created, create a topic for sending and receiving messages.
- Step 4: Connect to a RocketMQ Instance to Produce and Consume Messages
On the client, connect to the instance and use commands to produce and consume messages.
Step 1: Preparations
- Sign up for a Huawei ID and complete real-name authentication.
If you already have a Huawei account, skip this step. If you do not have one, see Signing up for a HUAWEI ID and Enabling Huawei Cloud Services and Real-Name Authentication.
- Top up the account.
If your account balance is sufficient, go to the next task. Otherwise, top up your account. For details, see Topping Up an Account.
- Grant RocketMQ instance permissions.
The RocketMQ administrator permission DMS FullAccess is required. For details, see Creating a User and Granting DMS for RocketMQ Permissions.
- Create a VPC and subnet.
The VPC must be created in the same region as the RocketMQ instance.
A RocketMQ instance runs in a Virtual Private Cloud (VPC). Before creating an instance, ensure that a VPC is available. For details about how to create a VPC and a subnet, see Creating a VPC and Subnet.
- Create a security group.
See Creating a Security Group.
To connect to RocketMQ instances, add the security group rules described in Table 1.
Table 1 Security group rules Direction
Protocol
Port
Source
Description
Inbound
TCP
8100
0.0.0.0/0
The port is used for intra-VPC access to metadata nodes.
After a security group is created, its default inbound rule allows communication among ECSs within the security group and its default outbound rule allows all outbound traffic. In this case, you can access a RocketMQ instance within a VPC, and do not need to add rules according to Table 1.
- Create an elastic cloud server (ECS) and configure environment variables.
The following takes a Linux ECS as an example. For more information about how to install JDK and configure the environment variables for a Windows ECS, please search the Internet.
- Log in to the console, click in the upper left corner, click Elastic Cloud Server under Computing, and then create an ECS.
For details, see Purchasing a Custom ECS. If you already have an available ECS, skip this step.
- Log in to an ECS as user root.
- Install the JDK and configure the environment variables JAVA_HOME and PATH.
- Download the JDK.
Use Oracle JDK instead of ECS's default JDK (for example, OpenJDK), because ECS's default JDK may not be suitable for the sample project. Obtain Oracle JDK 1.8.111 or later from Oracle's official website.
- Run the following command to decompress the JDK package.
tar -zxvf jdk-8u321-linux-x64.tar.gz
Change jdk-8u321-linux-x64.tar.gz to your JDK version.
- Run the following command to edit the environment variable file .bash_profile:
vim ~/.bash_profile
- Add the following content to the environment variable file:
export JAVA_HOME=/opt/java/jdk1.8.0_321 export PATH=$JAVA_HOME/bin:$PATH
Change /opt/java/jdk1.8.0_321 to the path where you install JDK.
- Press Esc to exit the editing mode and run the following command to save the environment variable file:
:wq
- Run the following command to make the environment variables take effect:
source .bash_profile
- Download the JDK.
- Run the following command to check whether the JDK is successfully installed.
java -version
If the following information is displayed, the JDK is installed successfully:java version "1.8.0_321"
- Run the following command to download the rocketmq-tutorial sample software package.
wget https://dms-demos.obs.cn-north-1.myhuaweicloud.com/rocketmq-tutorial.zip
- Run the following command to decompress rocketmq-tutorial.
unzip rocketmq-tutorial.zip
- Log in to the console, click in the upper left corner, click Elastic Cloud Server under Computing, and then create an ECS.
Step 2: Create a RocketMQ Instance
Before using RocketMQ for message production and consumption, create a RocketMQ instance. The VM resource in the instance store topics.
- Go to the Buy Instance page.
- Set the instance information. For details, see Table 2.
Table 2 Setting instance information Parameter
Description
Billing Mode
Select the billing mode of the instance. Select Pay-per-use. You will be billed for your usage duration. The fees are calculated in seconds and settled by hour.
Region
For lower network latency and faster access to your resources, select the nearest region. Select AP-Singapore.
Project
Select the project in this region. Select AP-Singapore.
AZ
An AZ is a physical region where resources use independent power supply and networks. AZs are physically isolated but interconnected through an internal network. Select AZ1.
Instance Name
Enter the instance name, for example, rocketmq-test.
Enterprise Project
An enterprise project manages project resources in groups. Enterprise projects are logically isolated. Select "default". This parameter is for enterprise users.
Specifications
Select Default here, which customizes the version, instance type, architecture, flavor, and storage space for the RocketMQ instance.
Version
Select an instance version. Select 4.8.0.
Fixed once the instance is created. Use the same version as your client.
Architecture
Select an instance architecture. Select Cluster here.
Broker Flavor
Select an instance flavor. Select rocketmq.4u8g.cluster here.
Brokers
Specify the instance broker quantity. Enter 1 here.
Storage Space per Broker
Specify the disk type and storage space per broker for storing RocketMQ data. Select Ultra-high I/O and enter 300. Total storage space of an instance = Storage space per broker × Number of brokers
VPC
Select a VPC and a subnet. Here, select the ones created in Step 1: Preparations.
Security Group
Select the security group. Here, select the one created in Step 1: Preparations.
SSL
SSL: Ciphertext access with high security, but lower performance. Enable it here.
ACL
Enabling ACL can manage permissions for message production and consumption. Do not enable it here.
Advanced settings
Public Access
EIPs are required to enable public access. Do not enable it here.
Tags
Identifiers of the RocketMQ instance. Skip it here.
Description
Additional information about the instance. Skip it.
- Click Buy.
- Confirm the instance information, check the agreement, and submit the request.
- Return to the instance list and check whether the RocketMQ instance has been created.
It takes 3 to 15 minutes to create an instance. During this period, the instance status is Creating.
- If the instance is created successfully, its status changes to Running.
- If the instance is in the Creation failed state, delete it. Then create a new one. If the instance creation fails again, contact customer service.
- After the instance is created, click its name to go to the instance basic information page.
- Record the instance connection addresses for later use.
Figure 2 Recording instance connection addresses
Step 3: Create a Topic
A topic is the basic unit for sending and receiving messages. After creating a RocketMQ instance, you must manually create topics before creating and retrieving messages.
- Click a RocketMQ instance to go to the instance basic information page.
- In the navigation pane, choose Topics.
- Click Create Topic.
- Configure the topic name and other parameters by referring to Table 3.
Table 3 Topic parameters Parameter
Description
Topic Name
Enter a topic name. Enter Topic01 here.
Permission
Permission of the topic. Select Publish/Subscribe here. Producers can publish messages to this topic and consumers can consume the messages from this topic.
Brokers
Associated brokers. Select broker-0 here and enter 3 queues.
Figure 3 Creating a topic
- Click OK.
Step 4: Connect to a RocketMQ Instance to Produce and Consume Messages
- Go to the rocketmq-tutorial/bin directory on the ECS.
cd rocketmq-tutorial/bin
- Produce normal messages by the following commands.
The following is a command example:
JAVA_OPT=-Dtls.enable=true sh mqadmin sendMessage -n "10.xxx.xxx.89:8100;10.xxx.xxx.144:8100" -t Topic01 -p "hello rocketmq"
- Consume normal messages by the following commands.
The following is a command example:
JAVA_OPT=-Dtls.enable=true sh mqadmin consumeMessage -n "10.xxx.xxx.89:8100;10.xxx.xxx.144:8100" -t Topic01
The content of BODY is the consumed message content.
To stop consuming messages, press Ctrl+C to exit.
Related Information
- Learn more about RocketMQ Concepts.
- In RocketMQ instance creation, SSL can be disabled if ciphertext is not needed in access between the consumer client and the producer client. In this case, to access the RocketMQ instance, see Accessing RocketMQ on a Client (Without SSL).
- If you need multiple users and grant different topic and consumer group permissions for them, enable ACL and configure ACL users so that permissions are isolated among users. For details, see Enabling RocketMQ ACL and Configuring RocketMQ ACL Users.
- To enable public access to RocketMQ instances, see Configuring Public Access for a RocketMQ Instance.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot