Help Center/ Distributed Message Service for RocketMQ/ Getting Started/ Getting Started with RocketMQ to Produce and Consume Messages
Updated on 2024-07-10 GMT+08:00

Getting Started with RocketMQ to Produce and Consume Messages

This document takes the example of creating and connecting to a RocketMQ instance with SSL enabled to get you quickly started with Distributed Message Service (DMS) for RocketMQ.

Procedure

Figure 1 Procedure for using DMS for RocketMQ
  1. Prepare the environment.

    A RocketMQ instance runs in a Virtual Private Cloud (VPC). Before creating an instance, ensure that a VPC is available.

  2. Create a RocketMQ instance.

    You can select the specification and quantity and enable SSL when creating a RocketMQ instance. Enabling SSL secures data transmission with encryption.

  3. Create a topic.

    After an instance is created, create a topic for sending and receiving messages.

  4. Connect to the instance.

    On the client, connect to the instance and use commands to create and retrieve messages.

For the basic concepts of RocketMQ, see RocketMQ Concepts.

Step 1: Prepare the Environment

VPC

A VPC provides an isolated virtual network for your RocketMQ instances. You can configure and manage the network as required.

  1. Before creating a RocketMQ instance, ensure that a VPC and a subnet are available.

    For details, see Creating a VPC. If you already have an available VPC and subnet, you do not need to create new ones.

    Note the following when creating a VPC and subnet:

    • The VPC and the RocketMQ instance must be in the same region.
    • Use the default settings when creating a VPC and subnet.

  2. Before creating a RocketMQ instance, ensure that a security group is available.

    For details, see Creating a Security Group. If you already have an available security group, you do not need to create a new one.

    To use DMS for RocketMQ, add the security group rules described in Table 1. You can also add other rules based on site requirements.
    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.

    Inbound

    TCP

    10100-10199

    0.0.0.0/0

    The port is used for accessing service nodes.

    After a security group is created, it has a default inbound rule that allows communication among ECSs within the security group and a default outbound rule that allows all outbound traffic. If you access your RocketMQ instance within a VPC, you do not need to add the rules described in Table 1.

ECS

Before connecting to a RocketMQ instance, ensure that you have purchased an ECS, installed the JDK, and configured 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.

  1. Log in to the management console, click in the upper left corner, click Elastic Cloud Server under Computing, and then create an ECS.

    For details, see Purchasing an ECS. If you already have an available ECS, skip this step.

  2. Log in to the ECS.
  3. Install JDK or JRE, and add the following contents to .bash_profile in the home directory to configure the environment variables JAVA_HOME and PATH: In this command, /opt/java/jdk1.8.0_151 is the JDK installation path. Change it to the path where you install JDK or JRE.

    export JAVA_HOME=/opt/java/jdk1.8.0_151 
    export PATH=$JAVA_HOME/bin:$PATH

    Run the source .bash_profile command for the modification to take effect.

    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.

Step 2: Create a RocketMQ Instance

This section takes the example of creating a RocketMQ v4.8.0 instance with SSL enabled in the AP–Singapore region to describe how to create a RocketMQ instance on the console.

  1. Go to the Buy Instance page.
  2. Set Billing Mode to Pay-per-use.
  3. Select a region closest to your application to reduce latency and accelerate access.
  4. Select a project from the drop-down list.
  5. Select one AZ or at least three AZs.
  6. Specify the Instance Name and the Enterprise Project.
  7. Set the instance information. For details, see Table 2.

    Table 2 Setting instance information

    Parameter

    Description

    Specifications

    Select Default.

    Version

    Select 4.8.0.

    Fixed once the instance is created. Use the same version as your client.

    Architecture

    Select Cluster.

    CPU Architecture

    Select x86.

    Broker Flavor

    Select rocketmq.4u8g.cluster.

    Brokers

    Enter 1.

    Storage Space per Broker

    Select Ultra-high I/O and enter 300 GB.

    Total storage space of an instance = Storage space per broker × Number of brokers

  8. Configure the instance network. For details, see Table 3.

    Table 3 Configuring instance network

    Parameter

    Description

    VPC

    Select the created VPC and subnet.

    After the RocketMQ instance is created, its VPC and subnet cannot be changed.

    Security Group

    Select the created security group.

    SSL

    Enable it.

    ACL

    Enable it.

  9. Click Advanced Settings. For more information, see Table 4.

    Table 4 Advanced settings

    Parameter

    Description

    Public Access

    Do not enable it.

    Tags

    Skip it.

    Description

    Skip it.

  10. Click Buy.
  11. Confirm the instance information and submit the request.
  12. 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.

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.

  1. Log in to the DMS for RocketMQ consoleconsole.
  2. Click a RocketMQ instance to go to the instance details page.
  3. In the navigation pane, choose Topics.
  4. Click Create Topic.
  5. Configure the topic name and other parameters by referring to Table 5.

    Table 5 Topic parameters

    Parameter

    Description

    Topic Name

    Name of the topic.

    The topic name must be unique. Otherwise, the topic cannot be created.

    Once the topic is created, you cannot modify its name.

    NOTE:

    A percent (%) or vertical bar (|) contained in a topic name will be converted to an underscore (_) by Cloud Eye. For example, if a topic name is test%01, it will be displayed as test_01 on Cloud Eye.

    Permission

    Topic permission, which can be publish/subscribe, publish, or subscribe.

    Brokers

    Specify the broker to create the topic on, and the number of queues in the topic.

    If the instance is deployed on multiple brokers, click Add to add more brokers and set the number of queues in the topic.

  6. Click OK.

Step 4: Connect to a RocketMQ Instance to Create and Retrieve Messages

This section describes how to use TCP to connect to a RocketMQ instance with SSL in CLI mode.

  1. Download the rocketmq-tutorial software package.

    wget https://dms-demos.obs.cn-north-1.myhuaweicloud.com/rocketmq-tutorial.zip

  2. Decompress the rocketmq-tutorial package.

    unzip rocketmq-tutorial.zip

  3. Go to the rocketmq-tutorial/bin directory.

    cd rocketmq-tutorial/bin

  4. Create normal messages using the sample project.

    JAVA_OPT=-Dtls.enable=true sh mqadmin sendMessage -n "${Connection addresses}" -t ${Topic name} -p "hello rocketmq"

    Parameter description:

    • Connection addresses: the RocketMQ instance address.
    • Topic name: name of the topic created for the RocketMQ instance

    In the following example, 100.xxx.xxx.89:8200;100.xxx.xxx.144:8200 are the connection addresses to the RocketMQ instance, and topic-test is the topic name.

    JAVA_OPT=-Dtls.enable=true sh mqadmin sendMessage -n "100.xxx.xxx.89:8200;100.xxx.xxx.144:8200" -t topic-test -p "hello rocketmq"

    Press Ctrl+C to exit.

  5. Retrieve normal messages using the sample project.

    JAVA_OPT=-Dtls.enable=true sh mqadmin consumeMessage -n "${Connection addresses}" -t ${Topic name}

    Parameter description:

    • Connection addresses: the RocketMQ instance address.
    • Topic name: name of the topic created for the RocketMQ instance

    In the following example, 100.xxx.xxx.89:8200;100.xxx.xxx.144:8200 are the connection addresses to the RocketMQ instance, and topic-test is the topic name.

    JAVA_OPT=-Dtls.enable=true sh mqadmin consumeMessage -n "100.xxx.xxx.89:8200;100.xxx.xxx.144:8200" -t topic-test

    To stop consuming messages, press Ctrl+C to exit.

  6. Create messages with traces using the sample project.

    JAVA_OPT=-Dtls.enable=true sh mqadmin sendMessage -n "${Connection addresses}" -t ${Topic name} -p "hello rocketmq" -m true

    Parameter description:

    • Connection addresses: the RocketMQ instance address.
    • Topic name: name of the topic created for the RocketMQ instance

    In the following example, 100.xxx.xxx.89:8200;100.xxx.xxx.144:8200 are the connection addresses to the RocketMQ instance, and topic-test is the topic name.

    JAVA_OPT=-Dtls.enable=true sh mqadmin sendMessage -n "100.xxx.xxx.89:8200;100.xxx.xxx.144:8200" -t topic-test -p "hello rocketmq" -m true

    Press Ctrl+C to exit.

  7. Retrieve messages and send the message traces using the sample project.

    JAVA_OPT=-Dtls.enable=true sh mqadmin consumeMessage -n "${Connection addresses}" -t ${Topic name} -m true

    Parameter description:

    • Connection addresses: the RocketMQ instance address.
    • Topic name: name of the topic created for the RocketMQ instance

    In the following example, 100.xxx.xxx.89:8200;100.xxx.xxx.144:8200 are the connection addresses to the RocketMQ instance, and topic-test is the topic name.

    JAVA_OPT=-Dtls.enable=true sh mqadmin consumeMessage -n "100.xxx.xxx.89:8200;100.xxx.xxx.144:8200" -t topic-test -m true

    Press Ctrl+C to exit.