用两台服务器,搭建出一个双master双slave、无单点故障的高可用 RocketMQ 集群。此处假设两台服务器的物理 IP 分别为:192.168.50.1、192.168.50.2。

内容目录

1. 启动 NameServer 集群

2. 启动 Broker 集群

3. RocketMQ 可视化管理控制台:rocketmq-console

4. 集群测试

1. 启动 NameServer 集群

在两台服务器上分别启动 NameServer,可以得到一个无单点故障的 NameServer 服务,服务地址分别为:192.168.50.1:9876、192.168.50.2:9876。

2. 启动 Broker 集群

修改 Broker 配置文件,以使每台服务器上都可以启动一个 Master 角色 的 Broker 和 一个Slave 角色的 Broker。
首先找到 Broker 配置文件,此处我们搭建一个同步双写模式的集群,所以需要修改 2m-2s-sync 目录下的 broker 配置文件:

  1. [root@- ~]# cd /usr/local/rocketmq-all-4.3.-bin-release/conf/
  2. [root@- conf]# ls
  3. 2m-2s-async 2m-2s-sync 2m-noslave broker.conf logback_broker.xml logback_namesrv.xml logback_tools.xml
  4. [root@- conf]# cd 2m-2s-sync/
  5. [root@- 2m-2s-sync]# ls
  6. broker-a.properties broker-a-s.properties broker-b.properties broker-b-s.properties

1) 修改 192.168.50.1 服务器上的 broker-a.properties 为 Master 角色的 Broker:

  1. namesrvAddr=192.168.50.1:;192.168.50.2:
  2. brokerClusterName=rocketMqCluster
  3. brokerIP1=192.168.50.1
  4. brokerName=broker-a
  5. brokerId=
  6. deleteWhen=
  7. fileReservedTime=
  8. mapedFileSizeConsumeQueue=
  9. brokerRole=SYNC_MASTER
  10. flushDiskType=ASYNC_FLUSH
  11. listenPort=
  12. autoCreateTopicEnable=true
  13. autoCreateSubscriptionGroup=true
  14. storePathRootDir=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a
  15. storePathCommitLog=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/commitlog
  16. storePathConsumeQueue=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/consumequeue
  17. storePathIndex=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/index
  18. storeCheckpoint=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/checkpoint
  19. abortFile=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/abort

2) 修改 192.168.50.2 服务器上的 broker-b.properties 为 Master 角色的 Broker:

  1. namesrvAddr=192.168.50.1:;192.168.50.2:
  2. brokerClusterName=rocketMqCluster
  3. brokerIP1=192.168.50.2
  4. brokerName=broker-b
  5. brokerId=
  6. deleteWhen=
  7. fileReservedTime=
  8. mapedFileSizeConsumeQueue=
  9. brokerRole=SYNC_MASTER
  10. flushDiskType=ASYNC_FLUSH
  11. listenPort=
  12. autoCreateTopicEnable=true
  13. autoCreateSubscriptionGroup=true
  14. storePathRootDir=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b
  15. storePathCommitLog=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/commitlog
  16. storePathConsumeQueue=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/consumequeue
  17. storePathIndex=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/index
  18. storeCheckpoint=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/checkpoint
  19. abortFile=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/abort

3) 修改 192.168.50.1 服务器上的 broker-b-s.properties 为 Slave 角色的 Broker:

  1. namesrvAddr=192.168.50.1:;192.168.50.2:
  2. brokerClusterName=rocketMqCluster
  3. brokerIP1=192.168.50.1
  4. brokerName=broker-b
  5. brokerId=
  6. deleteWhen=
  7. fileReservedTime=
  8. mapedFileSizeConsumeQueue=
  9. brokerRole=SLAVE
  10. flushDiskType=ASYNC_FLUSH
  11. listenPort=
  12. autoCreateTopicEnable=true
  13. autoCreateSubscriptionGroup=true
  14. storePathRootDir=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s
  15. storePathCommitLog=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s/commitlog
  16. storePathConsumeQueue=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s/consumequeue
  17. storePathIndex=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s/index
  18. storeCheckpoint=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s/checkpoint
  19. abortFile=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s/abort

4) 修改 192.168.50.2 服务器上的 broker-a-s.properties 为 Slave 角色的 Broker:

  1. namesrvAddr=192.168.50.1:;192.168.50.2:
  2. brokerClusterName=rocketMqCluster
  3. brokerIP1=192.168.50.2
  4. brokerName=broker-a
  5. brokerId=
  6. deleteWhen=
  7. fileReservedTime=
  8. brokerRole=SLAVE
  9. flushDiskType=ASYNC_FLUSH
  10. listenPort=
  11. autoCreateTopicEnable=true
  12. autoCreateSubscriptionGroup=true
  13. storePathRootDir=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s
  14. storePathCommitLog=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/commitlog
  15. storePathConsumeQueue=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/consumequeue
  16. storePathIndex=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/index
  17. storeCheckpoint=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/checkpoint
  18. abortFile=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/abort

一台服务器上启动多个Broker 时,需指定不同的端口号,记得防火墙放开 NameServer 和 Broker 中用到的端口号哦~

分别启动四个 Broker:

  1. nohup sh bin/mqbroker -c broker_config_file &

3. RocketMQ 可视化管理控制台:rocketmq-console

在服务器 192.168.50.1 上安装即可,无需集群

  1. [root@- local]# git clone https://github.com/apache/rocketmq-externals.git
  2. Cloning into 'rocketmq-externals'...
  3. remote: Enumerating objects: , done.
  4. remote: Counting objects: % (/), done.
  5. remote: Compressing objects: % (/), done.
  6. remote: Total (delta ), reused (delta ), pack-reused
  7. Receiving objects: % (/), 11.86 MiB | 232.00 KiB/s, done.
  8. Resolving deltas: % (/), done.
  9. [root@- local]# cd rocketmq-externals/
  10. [root@- rocketmq-externals]# ls
  11. dev README.md rocketmq-console rocketmq-docker rocketmq-flink rocketmq-flume rocketmq-hbase rocketmq-iot-bridge rocketmq-jms rocketmq-mysql rocketmq-php rocketmq-redis rocketmq-sentinel rocketmq-serializer rocketmq-spark
  12. [root@- rocketmq-externals]# git branch
  13. * master
  14. [root@- rocketmq-externals]# git fetch origin release-rocketmq-console-1.0.
  15. From https://github.com/apache/rocketmq-externals
  16. * branch release-rocketmq-console-1.0. -> FETCH_HEAD
  17. [root@- rocketmq-externals]# git checkout -b release-1.0. origin/release-rocketmq-console-1.0.
  18. Branch 'release-1.0.0' set up to track remote branch 'release-rocketmq-console-1.0.0' from 'origin'.
  19. Switched to a new branch 'release-1.0.0'
  20. [root@- rocketmq-externals]# ls
  21. README.md rocketmq-console
  22. [root@- rocketmq-externals]# ls rocketmq-console/
  23. doc LICENSE NOTICE pom.xml README.md src style
  24. [root@- rocketmq-externals]# vim rocketmq-console/src/main/resources/application.properties

编辑 application.properties:

  1. server.contextPath=/rocketmq
  2. server.port=
  3. #spring.application.index=true
  4. spring.application.name=rocketmq-console
  5. spring.http.encoding.charset=UTF-
  6. spring.http.encoding.enabled=true
  7. spring.http.encoding.force=true
  8. logging.config=classpath:logback.xml
  9. #if this value is empty,use env value rocketmq.config.namesrvAddr NAMESRV_ADDR | now, you can set it in ops page.default localhost:9876
  10. rocketmq.config.namesrvAddr=192.168.50.1:;192.168.50.2:
  11. #if you use rocketmq version < 3.5.8, rocketmq.config.isVIPChannel should be false.default true
  12. rocketmq.config.isVIPChannel=
  13. #rocketmq-console's data path:dashboard/monitor
  14. rocketmq.config.dataPath=/tmp/rocketmq-console/data
  15. #set it false if you don't want use dashboard.default true
  16. rocketmq.config.enableDashBoardCollect=true

移动 rocketmq-console 所在目录,编译并启动 rocketmq-console:

  1. [root@- rocketmq-console]# mv /usr/local/rocketmq-externals/rocketmq-console /usr/local/rocketmq-console
  2. [root@- rocketmq-console]# cd /usr/local/rocketmq-console/
  3. [root@- rocketmq-console]# ls
  4. doc LICENSE NOTICE pom.xml README.md src style
  5. [root@- rocketmq-console]# mvn clean package -Dmaven.test.skip=true
  6. ........
  7. [INFO] Building jar: /usr/local/rocketmq-console/target/rocketmq-console-ng-1.0.-sources.jar
  8. [INFO] ------------------------------------------------------------------------
  9. [INFO] BUILD SUCCESS
  10. [INFO] ------------------------------------------------------------------------
  11. [INFO] Total time: : min
  12. [INFO] Finished at: --11T17::+:
  13. [INFO] ------------------------------------------------------------------------
  14. [root@- rocketmq-console]# ls
  15. doc LICENSE NOTICE pom.xml README.md src style target
  16. [root@- rocketmq-console]# ls target/
  17. checkstyle-cachefile checkstyle-checker.xml checkstyle-result.xml classes generated-sources maven-archiver maven-status rocketmq-console-ng-1.0..jar rocketmq-console-ng-1.0..jar.original rocketmq-console-ng-1.0.-sources.jar
  18. [root@- rocketmq-console]# java -jar target/rocketmq-console-ng-1.0..jar
  19. .......
  20. [-- ::15.980] INFO Initializing ProtocolHandler ["http-nio-8080"]
  21. [-- ::15.991] INFO Starting ProtocolHandler [http-nio-]
  22. [-- ::16.232] INFO Using a shared selector for servlet write/read
  23. [-- ::16.251] INFO Tomcat started on port(s): (http)
  24. [-- ::16.257] INFO Started App in 6.594 seconds (JVM running for 7.239)

4. 集群测试

Producer 测试代码:

  1. public class SyncProducerTest {
  2. public static void main(String[] args) {
  3. DefaultMQProducer producer = new DefaultMQProducer("producer_test_group");
  4. producer.setNamesrvAddr("39.107.153.215:9876;39.107.157.89:9876");
  5. try{
  6. producer.start();
  7. for(int i=0;i<100;i++){
  8. Message message = new Message("topic_test", "tag_test", ("Hello World" + 1).getBytes("UTF-8"));
  9. SendResult sendResult = producer.send(message);
  10. System.out.println(JSON.toJSON(sendResult));
  11. }
  12. producer.shutdown();
  13. }catch (Exception e){
  14. e.printStackTrace();
  15. }
  16. }
  17. }

Consumer 测试代码:

  1. public class SyncConsumerTest {
  2. public static void main(String[] args) {
  3. DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_test_group");
  4. consumer.setNamesrvAddr("39.107.153.215:9876;39.107.157.89:9876");
  5. consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
  6. try {
  7. consumer.subscribe("topic_test", "*");
  8. consumer.registerMessageListener((MessageListenerConcurrently) (messageList, context) -> {
  9. System.out.println(Thread.currentThread().getName() + " Receive New Message:" + messageList);
  10. return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
  11. });
  12. consumer.start();
  13. }catch (Exception e){
  14. e.printStackTrace();
  15. }
  16. }
  17. }

SyncProducerTest 运行日志:

SyncConsumerTest 运行日志:

通过日志可以看到,消费者、生产者收发消息都是正常的,我们去可视化管理控制台查看下 http://192.168.50.1:8080/rocketmq:

通过管控台可以看到,双 master 双 slave 的 broker 集群一切正常,并可进一步看到每个 broker 处理消息的情况。

【原创】《从0开始学RocketMQ》—集群搭建的更多相关文章

  1. RocketMQ集群搭建(3m-3s-async)

    RocketMQ集群搭建(3m-3s-async) 各角色介绍 角色 作用 Producer 消息发送者,将消息发送到 Broker.无状态,其与NameServer集群中的一个节点建立长连接,定期从 ...

  2. Hadoop 3.1.2(HA)+Zookeeper3.4.13+Hbase1.4.9(HA)+Hive2.3.4+Spark2.4.0(HA)高可用集群搭建

    目录 目录 1.前言 1.1.什么是 Hadoop? 1.1.1.什么是 YARN? 1.2.什么是 Zookeeper? 1.3.什么是 Hbase? 1.4.什么是 Hive 1.5.什么是 Sp ...

  3. redis 5.0.3 讲解、集群搭建

    REDIS 一 .redis 介绍 不管你是从事Python.Java.Go.PHP.Ruby等等... Redis都应该是一个比较熟悉的中间件.而大部分经常写业务代码的程序员,实际工作中或许只用到了 ...

  4. RocketMQ集群搭建

    1.RocketMQ介绍 1.1. 简介 RocketMQ 是一款分布式.队列模型的消息中间件,具有以下特点: 能够保证严格的消息顺序 提供丰富的消息拉取模式 高效的订阅者水平扩展能力 实时的消息订阅 ...

  5. hadoop2.2.0的ha分布式集群搭建

    hadoop2.2.0 ha集群搭建 使用的文件如下:    jdk-6u45-linux-x64.bin    hadoop-2.2.0.x86_64.tar    zookeeper-3.4.5. ...

  6. mongodb之 3.4.0 mongodb sharing 副本集群搭建

    系统系统 centos6.5三台服务器:10.100.25.42/43/44安装包: mongodb-linux-x86_64-rhel62-3.4.0.tgz 服务器规划:mongos mongos ...

  7. 4 种高可用 RocketMQ 集群搭建方案!

    背景 笔者所在的业务线,最初化分为三个服务,由于业务初期业务复杂度相对简单,三个业务服务都能很好的独立完成业务功能. 随着产品迭代,业务功能越来越多后慢慢也要面对高并发.业务解耦.分布式事务等问题,所 ...

  8. RocketMQ集群搭建方式

    各角色介绍 Producer:消息的发送者:举例:发信者 Consumer:消息接收者:举例:收信者 Broker:暂存和传输消息:举例:邮局 NameServer:管理Broker:举例:各个邮局的 ...

  9. RocketMq 集群搭建 部署

    原文链接:https://blog.csdn.net/weixin_40533111/article/details/84451219 作者小太阳^_^,转载请注明出处,谢谢 前言本文基于最新版roc ...

  10. RocketMQ 集群搭建--双Master方案

    安装环境 jdk1.7 alibaba-rocketmq-3.2.6.tar.gz VM虚拟机redhat6.5-x64:192.168.1.201  192.168.1.202 Xshell4 部署 ...

随机推荐

  1. Eclipse Explorer插件快速打开项目文件在系统资源管理器

    官网:https://github.com/Jamling/eclipse-explorer 安装: 1.在线安装 [Help]->[Eclipse Marketplace]搜索:Eclipse ...

  2. java中可以通过调用ping命令来判断网络是否连接正常

    原文:http://www.open-open.com/code/view/1446382328960 import java.io.BufferedReader; import java.io.IO ...

  3. POJ3262 Protecting the Flowers 【贪心】

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4418   Accepted: ...

  4. android动画具体解释六 XML中定义动画

    动画View 属性动画系统同意动画View对象并提供非常多比view动画系统更高级的功能.view动画系统通过改变绘制方式来变换View对象,view动画是被view的容器所处理的,由于View本身没 ...

  5. 【剑指offer】数组中仅仅出现一次的数字(1)

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/27649027 题目描写叙述: 一个整型数组里除了两个数字之外.其它的数字都出现了两次. 请 ...

  6. Fix "Unable to lock the administration directory (/var/lib/dpkg/)" in Ubuntu

    While using the apt-get command or the relatively new APT package management tool in Ubuntu Linux or ...

  7. 目标跟踪之高斯混合模型---cv实现

    #include <stdio.h>#include <cv.h>#include <cxcore.h>#include <highgui.h>#inc ...

  8. ios archives 出现的是other items而不是iOS Apps的解决方案

    ios archives 出现的是other items而不是iOS Apps的解决方案 项目打包时出现的是不是出现在iOS Apps栏目下面,而是Other Items而且右边对应的Upload t ...

  9. Linq To Entities中的动态排序

    换了工作有一个月了,一样的工作.一样的代码.一样的体力活仍就…… Linq To Entityes 也是不新玩意了,近半年来也一直与之打交道,但一直也没对其深究过.今天新加的功能要对所有列支持排序,这 ...

  10. ASP.NET MVC中为DropDownListFor设置选中项的方法

    在MVC中,当涉及到强类型编辑页,如果有select元素,需要根据当前Model的某个属性值,让Select的某项选中.本篇只整理思路,不涉及完整代码. □ 思路 往前台视图传的类型是List< ...