Updated on 2023-12-11 GMT+08:00

Accessing a RabbitMQ Instance with SSL Encryption

If SSL is enabled, data will be encrypted before transmission for enhanced security.

This section describes intra-VPC access to a RabbitMQ instance with SSL enabled.

Prerequisites

  • A RabbitMQ instance has been created following the instructions in Buying an Instance, and the username and password used to create the instance have been obtained.
  • The Instance Address (Private Network) or Instance Address (Public Network) of the instance has been recorded from the instance details.
  • An ECS has been created, and its VPC, subnet, and security group configurations are the same as those of the RabbitMQ instance.

Accessing the Instance in CLI Mode

  1. Log in to the ECS. If public network access is enabled, log in to the server for running commands.
  2. Install JDK or JRE, and add the following lines to .bash_profile in the home directory to configure the environment variables JAVA_HOME and PATH:

    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.

  3. Run the following command to download RabbitMQ-Tutorial-SSL.zip:

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

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

    $ unzip RabbitMQ-Tutorial-SSL.zip

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

    $ cd RabbitMQ-Tutorial-SSL

  6. Create messages using the sample project.

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

    host indicates the connection address for accessing the instance. port is the listening port of the instance, which is 5671 by default. user and password indicate the username and password used for accessing the instance.

    Figure 1 Sample project for message creation

    Press Ctrl+C to exit.

  7. Retrieve messages using the sample project.

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

    host indicates the connection address for accessing the instance. port is the listening port of the instance, which is 5671 by default. user and password indicate the username and password used for accessing the instance.

    Figure 2 Sample project for message retrieval

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

Java Sample Code

Accessing a RabbitMQ instance and creating messages

ConnectionFactory factory = new ConnectionFactory();
 factory.setHost(host);
 factory.setPort(port);

 factory.setUsername(user);
 factory.setPassword(password);
 factory.useSslProtocol();
 Connection connection = factory.newConnection();
 Channel channel = connection.createChannel();

 channel.queueDeclare(QUEUE_NAME, false, false, false, null);

 String message = "Hello World!";
 channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
 System.out.println(" [x] Sent '" + message + "'");

 channel.close();
 connection.close();

Accessing a RabbitMQ instance and retrieving messages

ConnectionFactory factory = new ConnectionFactory();
 factory.setHost(host);
 factory.setPort(port);
 factory.setUsername(user);
 factory.setPassword(password);
 factory.useSslProtocol();
 Connection connection = factory.newConnection();
 Channel channel = connection.createChannel();

 channel.queueDeclare(QUEUE_NAME, false, false, false, null);
 System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

 Consumer consumer = new DefaultConsumer(channel)
 {
     @Override
     public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
             byte[] body)
             throws IOException
     {
         String message = new String(body, "UTF-8");
         System.out.println(" [x] Received '" + message + "'");
     }
 };
 channel.basicConsume(QUEUE_NAME, true, consumer);