Help Center> Distributed Message Service for RabbitMQ> Getting Started> Getting Started with RabbitMQ for Message Production and Consumption
Updated on 2024-07-16 GMT+08:00

Getting Started with RabbitMQ for Message Production and Consumption

This section takes an example to get you started with DMS for RabbitMQ. The example creates a RabbitMQ instance with SSL disabled, and accesses it over a private network on a client within a VPC. As a result, messages can be produced and consumed.

Figure 1 Procedure for using DMS for RabbitMQ
  1. Step 1: Preparations

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

  2. Step 2: Creating a RabbitMQ Instance

    You can select the specifications and quantity when creating a RocketMQ instance.

  3. Step 3: Accessing an Instance for Message Production and Consumption

    A client connects to the instance with SSL disabled using the demo provided by RabbitMQ.

Step 1: Preparations

  1. Sign up for a Huawei ID and complete real-name authentication.

    For details, see Signing up for a HUAWEI ID and Enabling Huawei Cloud Services and Real-Name Authentication.

    If you already have a Huawei account and have completed real-name authentication, skip this step.

  2. Top up the account.

    Ensure that your account has sufficient balance before creating a RabbitMQ instance. For details about how to top up an account, see Topping Up an Account.

  3. Grant RabbitMQ instance permissions.

    To achieve fine-grained management of your cloud resources, create Identity and Access Management (IAM) user groups and users and grant specified permissions to the users. For more information, see Creating a User and Granting DMS for RabbitMQ Permissions.

  4. Create a VPC and subnet.

    Before creating a RabbitMQ instance, ensure that a VPC and a subnet are available. For details about how to create a VPC and subnet, see Creating a VPC and Subnet.

    The VPC must be created in the same region as the RabbitMQ instance.

  5. Create a security group and add security group rules.

    Before creating a RabbitMQ instance, ensure that a security group is available. For details about how to create a security group, see Creating a Security Group.

    To connect to RabbitMQ instances, add the security group rules described in Table 1. Other rules can be added based on site requirements.
    Table 1 Security group rules

    Direction

    Protocol

    Port

    Source address

    Description

    Inbound

    TCP

    5672

    0.0.0.0/0

    Accessing a RabbitMQ instance (SSL disabled)

    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 RabbitMQ instance over a private network within a VPC, you do not need to add the rules described in Table 1.

  6. Construct a client for message production and consumption.

    This section uses a Linux elastic cloud server (ECS) as the client. Before creating a RabbitMQ instance, create an ECS with elastic IPs (EIPs), install JDK, and configure the environment variables.
    1. Log in to the console, click in the upper left corner, click Elastic Cloud Server under Computing, and then create an ECS.

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

    2. Log in to an ECS as user root.
    3. Install Java JDK and configure the environment variables JAVA_HOME and PATH.
      1. Download a JDK.

        Use Oracle JDK instead of ECS's default JDK (for example, OpenJDK), because ECS's default JDK may not be suitable. Obtain Oracle JDK 1.8.111 or later from Oracle's official website.

      2. Decompress the JDK.
        tar -zxvf jdk-8u321-linux-x64.tar.gz

        Change jdk-8u321-linux-x64.tar.gz to your JDK version.

      3. Open the .bash_profile file.
        vim ~/.bash_profile
      4. Add the following content:
        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.

      5. Press Esc. Enter the following line and press Enter. Save the .bash_profile file and exit.
        :wq
      6. Run the following command to make the change take effect:
        source .bash_profile
      7. Check whether the JDK is installed.
        java -version
        If the following message is returned, the JDK is installed.
        java version "1.8.0_321"

Step 2: Creating a RabbitMQ Instance

  1. Go to the Buy Instance page.
  2. Set basic instance information shown in Figure 2. Table 2 lists the configuration details.

    Table 2 Basic instance settings

    Parameter

    Description

    Billing Mode

    Select Pay-per-use, which is a postpaid mode. You can pay after using the service, and will be billed for your usage duration. The fees are calculated in seconds and settled by hour.

    Region

    DMS for RabbitMQ in different regions cannot communicate with each other over an intranet. Select a nearest location for low latency and fast access.

    Select AP-Singapore.

    Project

    Projects isolate compute, storage, and network resources across geographical regions. For each region, a preset project is available.

    Select AP-Singapore (default).

    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

    You can customize a name that complies with the rules: 4–64 characters; starts with a letter; can contain only letters, digits, hyphens (-), and underscores (_).

    Enter "rabbitmq-test".

    Enterprise Project

    This parameter is for enterprise users. An enterprise project manages project resources in groups. Enterprise projects are logically isolated.

    Select default.

    Version

    RabbitMQ version.

    Select 3.8.35.

    Architecture

    Select Single-node, which indicates that a RabbitMQ broker will be deployed.

    Broker Flavor

    Select a broker flavor as required.

    Select rabbitmq.2u4g.single.

    Brokers

    Fixed and the default value is 1.

    Storage space per broker

    Select the disk type and specify the disk size as required.

    Total storage space = Storage space per broker × Broker quantity. The disk type cannot be changed once the instance is created.

    Select Ultra-high I/O and enter 100.

    Figure 2 Configuring basic instance information

  3. Configure the instance network as shown in Figure 3. For details, see Table 3.

    Table 3 Configuring instance network

    Parameter

    Description

    VPC

    The VPC and subnet cannot be changed once the instance is created.

    Select the VPC and subnet prepared in 4.

    Security group

    Select the security group prepared in 5.

    Figure 3 Configuring instance network

  4. Set the instance access mode as shown in Figure 4. For details, see Table 4.

    Table 4 Setting the instance access mode

    Parameter

    Description

    SSL

    Skip it.

    Username

    Enter the username used for accessing the instance.

    A username should contain 4 to 64 characters, start with a letter, and contain only letters, digits, hyphens (-), and underscores (_).

    Enter test.

    Password

    Enter the password used for accessing the instance.

    A password must meet the following requirements:

    • Contains 8 to 32 characters.
    • Contains at least three types of the following characters: uppercase letters, lowercase letters, digits, and special characters `~! @#$ %^&*()-_=+\|[{}];:'",<.>? and spaces, and cannot start with a hyphen (-).
    • Cannot be the username spelled forwards or backwards.
    Figure 4 Configuring the instance access mode

  5. Skip Advanced Settings.
  6. Click Buy.
  7. Confirm the instance information, read and agree to the Huawei Cloud Customer Agreement, and then submit the request.
  8. Return to the instance list and check whether the 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 the instance that fails to be created and try purchasing another one. If the instance purchase fails again, contact customer service.

  9. After the instance is created, click its name to go to the instance details page.
  10. In the Connection area, view and record the connection address.

    Figure 5 Connection address

Step 3: Accessing an Instance for Message Production and Consumption

  1. Go to the root directory on the ECS and download the sample project code RabbitMQ-Tutorial.zip.

    wget https://dms-demo.obs.cn-north-1.myhuaweicloud.com/RabbitMQ-Tutorial.zip

    /root is the path for storing the sample project code. Change it to the actual path if needed.

  2. Run the following command to decompress RabbitMQ-Tutorial.zip:

    unzip RabbitMQ-Tutorial.zip

  3. Run the following command to navigate to the RabbitMQ-Tutorial directory, which contains the precompiled JAR file:

    cd RabbitMQ-Tutorial

  4. Produce messages using the sample project.

    java -cp .:rabbitmq-tutorial.jar Send ${host} ${port} ${user} ${password}

    Description:

    Sample message production:

    [root@ecs-test RabbitMQ-Tutorial]# java -cp .:rabbitmq-tutorial.jar Send 192.168.xx.40 5672 test Zxxxxxxs
     [x] Sent 'Hello World!'
    [root@ecs-test RabbitMQ-Tutorial]# java -cp .:rabbitmq-tutorial.jar Send 192.168.xx.40 5672 test Zxxxxxxs
     [x] Sent 'Hello World!'

  5. Consume messages using the sample project.

    java -cp .:rabbitmq-tutorial.jar Recv ${host} ${port} ${user} ${password}

    Description:

    Sample message consumption:

    [root@ecs-test RabbitMQ-Tutorial]# java -cp .:rabbitmq-tutorial.jar Recv 192.168.xx.40 5672 test Zxxxxxxs
     [*] Waiting for messages. To exit press CTRL+C
     [x] Received 'Hello World!'
     [x] Received 'Hello World!'

    Press Ctrl+C to cancel.

Related Information