Run RocketMQ in Docker

This section introduces how to quickly deploy a single-node, single-replica RocketMQ service using Docker and complete simple message sending and receiving.

Run RocketMQ in Docker - 图1System Requirements

  1. 64-bit operating system
  2. 64-bit JDK 1.8+

1.Pull RocketMQ Image

Here, we take the RocketMQ 4.9.6 version image from dockerhub as an example to introduce the deployment process.

  1. docker pull apache/rocketmq:4.9.6

2.Create a Shared Network for Containers

RocketMQ involves multiple services and requires multiple containers. Creating a Docker network facilitates communication between containers.

  1. docker network create rocketmq

3.Start NameServer

  1. # Start NameServer
  2. docker run -d --name rmqnamesrv -p 9876:9876 --net rocketmq apache/rocketmq:4.9.6 sh mqnamesrv
  3. # Verify if NameServer started successfully
  4. docker logs -f rmqnamesrv

Run RocketMQ in Docker - 图2info

We can see ‘The Name Server boot success..’, indicating that the NameServer has started successfully.

4.Start Broker

After the NameServer starts successfully, we start the Broker.

  • Linux
  • Windows
  1. # Configure the broker's IP address
  2. echo "brokerIP1=127.0.0.1" >broker.conf
  3. # Start Broker
  4. docker run -d \
  5. --name rmqbroker \
  6. --net rocketmq \
  7. -p 10912:10912 -p 10911:10911 -p 10909:10909 \
  8. -e "NAMESRV_ADDR=rmqnamesrv:9876" \
  9. -v ./broker.conf:/home/rocketmq/rocketmq-4.9.6/conf/broker.conf \
  10. apache/rocketmq:4.9.6 sh mqbroker \
  11. -c /home/rocketmq/rocketmq-4.9.6/conf/broker.conf
  12. # Verify if Broker started successfully
  13. docker logs rmqbroker
  1. # Configure the broker's IP address
  2. echo "brokerIP1=127.0.0.1" >broker.conf
  3. # Start Broker
  4. docker run -d ^
  5. --name rmqbroker ^
  6. --net rocketmq ^
  7. -p 10912:10912 -p 10911:10911 -p 10909:10909 ^
  8. -e "NAMESRV_ADDR=rmqnamesrv:9876" ^
  9. -v %cd%\broker.conf:/home/rocketmq/rocketmq-4.9.6/conf/broker.conf ^
  10. apache/rocketmq:4.9.6 sh mqbroker ^
  11. -c /home/rocketmq/rocketmq-4.9.6/conf/broker.conf
  12. # Verify if Broker started successfully
  13. docker logs rmqbroker

Run RocketMQ in Docker - 图3info

We can see ‘The broker boot success..’, indicating that the Broker has started successfully.

Run RocketMQ in Docker - 图4note

At this point, a single-node replica RocketMQ cluster has been deployed. We can use scripts for simple message sending and receiving.

5.Send and Receive Messages with Tools

  1. # Enter the broker container
  2. $ docker exec -it rmqbroker bash
  3. $ sh tools.sh org.apache.rocketmq.example.quickstart.Producer
  4. SendResult [sendStatus=SEND_OK, msgId= ...
  5. $ sh tools.sh org.apache.rocketmq.example.quickstart.Consumer
  6. ConsumeMessageThread_%d Receive New Messages: [MessageExt...

6.Send and Receive Messages with SDK

After testing with tools, we can try sending and receiving messages using the SDK. Here, we use the Java SDK as an example to introduce the process.

  1. Create a Java project in IDEA.

  2. Add the following dependencies to the pom.xml file to introduce the Java library.

    1. <dependency>
    2. <groupId>org.apache.rocketmq</groupId>
    3. <artifactId>rocketmq-client</artifactId>
    4. <version>4.9.6</version>
    5. </dependency>
  3. In the created Java project, create and run a program to send a simple message. Apache RocketMQ can send messages in three ways: synchronous, asynchronous, and one-way transmission. Here we use the synchronous mode as an example:

    1. import org.apache.rocketmq.client.producer.DefaultMQProducer;
    2. import org.apache.rocketmq.client.producer.SendResult;
    3. import org.apache.rocketmq.common.message.Message;
    4. public class ProducerExample {
    5. public static void main(String[] args) throws Exception {
    6. // Create producer instance and set the producer group name
    7. DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
    8. // Set the Name Server address (replace with actual Name Server address)
    9. producer.setNamesrvAddr("localhost:9876");
    10. producer.start();
    11. try {
    12. // Create a message instance, specifying the topic, tag, and message body
    13. Message msg = new Message("TestTopic", "TagA", ("Hello RocketMQ").getBytes());
    14. // Send the message and get the send result
    15. SendResult sendResult = producer.send(msg);
    16. System.out.println("Message sent: " + new String(msg.getBody()));
    17. System.out.println("Send result: " + sendResult);
    18. } catch (Exception e) {
    19. e.printStackTrace();
    20. System.out.println("Message sending failed.");
    21. } finally {
    22. // Shutdown the producer
    23. producer.shutdown();
    24. }
    25. }
    26. }
  4. In the created Java project, create and run a program to subscribe to simple messages. Apache RocketMQ has two consumption modes: Push and Pull. Here we use the Push mode as an example.

    1. import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
    2. import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
    3. import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
    4. import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
    5. import org.apache.rocketmq.common.message.MessageExt;
    6. import java.util.List;
    7. public class ConsumerExample {
    8. public static void main(String[] args) throws Exception {
    9. // Create consumer instance and set the consumer group name
    10. DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");
    11. // Set the Name Server address (replace with actual Name Server address)
    12. consumer.setNamesrvAddr("localhost:9876");
    13. // Subscribe to the specified topic and tag (* means all tags)
    14. consumer.subscribe("TestTopic", "*");
    15. // Register message listener
    16. consumer.registerMessageListener(new MessageListenerConcurrently() {
    17. @Override
    18. public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
    19. for (MessageExt msg : msgs) {
    20. System.out.println("Received message: " + new String(msg.getBody()));
    21. }
    22. return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    23. }
    24. });
    25. // Start the consumer
    26. consumer.start();
    27. System.out.println("Consumer started.");
    28. }
    29. }

7.Stop the Containers

After completing the experiment, we can stop the containers as follows:

  1. # Stop the NameServer container
  2. docker stop rmqnamesrv
  3. # Stop the Broker container
  4. docker stop rmqbroker