RocketMq-集群搭建笔记

服务器准备

10.96.115.40 mq-01 broker-a
10.96.83.21 mq-02 broker-b
10.96.83.21 mq-02 broker-a-s
10.96.115.40 mq-01 broker-b-s
10.96.83.21、10.96.115.40 mq-01、mq-02 nameserver、console

代码准备

采用的是源码编译,

1
2
3
4
5
6
7
8
9
git clone https://github.com/apache/rocketmq.git

cd rocketmq

mvn -Prelease-all -DskipTests clean install -U

cp -rf distribution/target/rocketmq-4.5.2/rocketmq-4.5.2/ {workdir}/rocketmq-4.5.2

cd {workdir}/rocketmq-4.5.2

启动nameserver

1
nohup sh bin/mqnamesrv &

启动borker

很贴心的为我们准备了配置的模板2m-2s-async、2m-2s-sync、2m-noslave

  • 10.96.115.40是broker-a、broker-b-s
  • 10.96.83.21是broker-b、broker-a-s

10.96.115.40的操作

broker-a的配置

1
2
3
4
5
6
7
8
9
10
11
namesrvAddr=10.96.115.40:9876;10.96.83.21:9876    //所关联的namesrv地址
brokerClusterName=TestCluster     //集群名称
brokerName=broker-a
brokerId=0                                                 //0代表主
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER             //角色
flushDiskType=ASYNC_FLUSH           //异步刷盘
storePathRootDir=/home/xiaoju/logs/rocketmqlogs/store  
storePathCommitLog=/home/xiaoju/logs/rocketmqlogs/store/commitlog  //日志存放目录
listenPort=10900

broker-b-s的配置

1
2
3
4
5
6
7
8
9
10
11
12
namesrvAddr=10.96.115.40:9876;10.96.83.21:9876
brokerClusterName=TestCluster
brokerName=broker-b
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
listenPort=10910   //更改端口号,同一台机器上两个broker不可采用同一端口

storePathRootDir=/opt/logs/rocketmqlogs/store  
storePathCommitLog=/opt/logs/rocketmqlogs/store/commitlog

启动broker

1
2
3
cd {workdir}/rocketmq-4.5.2
nohup sh bin/mqbroker -c /home/xiaoju/rocketmq-4.5.2/conf/2m-2s-async/broker-a.properties &
nohup sh bin/mqbroker -c /home/xiaoju/rocketmq-4.5.2/conf/2m-2s-async/broker-b-s.properties &

10.96.83.21的操作

broker-b的配置

1
2
3
4
5
6
7
8
9
10
11
namesrvAddr=10.96.115.40:9876;10.96.83.21:9876
brokerClusterName=TestCluster
brokerName=broker-b
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
storePathRootDir=/home/xiaoju/logs/rocketmqlogs/store  
storePathCommitLog=/home/xiaoju/logs/rocketmqlogs/store/commitlog
listenPort=10900

broker-a-s的配置

1
2
3
4
5
6
7
8
9
10
11
namesrvAddr=10.96.115.40:9876;10.96.83.21:9876
brokerClusterName=TestCluster
brokerName=broker-a
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
listenPort=10910
storePathRootDir=/opt/logs/rocketmqlogs/store
storePathCommitLog=/opt/logs/rocketmqlogs/store/commitlog

启动broker

1
2
3
cd {workdir}/rocketmq-4.5.2
nohup sh bin/mqbroker -c /home/xiaoju/rocketmq-4.5.2/conf/2m-2s-async/broker-b.properties &
nohup sh bin/mqbroker -c /home/xiaoju/rocketmq-4.5.2/conf/2m-2s-async/broker-a-s.properties &

启动rocketmq-console

一个springboot工程

1
git clone https://github.com/apache/rocketmq-externals.git

mqadmin常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
查看帮助:
# sh mqadmin <command> -h
查询Producer的网络连接情况:
# sh mqadmin producerConnection -n localhost:9876 -g <producer-group> -t <producer-topic>
查询Consumer的网络连接情况:
# sh mqadmin consumerConnection -n localhost:9876 -g <consumer-group>
查询Consumer的消费状态:
# sh mqadmin consumerProgress -n localhost:9876 -g <consumer-group>

查询消息是否发送成功
获取指定Topic:
# sh mqadmin topicList -n localhost:9876 | grep <topicName>
查看Topic状态:
# sh mqadmin topicStatus -n localhost:9876 -t <topicName>
根据offset获取消息:
# sh sh mqadmin queryMsgByOffset -n localhost:9876 -b <broker-name> -i <queueId> -o <offset> -t <topicName>
根据offsetMsgId查询消息:
# sh sh mqadmin queryMsgById -n localhost:9876 -i <offsetMsgId>

查询消息是否被消费成功
查询消息详情:
# sh mqadmin queryMsgById -i {MsgId} -n {NameServerAddr}
查看Consumer Group订阅了哪些TOPIC:
# sh mqadmin consumerProgress -g <ConsumerGroup> -n <NameServerAddr>

查询TOPIC被哪些Consumer Group订阅了
没有查询特定TOPIC订阅情况,只能查询所有后再过滤:
# sh mqadmin statsAll -n <NameServerAddr> | grep <TOPIC>
返回结果:#Topic #Consumer Group #InTPS #OutTPS #InMsg24Hour #OutMsg24Hour

关闭nameserver和所有的broker:
# sh mqshutdown namesrv
# sh mqshutdown broker

查看所有消费组group:
# sh mqadmin consumerProgress -n 192.168.23.159:9876
查看指定消费组(kevinGroupConsumer)下的所有topic数据堆积情况:
# sh mqadmin consumerProgress -n 192.168.23.159:9876 -g kevinGroupConsumer
查看所有topic :
# sh mqadmin topicList -n 192.168.23.159:9876
查看topic信息列表详情统计
# sh mqadmin topicstatus -n 192.168.23.159:9876 -t myTopicTest1
新增topic
# sh mqadmin updateTopic –n 10.96.115.40:9876;10.96.83.21:9876 –c TestCluster –t <TopictName>
删除topic
# sh mqadmin deleteTopic –n 10.45.47.168:9876 –c DefaultCluster –t ZTEExample