RocketMQ使用Docker Compose部署及使用

简介: RocketMQ使用Docker Compose部署及使用

docker-compose.yml

注意:启动 RocketMQ Server + Broker + Console 至少需要 2G 内存

version: '3.5'
services:
  rmqnamesrv:
    image: rocketmqinc/rocketmq
    container_name: rmqnamesrv
    restart: always
    ports:
      - 9876:9876
    environment:
    #内存分配
      JAVA_OPT_EXT: "-server -Xms1g -Xmx1g"
    volumes:
      - ./logs:/root/logs
    command: sh mqnamesrv
    networks:
      rmq:
        aliases:
          - rmqnamesrv
  rmqbroker:
    image: rocketmqinc/rocketmq
    container_name: rmqbroker
    restart: always
    depends_on:
      - rmqnamesrv
    ports:
      - 10909:10909
      - 10911:10911
    volumes:
      - ./logs:/root/logs
      - ./store:/root/store
      - ./conf/broker.conf:/opt/rocketmq-4.4.0/conf/broker.conf
    command: sh mqbroker  -c /opt/rocketmq-4.4.0/conf/broker.conf
    environment:
      NAMESRV_ADDR: "rmqnamesrv:9876"
      JAVA_OPT_EXT: "-server -Xms1g -Xmx1g -Xmn1g"
    networks:
      rmq:
        aliases:
          - rmqbroker
  rmqconsole:
    image: styletang/rocketmq-console-ng
    container_name: rocketmq-console
    restart: always
    ports:
      - 8080:8080
    depends_on:
      - rmqnamesrv
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /home/rocketmq/console/logs:/root/logs
    environment:
      JAVA_OPTS: "-Drocketmq.namesrv.addr=rmqnamesrv:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false"
    networks:
      rmq:
        aliases:
          - rmqconsole
networks:
  rmq:
    name: rmq
    driver: bridge

image.gif

broker.conf

RocketMQ Broker 需要一个配置文件,按照上面的 Compose 配置,我们需要在 ./conf 目录下创建一个名为 broker.conf 的配置文件,内容如下:

brokerName = broker-a  
brokerId = 0  
deleteWhen = 04  
fileReservedTime = 48  
brokerRole = ASYNC_MASTER  
flushDiskType = ASYNC_FLUSH  
# 如果是本地程序调用云主机 mq,这个需要设置成 云主机 IP
brokerIP1=192.168.72.78

image.gif

启动rocketmq

在编写的docker-compose.yml下执行

docker-compose up -d

image.gif

Maven 依赖

<dependency>
        <groupId>org.apache.rocketmq</groupId>
        <artifactId>rocketmq-client</artifactId>
        <version>4.3.0</version>
    </dependency>

image.gif

Gradle

compile 'org.apache.rocketmq:rocketmq-client:4.3.0'

image.gif

同步消费者

public class SyncProducer {
    public static void main(String[] args) throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new
            DefaultMQProducer("please_rename_unique_group_name");
        // Specify name server addresses.
        producer.setNamesrvAddr("localhost:9876");
        //Launch the instance.
        producer.start();
        for (int i = 0; i < 100; i++) {
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("TopicTest" /* Topic */,
                "TagA" /* Tag */,
                ("Hello RocketMQ " +
                    i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
            //Call send message to deliver message to one of brokers.
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }
        //Shut down once the producer instance is not longer in use.
        producer.shutdown();
    }
}

image.gif

异步生产者

public class AsyncProducer {
    public static void main(String[] args) throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        // Specify name server addresses.
        producer.setNamesrvAddr("localhost:9876");
        //Launch the instance.
        producer.start();
        producer.setRetryTimesWhenSendAsyncFailed(0);
        int messageCount = 100;
        final CountDownLatch countDownLatch = new CountDownLatch(messageCount);
        for (int i = 0; i < messageCount; i++) {
            try {
                final int index = i;
                Message msg = new Message("Jodie_topic_1023",
                    "TagA",
                    "OrderID188",
                    "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
                producer.send(msg, new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        countDownLatch.countDown();
                        System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
                    }
                    @Override
                    public void onException(Throwable e) {
                        countDownLatch.countDown();
                        System.out.printf("%-10d Exception %s %n", index, e);
                        e.printStackTrace();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        countDownLatch.await(5, TimeUnit.SECONDS);
        producer.shutdown();
    }
}

image.gif

单向生产者

public class OnewayProducer {
    public static void main(String[] args) throws Exception{
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        // Specify name server addresses.
        producer.setNamesrvAddr("localhost:9876");
        //Launch the instance.
        producer.start();
        for (int i = 0; i < 100; i++) {
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("TopicTest" /* Topic */,
                "TagA" /* Tag */,
                ("Hello RocketMQ " +
                    i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
            //Call send message to deliver message to one of brokers.
            producer.sendOneway(msg);
        }
        //Wait for sending to complete
        Thread.sleep(5000);        
        producer.shutdown();
    }
}

image.gif

消费者

public class Consumer {
    public static void main(String[] args) throws InterruptedException, MQClientException {
        // Instantiate with specified consumer group name.
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");
        // Specify name server addresses.
        consumer.setNamesrvAddr("localhost:9876");
        // Subscribe one more more topics to consume.
        consumer.subscribe("TopicTest", "*");
        // Register callback to execute on arrival of messages fetched from brokers.
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                ConsumeConcurrentlyContext context) {
                System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //Launch the consumer instance.
        consumer.start();
        System.out.printf("Consumer Started.%n");
    }
}

image.gif

订阅模式

    • 生产者:
    public class OrderedProducer {
        public static void main(String[] args) throws Exception {
            //Instantiate with a producer group name.
            MQProducer producer = new DefaultMQProducer("example_group_name");
            //Launch the instance.
            producer.start();
            String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
            for (int i = 0; i < 100; i++) {
                int orderId = i % 10;
                //Create a message instance, specifying topic, tag and message body.
                Message msg = new Message("TopicTestjjj", tags[i % tags.length], "KEY" + i,
                        ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
                SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                    Integer id = (Integer) arg;
                    int index = id % mqs.size();
                    return mqs.get(index);
                }
                }, orderId);
                System.out.printf("%s%n", sendResult);
            }
            //server shutdown
            producer.shutdown();
        }
    }

    image.gif

    • 消费者:
    public class OrderedConsumer {
        public static void main(String[] args) throws Exception {
            DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");
            consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
            consumer.subscribe("TopicTest", "TagA || TagC || TagD");
            consumer.registerMessageListener(new MessageListenerOrderly() {
                AtomicLong consumeTimes = new AtomicLong(0);
                @Override
                public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs,
                                                           ConsumeOrderlyContext context) {
                    context.setAutoCommit(false);
                    System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
                    this.consumeTimes.incrementAndGet();
                    if ((this.consumeTimes.get() % 2) == 0) {
                        return ConsumeOrderlyStatus.SUCCESS;
                    } else if ((this.consumeTimes.get() % 3) == 0) {
                        return ConsumeOrderlyStatus.ROLLBACK;
                    } else if ((this.consumeTimes.get() % 4) == 0) {
                        return ConsumeOrderlyStatus.COMMIT;
                    } else if ((this.consumeTimes.get() % 5) == 0) {
                        context.setSuspendCurrentQueueTimeMillis(3000);
                        return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
                    }
                    return ConsumeOrderlyStatus.SUCCESS;
                }
            });
            consumer.start();
            System.out.printf("Consumer Started.%n");
        }
    }

    image.gif

    广播

      • 生产者:
      public class BroadcastProducer {
          public static void main(String[] args) throws Exception {
              DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
              producer.start();
              for (int i = 0; i < 100; i++){
                  Message msg = new Message("TopicTest",
                      "TagA",
                      "OrderID188",
                      "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
                  SendResult sendResult = producer.send(msg);
                  System.out.printf("%s%n", sendResult);
              }
              producer.shutdown();
          }
      }

      image.gif

      • 消费者:
      public class BroadcastConsumer {
          public static void main(String[] args) throws Exception {
              DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("example_group_name");
              consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
              //set to broadcast mode
              consumer.setMessageModel(MessageModel.BROADCASTING);
              consumer.subscribe("TopicTest", "TagA || TagC || TagD");
              consumer.registerMessageListener(new MessageListenerConcurrently() {
                  @Override
                  public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
                      ConsumeConcurrentlyContext context) {
                      System.out.printf(Thread.currentThread().getName() + " Receive New Messages: " + msgs + "%n");
                      return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                  }
              });
              consumer.start();
              System.out.printf("Broadcast Consumer Started.%n");
          }
      }

      image.gif

      SpringBoot整合RocketMQ

        • maven:
        <dependency>
                    <groupId>org.apache.rocketmq</groupId>
                    <artifactId>rocketmq-spring-boot-starter</artifactId>
                </dependency>


        • controll:
        @RestController
        public class ProducerController {
            @Resource
            private RocketMQTemplate rocketMQTemplate;
            @GetMapping("/send")
            public String send(String msg) {
                for (int i = 1; i < 100; i++) {
                    rocketMQTemplate.convertAndSend("test-topic888",msg+i);
                }
                return "success";
            }
        }


        相关实践学习
        消息队列RocketMQ版:基础消息收发功能体验
        本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
        消息队列 MNS 入门课程
        1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
        相关文章
        |
        14天前
        |
        关系型数据库 MySQL API
        |
        9天前
        |
        人工智能 API 数据安全/隐私保护
        使用 Docker 一键免费部署 63.8k 的私人 ChatGPT 网页应用
        NextChat 是一个可以在 GitHub 上一键免费部署的私人 ChatGPT 网页应用,支持 GPT3、GPT4 和 Gemini Pro 模型。该项目在 GitHub 上获得了 63.8k 的 star 数。部署简单,只需拉取 Docker 镜像并运行容器,设置 API Key 后即可使用。此外,NextChat 还提供了预设角色的面具功能,方便用户快速创建对话。
        60 22
        使用 Docker 一键免费部署 63.8k 的私人 ChatGPT 网页应用
        |
        20天前
        |
        SQL 关系型数据库 数据库
        国产数据实战之docker部署MyWebSQL数据库管理工具
        【10月更文挑战第23天】国产数据实战之docker部署MyWebSQL数据库管理工具
        60 4
        国产数据实战之docker部署MyWebSQL数据库管理工具
        |
        10天前
        |
        运维 开发者 Docker
        Docker Compose:简化容器化应用的部署与管理
        Docker Compose:简化容器化应用的部署与管理
        |
        10天前
        |
        Docker 微服务 容器
        使用Docker Compose实现微服务架构的快速部署
        使用Docker Compose实现微服务架构的快速部署
        23 1
        |
        22天前
        |
        负载均衡 监控 开发者
        深入浅出:掌握 Docker Compose 的高级用法
        【10月更文挑战第22天】本文深入探讨了 Docker Compose 的高级用法,包括环境变量、服务扩展、网络配置和数据卷管理。通过实例详细介绍了如何利用这些功能提升开发效率和应用部署的灵活性。适合希望深入了解 Docker Compose 的开发者阅读。
        |
        22天前
        |
        消息中间件 Linux RocketMQ
        在Red Hat Enterprise Linux 9上使用Docker快速安装并部署
        通过以上步骤,你可以在Red Hat Enterprise Linux 9上使用Docker快速安装并部署RocketMQ。这种方法不仅简化了安装过程,还提供了一个灵活的环境来管理和扩展消息队列系统。RocketMQ作为一款高性能的分布式消息系统,通过Docker可以实现快速部署和高效管理。
        53 2
        |
        10天前
        |
        NoSQL Redis Docker
        【赵渝强老师】使用Docker Compose管理容器
        Docker Compose 通过 YAML 文件管理多个容器,简化复杂系统的部署和管理。本文介绍了 Docker Compose 的基本概念,并通过一个包含 Redis DB 和 Python Web 模块的示例,展示了如何使用 Docker Compose 部署和管理多容器应用。手动部署和 Docker Compose 部署的对比突显了 Docker Compose 在系统复杂度增加时的优势。
        |
        10天前
        |
        前端开发 开发者 Docker
        深入探索Docker Compose:简化多容器应用的部署
        深入探索Docker Compose:简化多容器应用的部署
        37 0
        |
        26天前
        |
        消息中间件 JSON Java
        开发者如何使用轻量消息队列MNS
        【10月更文挑战第19天】开发者如何使用轻量消息队列MNS
        66 4