Run RocketMQ with Docker Compose

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

Run RocketMQ with Docker Compose - 图1SYSTEM REQUIREMENTS

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

1.Configure broker.conf

  1. # Configure the broker's IP address
  2. echo "brokerIP1=127.0.0.1" > broker.conf

2.Configure docker-compose

To quickly start and run the RockerMQ cluster, you can use the following template to create a docker-compose.yml file by modifying or adding configurations in the environment section.

  1. version: '3.8'
  2. services:
  3. namesrv:
  4. image: apache/rocketmq:4.9.6
  5. container_name: rmqnamesrv
  6. ports:
  7. - 9876:9876
  8. networks:
  9. - rocketmq
  10. command: sh mqnamesrv
  11. broker:
  12. image: apache/rocketmq:4.9.6
  13. container_name: rmqbroker
  14. ports:
  15. - 10909:10909
  16. - 10911:10911
  17. - 10912:10912
  18. environment:
  19. - NAMESRV_ADDR=rmqnamesrv:9876
  20. volumes:
  21. - ./broker.conf:/home/rocketmq/rocketmq-4.9.6/conf/broker.conf
  22. depends_on:
  23. - namesrv
  24. networks:
  25. - rocketmq
  26. command: sh mqbroker -c /home/rocketmq/rocketmq-4.9.6/conf/broker.conf
  27. networks:
  28. rocketmq:
  29. driver: bridge

3.Start RocketMQ

Start all defined services according to the docker-compose.yml file.

  • Linux
  • Windows
  1. docker-compose up -d
  1. docker-compose -p rocketmq_project up -d

4.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...

5.Send and Receive Messages with SDK

We can also try to use the client sdk to send and receive messages.

  1. Create a java project.

  2. Add sdk dependency to pom.xml

    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. }

6.Stop All Services

  1. docker-compose down