热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:原创《从0开始学RocketMQ》—集群搭建

本文由编程笔记#小编为大家整理,主要介绍了原创《从0开始学RocketMQ》—集群搭建相关的知识,希望对你有一定的参考价值。用两台服务器,搭建出一个双master双slave、
本文由编程笔记#小编为大家整理,主要介绍了原创《从0开始学RocketMQ》—集群搭建相关的知识,希望对你有一定的参考价值。


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


[[email protected]157-89 ~]# cd /usr/local/rocketmq-all-4.3.2-bin-release/conf/
[[email protected]
157-89 conf]# ls
2m
-2s-async 2m-2s-sync 2m-noslave broker.conf logback_broker.xml logback_namesrv.xml logback_tools.xml
[[email protected]
157-89 conf]# cd 2m-2s-sync/
[[email protected]
157-89 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:9876;192.168.50.2:9876
brokerClusterName
=rocketMqCluster
brokerIP1
=192.168.50.1
brokerName
=broker-a
brokerId
=0
deleteWhen
=04
fileReservedTime
=120
mapedFileSizeConsumeQueue
=500000
brokerRole
=SYNC_MASTER
flushDiskType
=ASYNC_FLUSH
listenPort
=10911
autoCreateTopicEnable
=true
autoCreateSubscriptionGroup
=true
storePathRootDir
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a
storePathCommitLog
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a/commitlog
storePathConsumeQueue
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a/consumequeue
storePathIndex
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a/index
storeCheckpoint
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a/checkpoint
abortFile
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a/abort

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


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

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


namesrvAddr=192.168.50.1:9876;192.168.50.2:9876
brokerClusterName
=rocketMqCluster
brokerIP1
=192.168.50.1
brokerName
=broker-b
brokerId
=1
deleteWhen
=04
fileReservedTime
=120
mapedFileSizeConsumeQueue
=500000
brokerRole
=SLAVE
flushDiskType
=ASYNC_FLUSH
listenPort
=10921
autoCreateTopicEnable
=true
autoCreateSubscriptionGroup
=true
storePathRootDir
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-b-s
storePathCommitLog
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-b-s/commitlog
storePathConsumeQueue
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-b-s/consumequeue
storePathIndex
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-b-s/index
storeCheckpoint
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-b-s/checkpoint
abortFile
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-b-s/abort

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


namesrvAddr=192.168.50.1:9876;192.168.50.2:9876
brokerClusterName
=rocketMqCluster
brokerIP1
=192.168.50.2
brokerName
=broker-a
brokerId
=1
deleteWhen
=04
fileReservedTime
=48
brokerRole
=SLAVE
flushDiskType
=ASYNC_FLUSH
listenPort
=10921
autoCreateTopicEnable
=true
autoCreateSubscriptionGroup
=true
storePathRootDir
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a-s
storePathCommitLog
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a-s/commitlog
storePathConsumeQueue
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a-s/consumequeue
storePathIndex
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a-s/index
storeCheckpoint
=/usr/local/rocketmq-all-4.3.2-bin-release/data/broker-a-s/checkpoint
abortFile
=/usr/local/rocketmq-all-4.3.2-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 上安装即可,无需集群


[[email protected]153-215 local]# git clone https://github.com/apache/rocketmq-externals.git
Cloning into rocketmq-externals...
remote: Enumerating objects:
10, done.
remote: Counting objects:
100% (10/10), done.
remote: Compressing objects:
100% (10/10), done.
remote: Total
9425 (delta 2), reused 1 (delta 0), pack-reused 9415
Receiving objects:
100% (9425/9425), 11.86 MiB | 232.00 KiB/s, done.
Resolving deltas:
100% (4235/4235), done.
[[email protected]
153-215 local]# cd rocketmq-externals/
[[email protected]
153-215 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
[[email protected]
153-215 rocketmq-externals]# git branch
* master
[[email protected]
153-215 rocketmq-externals]# git fetch origin release-rocketmq-console-1.0.0
From https:
//github.com/apache/rocketmq-externals
* branch release-rocketmq-console-1.0.0 -> FETCH_HEAD
[[email protected]
153-215 rocketmq-externals]# git checkout -b release-1.0.0 origin/release-rocketmq-console-1.0.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
[[email protected]
153-215 rocketmq-externals]# ls
README.md rocketmq
-console
[[email protected]
153-215 rocketmq-externals]# ls rocketmq-console/
doc LICENSE NOTICE pom.xml README.md src style
[[email protected]
153-215 rocketmq-externals]# vim rocketmq-console/src/main/resources/application.properties

编辑 application.properties:


server.cOntextPath=/rocketmq
server.port
=8080
#spring.application.index
=true
spring.application.name
=rocketmq-console
spring.http.encoding.charset
=UTF-8
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:9876;192.168.50.2:9876
#if you use rocketmq version <3.5.8, rocketmq.config.isVIPChannel should be false.default true
rocketmq.config.isVIPChannel
=
#rocketmq
-consoles data path:dashboard/monitor
rocketmq.config.dataPath=/tmp/rocketmq-console/data
#
set it false if you dont want use dashboard.default true
rocketmq.config.enableDashBoardCollect=true

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


[[email&#160;protected]153-215 rocketmq-console]# mv /usr/local/rocketmq-externals/rocketmq-console /usr/local/rocketmq-console
[[email&#160;protected]
153-215 rocketmq-console]# cd /usr/local/rocketmq-console/
[[email&#160;protected]
153-215 rocketmq-console]# ls
doc LICENSE NOTICE pom.xml README.md src style
[[email&#160;protected]
153-215 rocketmq-console]# mvn clean package -Dmaven.test.skip=true
........
[INFO] Building jar:
/usr/local/rocketmq-console/target/rocketmq-console-ng-1.0.0-sources.jar
[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO]
------------------------------------------------------------------------
[INFO] Total time:
02:54 min
[INFO] Finished at:
2019-01-11T17:02:34+08:00
[INFO]
------------------------------------------------------------------------
[[email&#160;protected]
153-215 rocketmq-console]# ls
doc LICENSE NOTICE pom.xml README.md src style target
[[email&#160;protected]
153-215 rocketmq-console]# ls target/
checkstyle
-cachefile checkstyle-checker.xml checkstyle-result.xml classes generated-sources maven-archiver maven-status rocketmq-console-ng-1.0.0.jar rocketmq-console-ng-1.0.0.jar.original rocketmq-console-ng-1.0.0-sources.jar
[[email&#160;protected]
153-215 rocketmq-console]# java -jar target/rocketmq-console-ng-1.0.0.jar
.......
[
2019-01-11 17:04:15.980] INFO Initializing ProtocolHandler ["http-nio-8080"]
[
2019-01-11 17:04:15.991] INFO Starting ProtocolHandler [http-nio-8080]
[
2019-01-11 17:04:16.232] INFO Using a shared selector for servlet write/read
[
2019-01-11 17:04:16.251] INFO Tomcat started on port(s): 8080 (http)
[
2019-01-11 17:04: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 运行日志:

技术分享图片

SyncConsumerTest 运行日志:

技术分享图片

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

技术分享图片

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



推荐阅读
  • 深入解析轻量级数据库 SQL Server Express LocalDB
    本文详细介绍了 SQL Server Express LocalDB,这是一种轻量级的本地 T-SQL 数据库解决方案,特别适合开发环境使用。文章还探讨了 LocalDB 与其他轻量级数据库的对比,并提供了安装和连接 LocalDB 的步骤。 ... [详细]
  • 本文介绍了一个基本的同步Socket程序,演示了如何实现客户端与服务器之间的简单消息传递。此外,文章还概述了Socket的基本工作流程,并计划在未来探讨同步与异步Socket的区别。 ... [详细]
  • ArcBlock 发布 ABT 节点 1.0.31 版本更新
    2020年11月9日,ArcBlock 区块链基础平台发布了 ABT 节点开发平台的1.0.31版本更新,此次更新带来了多项功能增强与性能优化。 ... [详细]
  • [编程题] LeetCode上的Dynamic Programming(动态规划)类型的题目
    继上次把backTracking的题目做了一下之后:backTracking,我把LeetCode的动态规划的题目又做了一下,还有几道比较难的Medium的题和Hard的题没做出来,后面会继续 ... [详细]
  • 本文介绍了进程的基本概念及其在操作系统中的重要性,探讨了进程与程序的区别,以及如何通过多进程实现并发和并行。文章还详细讲解了Python中的multiprocessing模块,包括Process类的使用方法、进程间的同步与异步调用、阻塞与非阻塞操作,并通过实例演示了进程池的应用。 ... [详细]
  • 本文详细介绍了Socket在Linux内核中的实现机制,包括基本的Socket结构、协议操作集以及不同协议下的具体实现。通过这些内容,读者可以更好地理解Socket的工作原理。 ... [详细]
  • 本文探讨了互联网服务提供商(ISP)如何可能篡改或插入用户请求的数据流,并提供了有效的技术手段来防止此类劫持行为,确保网络环境的安全与纯净。 ... [详细]
  • 纵坐标|据点_菜菜的sklearn课堂笔记支持向量机线性SVM决策过程的可视化
    纵坐标|据点_菜菜的sklearn课堂笔记支持向量机线性SVM决策过程的可视化 ... [详细]
  • Activity跳转动画 无缝衔接
    Activity跳转动画 无缝衔接 ... [详细]
  • 本文介绍了在解决Hive表中复杂数据结构平铺化问题后,如何通过创建视图来准确计算广告日志的曝光PV,特别是针对用户对应多个标签的情况。同时,详细探讨了UDF的使用方法及其在实际项目中的应用。 ... [详细]
  • 2022年4月15日的算法练习题,包括最长公共子序列和线段树的应用。 ... [详细]
  • 本文介绍了两个重要的Node.js库——cache-content-type和mime-types,它们在处理HTTP响应头时非常有用。cache-content-type是基于mime-types构建的,并且实现了缓存机制以提高性能。 ... [详细]
  • 本文详细解析 Skynet 的启动流程,包括配置文件的读取、环境变量的设置、主要线程的启动(如 timer、socket、monitor 和 worker 线程),以及消息队列的实现机制。 ... [详细]
  • java datarow_DataSet  DataTable DataRow 深入浅出
    本篇文章适合有一定的基础的人去查看,最好学习过一定net编程基础在来查看此文章。1.概念DataSet是ADO.NET的中心概念。可以把DataSet当成内存中的数据 ... [详细]
  • 深入理解线程池及其基本实现
    本文探讨了线程池的概念、优势及其在Java中的应用。通过实例分析不同类型的线程池,并指导如何构建一个简易的线程池。 ... [详细]
author-avatar
yoyokk99的秋天
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有