完成了单节点的RocketMQ的安装 RocketMQ-初体验RocketMQ(02)_单节点RocketMQ的安装 ,我们来搞个2个节点的集群来玩下
CentOS7
192.168.18.130 、192.168.18.131
Q: 生产环境,假设你有2台主机,应该如何部署RocketMQ集群更HA一些?
你有 A、B两台主机,
A主机 部署 : broker-a 主节点 和 broker-b 从节点
B主机 部署: broker-b 主节点 和 borker-a 从节点
两台主机互为主备,HA更可靠一些,即使有一台主机,假设B主机 宕机了。 我们的A主机上 仍然还有是 broker-a 主节点 和 broker-b 从节点, 这个时候只是 broker-b 只能提供消费消息的能力了,不能写入了。 还是要比 整个 broker-b cluster 因为主机B的宕机而全部不能提供服务了好。
在RocketMQ 安装目录 conf目录下,官方提供了一些参考配置
可以集合自己的场景,到对应的目录下看下官方推荐的配置。
130 : brokera-m brokera-s
131: brokerb-m brokerb-s
broker集群在同一台主机上互为主备 。
每台机器上启动一个namesrv。 两个 broker节点,互为主备,构成一个broker集群。
cp 两个文件出来
broker-m.conf master节点的配置文件
broker-s.conf slave节点的配置文件
broker-m.conf
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#集群名称,可自定义
brokerClusterName=DefaultCluster
brokerName=broker‐a
# 0 表示主节点
brokerId=0
# rocketmq‐name服务地址,多个地址用;分开,不配置默认为localhost:9876
namesrvAddr=192.168.18.130:9876;192.168.18.131:9876
deleteWhen=04
fileReservedTime=48
#当前节点角色
brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH
autoCreateTopicEnable=true
#broker通信端口,默认端口
listenPort=10911
#消息存储根路径
storePathRootDir=/data/rocketmq/store-m
broker-s.conf
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#集群名称,可自定义
brokerClusterName=DefaultCluster
brokerName=broker‐a
# 非0 表示从节点
brokerId=1
# rocketmq‐name服务地址,多个地址用;分开,不配置默认为localhost:9876
namesrvAddr=192.168.18.130:9876;192.168.18.131:9876
deleteWhen=04
fileReservedTime=48
#当前节点角色
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
autoCreateTopicEnable=true
#broker通信端口,默认端口
listenPort=10811
#消息存储根路径
storePathRootDir=/data/rocketmq/store-s
主要注意的地方
nohup sh bin/mqnamesrv -n 192.168.18.130:9876&
最好通过-n指定主IP,否则的话有可能在多网卡 或者docker环境下 启动失败
[root@artisan rocketmq-all-4.3.2-bin-release]# pwd
/usr/local/rocketmq/rocketmq-all-4.3.2-bin-release
[root@artisan rocketmq-all-4.3.2-bin-release]# nohup sh bin/mqnamesrv -n 192.168.18.130:9876&
[1] 9236
[root@artisan rocketmq-all-4.3.2-bin-release]# nohup: ignoring input and appending output to ‘nohup.out’
tail -f nohup.out 日志如下:
Java HotSpot(TM) 64-Bit Server VM warning: Using the DefNew young collector with the CMS collector is deprecated and will likely be removed in a future release
Java HotSpot(TM) 64-Bit Server VM warning: UseCMSCompactAtFullCollection is deprecated and will likely be removed in a future release.
The Name Server boot success. serializeType=JSON
查看进程
[root@artisan rocketmq-all-4.3.2-bin-release]# jps |grep -v Jps
9239 NamesrvStartup
[root@artisan rocketmq-all-4.3.2-bin-release]#
主节点: nohup sh bin/mqbroker -c conf/broker-m.conf &
从节点: nohup sh bin/mqbroker -c conf/broker-s.conf &
[root@artisan rocketmq-all-4.3.2-bin-release]# >nohup.out
主节点启动
[root@artisan rocketmq-all-4.3.2-bin-release]# nohup sh bin/mqbroker -c conf/broker-m.conf &
[2] 9415
[root@artisan rocketmq-all-4.3.2-bin-release]# nohup: ignoring input and appending output to ‘nohup.out’
tailf nohup.out
The broker[brokerâa, 192.168.18.130:10911] boot success. serializeType=JSON and name server is 192.168.18.130:9876;192.168.18.131:9876
^C^C
从节点启动
[root@artisan rocketmq-all-4.3.2-bin-release]# nohup sh bin/mqbroker -c conf/broker-s.conf &
[3] 9495
[root@artisan rocketmq-all-4.3.2-bin-release]# nohup: ignoring input and appending output to ‘nohup.out’
tailf nohup.out
The broker[brokerâa, 192.168.18.130:10911] boot success. serializeType=JSON and name server is 192.168.18.130:9876;192.168.18.131:9876
The broker[brokerâa, 192.168.18.130:10811] boot success. serializeType=JSON and name server is 192.168.18.130:9876;192.168.18.131:9876
^C
查看进程
[root@artisan rocketmq-all-4.3.2-bin-release]# jps |grep -v Jps
9239 NamesrvStartup
9419 BrokerStartup
9499 BrokerStartup
[root@artisan rocketmq-all-4.3.2-bin-release]#
操作同130 , 唯一不同的就是这个 brokerName 。
这里贴下 配置
主节点
[root@artisan conf]# cat broker-m.conf
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#集群名称,可自定义
brokerClusterName=DefaultCluster
brokerName=broker‐b
# 0 表示主节点
brokerId=0
# rocketmq‐name服务地址,多个地址用;分开,不配置默认为localhost:9876
namesrvAddr=192.168.18.130:9876;192.168.18.131:9876
deleteWhen=04
fileReservedTime=48
#当前节点角色
brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH
autoCreateTopicEnable=true
#broker通信端口,默认端口
listenPort=10911
#消息存储根路径
storePathRootDir=/data/rocketmq/store-m
从节点
[root@artisan conf]# cat broker-s.conf
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#集群名称,可自定义
brokerClusterName=DefaultCluster
brokerName=broker‐b
# 非0 表示从节点
brokerId=1
# rocketmq‐name服务地址,多个地址用;分开,不配置默认为localhost:9876
namesrvAddr=192.168.18.130:9876;192.168.18.131:9876
deleteWhen=04
fileReservedTime=48
#当前节点角色
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
autoCreateTopicEnable=true
#broker通信端口,默认端口
listenPort=10811
#消息存储根路径
storePathRootDir=/data/rocketmq/store-s
[root@artisan conf]#
同130
同130
mqadmin clusterlist -n 192.168.18.130:9876
[root@artisan rocketmq-all-4.3.2-bin-release]# sh bin/mqadmin clusterlist -n 192.168.18.130:9876
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
#Cluster Name #Broker Name #BID #Addr #Version #InTPS(LOAD) #OutTPS(LOAD) #PCWait(ms) #Hour #SPACE
DefaultCluster brokerâa 0 192.168.18.130:10911 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436943.73 0.1331
DefaultCluster brokerâa 1 192.168.18.130:10811 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436943.73 0.1331
DefaultCluster brokerâb 0 192.168.18.131:10911 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436943.73 0.1654
DefaultCluster brokerâb 1 192.168.18.131:10811 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436943.73 0.1654
^C^C^C[root@artisan rocketmq-all-4.3.2-bin-release]# ^C
[root@artisan rocketmq-all-4.3.2-bin-release]# sh bin/mqadmin clusterlist -n 192.168.18.131:9876
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
#Cluster Name #Broker Name #BID #Addr #Version #InTPS(LOAD) #OutTPS(LOAD) #PCWait(ms) #Hour #SPACE
DefaultCluster brokerâa 0 192.168.18.130:10911 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436943.73 0.1331
DefaultCluster brokerâa 1 192.168.18.130:10811 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436943.73 0.1331
DefaultCluster brokerâb 0 192.168.18.131:10911 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436943.73 0.1654
DefaultCluster brokerâb 1 192.168.18.131:10811 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436943.73 0.1654
[root@artisan rocketmq-all-4.3.2-bin-release]#
如果碰到了如下错误
[root@artisan bin]# ./mqadmin clusterlist -n 192.168.18.130:9876
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
org.apache.rocketmq.tools.command.SubCommandException: ClusterListSubCommand command failed
at org.apache.rocketmq.tools.command.cluster.ClusterListSubCommand.execute(ClusterListSubCommand.java:93)
at org.apache.rocketmq.tools.command.MQAdminStartup.main0(MQAdminStartup.java:132)
at org.apache.rocketmq.tools.command.MQAdminStartup.main(MQAdminStartup.java:83)
Caused by: org.apache.rocketmq.remoting.exception.RemotingTimeoutException: wait response on the channel <192.168.18.130:9876> timeout, 463(ms)
at org.apache.rocketmq.remoting.netty.NettyRemotingAbstract.invokeSyncImpl(NettyRemotingAbstract.java:391)
at org.apache.rocketmq.remoting.netty.NettyRemotingClient.invokeSync(NettyRemotingClient.java:374)
at org.apache.rocketmq.client.impl.MQClientAPIImpl.getBrokerClusterInfo(MQClientAPIImpl.java:1180)
at org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl.examineBrokerClusterInfo(DefaultMQAdminExtImpl.java:275)
at org.apache.rocketmq.tools.admin.DefaultMQAdminExt.examineBrokerClusterInfo(DefaultMQAdminExt.java:222)
at org.apache.rocketmq.tools.command.cluster.ClusterListSubCommand.printClusterBaseInfo(ClusterListSubCommand.java:172)
at org.apache.rocketmq.tools.command.cluster.ClusterListSubCommand.execute(ClusterListSubCommand.java:88)
... 2 more
[root@artisan bin]# ^C 重试几次
[root@artisan bin]# ./mqadmin clusterlist -n 192.168.18.130:9876
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0
#Cluster Name #Broker Name #BID #Addr #Version #InTPS(LOAD) #OutTPS(LOAD) #PCWait(ms) #Hour #SPACE
DefaultCluster broker?a 0 192.168.18.130:10911 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436999.54 0.1360
DefaultCluster broker?a 1 192.168.18.130:10811 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436999.54 0.1360
DefaultCluster broker?b 0 192.168.18.131:10911 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436999.54 0.1690
DefaultCluster broker?b 1 192.168.18.131:10811 V4_3_2 0.00(0,0ms) 0.00(0,0ms) 0 436999.54 0.1690
[root@artisan bin]#
可能是mqadmin没能正确的连接进去!网络抖动,这个跟底层netty的连接有关. 多重试几次,一般都没问题。
[root@artisan rocketmq-all-4.3.2-bin-release]# export NAMESRV_ADDR=192.168.18.131:9876
[root@artisan rocketmq-all-4.3.2-bin-release]#
[root@artisan rocketmq-all-4.3.2-bin-release]# sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
测试消费者
[root@artisan rocketmq-all-4.3.2-bin-release]# sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
运行结果 截个图
是不是希望有个页面可以管理和查看集群信息呢? RocketMQ 提供了 RocketMQ Console ,下篇博文我们来看下如何通过拉取源码来搭建一套本地的RocketMQ Console吧
刚刚搭建的 是 左边的 ,生产环境更建议使用右侧的部署方式
配置的话 ,仅需要调整 broker-s.conf 中的 brokerName即可
131:
broker-s.conf
130 :
broker-s :
启动后,打开RocketMQConsole 看下,符合部署。
请移步 RocketMQ4.3.x 史上配置最全详解,没有之一