【原创】《从0开始学RocketMQ》—集群搭建

时间:2022-05-12 02:45:29

用两台服务器,搭建出一个双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 配置文件:

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

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

namesrvAddr=192.168.50.1:;192.168.50.2:
brokerClusterName=rocketMqCluster
brokerIP1=192.168.50.1
brokerName=broker-a
brokerId=
deleteWhen=
fileReservedTime=
mapedFileSizeConsumeQueue=
brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH
listenPort=
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
storePathRootDir=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a
storePathCommitLog=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/commitlog
storePathConsumeQueue=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/consumequeue
storePathIndex=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/index
storeCheckpoint=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/checkpoint
abortFile=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a/abort

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

namesrvAddr=192.168.50.1:;192.168.50.2:
brokerClusterName=rocketMqCluster
brokerIP1=192.168.50.2
brokerName=broker-b
brokerId=
deleteWhen=
fileReservedTime=
mapedFileSizeConsumeQueue=
brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH
listenPort=
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
storePathRootDir=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b
storePathCommitLog=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/commitlog
storePathConsumeQueue=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/consumequeue
storePathIndex=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/index
storeCheckpoint=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/checkpoint
abortFile=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b/abort

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

namesrvAddr=192.168.50.1:;192.168.50.2:
brokerClusterName=rocketMqCluster
brokerIP1=192.168.50.1
brokerName=broker-b
brokerId=
deleteWhen=
fileReservedTime=
mapedFileSizeConsumeQueue=
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
listenPort=
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
storePathRootDir=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s
storePathCommitLog=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s/commitlog
storePathConsumeQueue=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s/consumequeue
storePathIndex=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s/index
storeCheckpoint=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-b-s/checkpoint
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:

namesrvAddr=192.168.50.1:;192.168.50.2:
brokerClusterName=rocketMqCluster
brokerIP1=192.168.50.2
brokerName=broker-a
brokerId=
deleteWhen=
fileReservedTime=
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
listenPort=
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
storePathRootDir=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s
storePathCommitLog=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/commitlog
storePathConsumeQueue=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/consumequeue
storePathIndex=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/index
storeCheckpoint=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/checkpoint
abortFile=/usr/local/rocketmq-all-4.3.-bin-release/data/broker-a-s/abort

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

分别启动四个 Broker:

nohup sh bin/mqbroker -c broker_config_file &

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

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

[root@- local]# git clone https://github.com/apache/rocketmq-externals.git
Cloning into 'rocketmq-externals'...
remote: Enumerating objects: , done.
remote: Counting objects: % (/), done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
Receiving objects: % (/), 11.86 MiB | 232.00 KiB/s, done.
Resolving deltas: % (/), done.
[root@- local]# cd rocketmq-externals/
[root@- rocketmq-externals]# ls
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
[root@- rocketmq-externals]# git branch
* master
[root@- rocketmq-externals]# git fetch origin release-rocketmq-console-1.0.
From https://github.com/apache/rocketmq-externals
* branch release-rocketmq-console-1.0. -> FETCH_HEAD
[root@- rocketmq-externals]# git checkout -b release-1.0. origin/release-rocketmq-console-1.0.
Branch 'release-1.0.0' set up to track remote branch 'release-rocketmq-console-1.0.0' from 'origin'.
Switched to a new branch 'release-1.0.0'
[root@- rocketmq-externals]# ls
README.md rocketmq-console
[root@- rocketmq-externals]# ls rocketmq-console/
doc LICENSE NOTICE pom.xml README.md src style
[root@- rocketmq-externals]# vim rocketmq-console/src/main/resources/application.properties

编辑 application.properties:

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

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

[root@- rocketmq-console]# mv /usr/local/rocketmq-externals/rocketmq-console /usr/local/rocketmq-console
[root@- rocketmq-console]# cd /usr/local/rocketmq-console/
[root@- rocketmq-console]# ls
doc LICENSE NOTICE pom.xml README.md src style
[root@- rocketmq-console]# mvn clean package -Dmaven.test.skip=true
........
[INFO] Building jar: /usr/local/rocketmq-console/target/rocketmq-console-ng-1.0.-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: : min
[INFO] Finished at: --11T17::+:
[INFO] ------------------------------------------------------------------------
[root@- rocketmq-console]# ls
doc LICENSE NOTICE pom.xml README.md src style target
[root@- rocketmq-console]# ls target/
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
[root@- rocketmq-console]# java -jar target/rocketmq-console-ng-1.0..jar
.......
[-- ::15.980] INFO Initializing ProtocolHandler ["http-nio-8080"]
[-- ::15.991] INFO Starting ProtocolHandler [http-nio-]
[-- ::16.232] INFO Using a shared selector for servlet write/read
[-- ::16.251] INFO Tomcat started on port(s): (http)
[-- ::16.257] INFO Started App in 6.594 seconds (JVM running for 7.239)

4. 集群测试

Producer 测试代码:

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

Consumer 测试代码:

public class SyncConsumerTest {
public static void main(String[] args) {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_test_group");
consumer.setNamesrvAddr("39.107.153.215:9876;39.107.157.89:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
try {
consumer.subscribe("topic_test", "*");
consumer.registerMessageListener((MessageListenerConcurrently) (messageList, context) -> {
System.out.println(Thread.currentThread().getName() + " Receive New Message:" + messageList);
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
consumer.start();
}catch (Exception e){
e.printStackTrace();
}
}
}

SyncProducerTest 运行日志:

【原创】《从0开始学RocketMQ》—集群搭建

SyncConsumerTest 运行日志:

【原创】《从0开始学RocketMQ》—集群搭建

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

【原创】《从0开始学RocketMQ》—集群搭建

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

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

  1. RocketMQ集群搭建&lpar;3m-3s-async&rpar;

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

  2. Hadoop 3&period;1&period;2&lpar;HA&rpar;&plus;Zookeeper3&period;4&period;13&plus;Hbase1&period;4&period;9&lpar;HA&rpar;&plus;Hive2&period;3&period;4&plus;Spark2&period;4&period;0&lpar;HA&rpar;高可用集群搭建

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

  3. redis 5&period;0&period;3 讲解、集群搭建

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

  4. RocketMQ集群搭建

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

  5. hadoop2&period;2&period;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&period;4&period;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. &period;NET 提升教育 第一期:VIP 付费课程培训通知!

    为响应 @当年在远方 同学的建议,在年前尝试进行一次付费的VIP培训. 培训的课件:点击下载培训周期:10个课程左右,每晚1个半小时培训价格:1000元/人.报名方式:有意向的请加QQ群:路过秋天.N ...

  2. Java高阶面试问题合集

    下面总结一下在Java面试中常用的一些问题,不具体解答,我只附上一些精彩的博文链接. Spring IOC AOP 底层原理 JAVA的反射机制和动态代理 Java反射机制和动态代理 多线程 Spri ...

  3. C&num;&lowbar;会员管理系统:开发五(用户注册)

    创建一个新的用户注册窗体(VIPRegistration.cs): 用户注册窗体(VIPRegistration.cs)详细代码如下: using System; using System.Colle ...

  4. 一种解决Code Map与ReSharper冲突导致Visual Studio 2013 Peek Definition失效的办法

    更正 3月6号: 经再次测试,单独使用Code Map或ReSharper,对Visual Studio的Peek Definition无影响,但是两者一起使用时,Peek Definition失效, ...

  5. bzoj2301&colon; &lbrack;HAOI2011&rsqb;Problem b懵逼乌斯反演

    属于结果的和好求但是结果不好求的题 (轻易能得到以k的倍数为最大公约数的对数,但是不好直接求k) 所以一波反演结束 其实反演的时候完全没有反演的感觉,就是不停地恒等变形 算是懵逼乌斯反演最简单的例题 ...

  6. 《Java2 实用教程(第五版)》学习指导

    <Java2 实用教程(第五版)> 第1章Java入门 主要内容:P1 1.1Java的地位:P1 1.2Java的特点:P2 1.3安装JDK:P5 1.4Java程序的开发步骤:P8 ...

  7. Web项目中的 &sol;

    如果 /  出现在路径的前面: web.xml中:http://loclalhost:8080/项目名称/      在项目的根路径下面 jsp中: http://localhost:8080/    ...

  8. 上传input中file文件到云端,并返回链接

    有的文件.图片等信息可以上传到云端上,然后使用链接调用,这样会更加的方便和快捷. <form id="form"> <input type="file& ...

  9. Linux 重启nginx

    重启 1.验证nginx配置文件是否正确 方法一:进入nginx安装目录sbin下,输入命令./nginx -t 看到如下显示nginx.conf syntax is ok nginx.conf te ...

  10. 小波学习之二(单层一维离散小波变换DWT的Mallat算法C&plus;&plus;实现优化)--转载

    小波学习之二(单层一维离散小波变换DWT的Mallat算法C++实现优化)   在上回<小波学习之一>中,已经详细介绍了Mallat算法C++实现,效果还可以,但也存在一些问题,比如,代码 ...