娃哈哈好喝-真的!
技术够用就行,吃好喝好睡好!

centos7部署rocketmq异步集群

1、环境介绍
主机名 ip Nameserver Broker
mq1 172.10.10.64 nameserver  
mq2 172.10.10.65 nameserver broker-a-master broker-b-slave
mq3 172.10.10.66 nameserver Broker-a-slave broker-b-master

为了方便可以将自定义主机名写在3台服务器的hosts里

[root@rocketmq1-64 ~]# cat /etc/hosts
172.10.10.64 mq1
172.10.10.65 mq2
172.10.10.66 mq3
2、基础环境搭建

3台服务器操作相同

jdk安装这里就省略了,本次演示使用的java版本:

[root@rocketmq1-64 ~]# java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
[root@rocketmq1-64 ~]#

下载安装mq,当前使用的是4.9.1版本

# 官方下载地址:https://rocketmq.apache.org/zh/download
[root@rocketmq1-64 ~]# unzip rocketmq-all-4.9.1-bin-release.zip
# 解压后将目录保存在/data/下面,并重新命名
[root@rocketmq1-64 ~]# mv rocketmq-all-4.9.1-bin-release ../rocketmq

配置mq环境变量

# 在文件最后加入下面内容
[root@rocketmq1-64 ~]# vim /etc/profile
export ROCKETMQ_HOME=/data/rocketmq
export PATH=$PATH:$ROCKETMQ_HOME/bin
export NAMESRV_ADDR='mq1:9876;mq2:9876;mq3:9876'
[root@rocketmq1-64 ~]# source /etc/profile
3、配置nameserver

name server是部署在3个节点上,所以三台都要配置,可以根据实际情况修改启动内存大小即可

# 修改jvm启动内存大小
[root@rocketmq1-64 bin]# pwd
/data/rocketmq/bin
[root@rocketmq1-64 bin]# ls
cachedog.sh       mqadmin       mqbroker.numanode0 mqnamesrv       nohup.out README.md     runserver.sh tools.sh
cleancache.sh     mqadmin.cmd   mqbroker.numanode1 mqnamesrv.cmd   os.sh     runbroker.cmd setcache.sh
cleancache.v1.sh mqbroker     mqbroker.numanode2 mqshutdown     play.cmd   runbroker.sh   startfsrv.sh
dledger           mqbroker.cmd mqbroker.numanode3 mqshutdown.cmd play.sh   runserver.cmd tools.cmd
[root@rocketmq1-64 bin]# vim runserver.sh
# 修改如下信息,根据实际情况修改,本次演示所以只给1g内存
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m

修改好之后可以启动nameserver

[root@rocketmq1-64 bin]# pwd
/data/rocketmq/bin
[root@rocketmq1-64 bin]# nohup ./mqnamesrv &
[root@rocketmq1-64 bin]# cat nohup
The Name Server boot success. serializeType=JSON

如果看到类似这样的信息说明启动成功,也可以查看监听端口,默认nameserver使用的是9876端口

4、配置broker

broker的配置文件在mq目录下的conf目录,官方有提供常用的部署方式示例文件

[root@rocketmq1-64 conf]# pwd
/data/rocketmq/conf
[root@rocketmq1-64 conf]# ll
total 36
drwxr-xr-x 2 root root   118 Aug 16  2021 2m-2s-async
drwxr-xr-x 2 root root   118 Aug 16  2021 2m-2s-sync
drwxr-xr-x 2 root root    91 Aug 16  2021 2m-noslave
-rwxr-xr-x 1 root root   976 May 29 16:07 broker.conf
drwxr-xr-x 2 root root    72 Aug 16  2021 dledger
-rwxr-xr-x 1 root root 14978 Aug 16  2021 logback_broker.xml
-rwxr-xr-x 1 root root  3836 Aug 16  2021 logback_namesrv.xml
-rwxr-xr-x 1 root root  3761 Aug 16  2021 logback_tools.xml
-rwxr-xr-x 1 root root  1305 Aug 16  2021 plain_acl.yml
-rwxr-xr-x 1 root root   834 Aug 16  2021 tools.yml
[root@rocketmq1-64 conf]#
# 示例目录介绍
2m-2s-async 异步主从,不会阻塞
2m-2s-sync 同步主从,需要确认是否同步到slave,如果没有同步到slave会导致生产者阻塞
dledger 是解决主从高可用的方案

broker只在mq2和mq3上部署,并且部署方式是2套主从交叉部署,mq2上部署broker-a的master节点和broker-b的slave节点,具体配置文件如下

mq2节点,broker-a-master的配置
[root@rocketmq2-65 2m-2s-async]# pwd
/data/rocketmq/conf/2m-2s-async
[root@rocketmq2-65 2m-2s-async]# ls
broker-a.properties broker-a-s.properties broker-b.properties broker-b-s.properties
[root@rocketmq2-65 2m-2s-async]# cat broker-a.properties
#所属集群名字,名字一样的节点就在同一个集群内
brokerClusterName=DefaultCluster
brokerIP1=172.10.10.65
#broker名字,名字一样的节点就是一组主从节点。
brokerName=broker-a
#brokerid,0就表示是Master,>0的都是表示 Slave
brokerId=0
#nameServer地址,分号分割
namesrvAddr=172.10.10.64:9876;172.10.10.165:9876;172.10.10.66:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=10911
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=48
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#存储路径,如果有多个broker集群,一定要区分开,同集群可以使用相同目录
storePathRootDir=/data/rocketmq/store-a
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/store-a/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/data/rocketmq/store-a/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq/store-a/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq/store-a/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq/store-a/abort
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
[root@rocketmq2-65 2m-2s-async]#
mq2节点,broker-b-slave的配置
[root@rocketmq2-65 2m-2s-async]# cat broker-b-s.properties 
brokerClusterName=DefaultCluster
brokerIP1=172.10.10.65
brokerName=broker-b
brokerId=1
namesrvAddr=172.10.10.64:9876;172.10.10.165:9876;172.10.10.66:9876
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
listenPort=10921
deleteWhen=04
fileReservedTime=48
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000
diskMaxUsedSpaceRatio=88
storePathRootDir=/data/rocketmq/store-b
storePathCommitLog=/data/rocketmq/store-b/commitlog
storePathConsumeQueue=/data/rocketmq/store-b/consumequeue
storePathIndex=/data/rocketmq/store-b/index
storeCheckpoint=/data/rocketmq/store-b/checkpoint
abortFile=/data/rocketmq/store-b/abort
maxMessageSize=65536
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
[root@rocketmq2-65 2m-2s-async]#
Mq3节点,broker-a-slave的配置
[root@rocketmq3-66 2m-2s-async]# cat broker-a-s.properties 
brokerClusterName=DefaultCluster
brokerIP1=172.10.10.66
brokerName=broker-a
brokerId=1
namesrvAddr=172.10.10.64:9876;172.10.10.165:9876;172.10.10.66:9876
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
listenPort=10911
deleteWhen=04
fileReservedTime=48
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000
diskMaxUsedSpaceRatio=88
storePathRootDir=/data/rocketmq/store-a
storePathCommitLog=/data/rocketmq/store-a/commitlog
storePathConsumeQueue=/data/rocketmq/store-a/consumequeue
storePathIndex=/data/rocketmq/store-a/index
storeCheckpoint=/data/rocketmq/store-a/checkpoint
abortFile=/data/rocketmq/store-a/abort
maxMessageSize=65536
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
[root@rocketmq1-66 2m-2s-async]#

Mq3节点,broker-b-master的配置

[root@rocketmq3-66 2m-2s-async]# cat broker-b.properties 
brokerClusterName=DefaultCluster
brokerIP1=172.10.10.66
brokerName=broker-b
brokerId=0
namesrvAddr=172.10.10.64:9876;172.10.10.165:9876;172.10.10.66:9876
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
listenPort=10921
deleteWhen=04
fileReservedTime=48
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000
diskMaxUsedSpaceRatio=88
storePathRootDir=/data/rocketmq/store-b
storePathCommitLog=/data/rocketmq/store-b/commitlog
storePathConsumeQueue=/data/rocketmq/store-b/consumequeue
storePathIndex=/data/rocketmq/store-b/index
storeCheckpoint=/data/rocketmq/store-b/checkpoint
abortFile=/data/rocketmq/store-b/abort
maxMessageSize=65536
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
[root@rocketmq1-66 2m-2s-async]#

启动borker

mq2节点

# 分别启动broker-a的master和broker-b-slave
[root@rocketmq2-65 bin]# pwd
/data/rocketmq/bin
[root@rocketmq2-65 bin]# nohup ./mqbroker -c ../conf/2m-2s-async/broker-a.properties &
[root@rocketmq2-65 bin]# nohup ./mqbroker -c ../conf/2m-2s-async/broker-b-s.properties &

mq3节点

# 分别启动broker-a的master和broker-b-slave
[root@rocketmq3-66 bin]# pwd
/data/rocketmq/bin
[root@rocketmq3-66 bin]# nohup ./mqbroker -c ../conf/2m-2s-async/broker-a-s.properties &
[root@rocketmq3-66 bin]# nohup ./mqbroker -c ../conf/2m-2s-async/broker-b.properties &
5、测试

测试生产消息

[root@rocketmq2-65 bin]# pwd
/data/rocketmq/bin
[root@rocketmq2-65 bin]# ./tools.sh org.apache.rocketmq.example.quickstart.Producer
# 如果看到类似这样的信息,说明可以生产消息
SendResult [sendStatus=SEND_OK, msgId=7F000001A8314AA298B7951967CC03E7, offsetMsgId=AC1414A600002AA9000000000002ECD2, messageQueue=MessageQueue [topic=TopicTest, brokerName=broker-b, queueId=3], queueOffset=249]
22:51:10.186 [NettyClientSelector_1] INFO RocketmqRemoting - closeChannel: close the connection to remote address[172.10.10.65:10911] result: true

测试消费消息

[root@rocketmq2-65 bin]# pwd
/data/rocketmq/bin
[root@rocketmq2-65 bin]# ./tools.sh org.apache.rocketmq.example.quickstart.Consumer
# 如果看到类似这样的信息,说明可以消费消息
ConsumeMessageThread_5 Receive New Messages: [MessageExt [brokerName=broker-a, queueId=0, storeSize=192, queueOffset=241, sysFlag=0, bornTimestamp=1716994270105, bornHost=/172.10.10.66:21702, storeTimestamp=1716994270108, storeHost=/172.10.10.65:10911, msgId=AC1414A500002A9F000000000002D292, commitLogOffset=184978, bodyCRC=855266886, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=250, CONSUME_START_TIME=1716994384554, UNIQ_KEY=7F000001A8314AA298B79519679903A0, CLUSTER=DefaultCluster, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 57, 50, 56], transactionId='null'}]]
6、dashboard安装
# 下载dashboard
[root@rocketmq1-64 ~]# wget https://dist.apache.org/repos/dist/release/rocketmq/rocketmq-dashboard/1.0.0/rocketmq-dashboard-1.0.0-source-release.zip

官网下载只有源码,需要自己用mvn进行打包,mvn工具安装就不介绍了,打包之前需要修改一下配置

[root@rocketmq1-64 ~]# unzip rocketmq-dashboard-1.0.0-source-release.zip
[root@rocketmq1-64 ~]# cd rocketmq-dashboard-1.0.0/
[root@rocketmq1-64 ~]# vim src/main/resources/application.properties
# 修改namesrv的地址,也可以修改端口,默认端口是8080
rocketmq.config.namesrvAddr=172.10.10.64:9876;172.10.10.65:9876;172.10.10.66:9876
# 修改完之后开始打包
[root@rocketmq1-64 ~]# mvn clean package -Dmaven.test.skip=true
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.406 s
[INFO] Finished at: 2024-05-29T23:18:22+08:00
[INFO] ------------------------------------------------------------------------
# 出现类似这样的信息说明打包成功
[root@rocketmq1-64 ~]# cd target/
[root@rocketmq1-64 target]# ls
checkstyle-cachefile   classes           maven-shared-archive-resources rocketmq-dashboard-1.0.0.jar.original
checkstyle-checker.xml generated-sources maven-status
checkstyle-result.xml   maven-archiver     rocketmq-dashboard-1.0.0.jar
# rocketmq-dashboard-1.0.0.jar这个jar包就是我们需要用到的
# 将jar包放到指定目录
[root@rocketmq1-64 target]# cp rocketmq-dashboard-1.0.0.jar /data/rocketmq-dashboard
[root@rocketmq1-64 target]# cd /data/rocketmq-dashboard
# 启动dashboard
[root@rocketmq1-64 rocketmq-dashboard]# nohup java -jar ./rocketmq-dashboard-1.0.0.jar >./dashboard.log 2>&1 &
[root@rocketmq1-64 rocketmq-dashboard]# cat dashboard.log
[2024-05-29 23:09:43.288] INFO Starting ProtocolHandler ["http-nio-0.0.0.0-8080"]
[2024-05-29 23:09:43.307] INFO Tomcat started on port(s): 8080 (http) with context path ''
[2024-05-29 23:09:43.310] INFO Started App in 4.056 seconds (JVM running for 4.627)
# 出现类似这样的信息说明启动成功,可以使用8080端口进行访问了
赞(0)
未经允许不得转载:娃哈哈好喝 » centos7部署rocketmq异步集群
分享到: 更多 (0)

相关推荐

  • 暂无文章