这里是weihubeats,觉得文章不错可以关注公众号小奏技术,文章首发。拒绝营销号,拒绝标题党
背景
由于RocketMQ
5.x主从同步的方式改动非常大,集群部署的方式也改动很大,所以打算本地启动一个小集群,进行debug研究源码学习
RocketMQ版本
- 5.1.0
集群
打算本地启动3个Nameserve
+ 2个broker
Nameserve
controller
内嵌在Nameserve
中,也是三个
由于是本地启动多个controller
+Nameserve
,所以我们需要将端口打散,避免端口冲突
controller的端口如何配置
实际Nameserve
的端口很容易知道是listenPort
这个配置
但是controller
得端口具体是怎么配置的,官方也没有详细的文档说明,这里我们直接看源码吧
-
- DLedgerConfig
这里的this.peers就是我们配置的controllerDLegerPeers
n0-127.0.0.1:9878;n1-10.100.10.200:9878;n2-10.100.9.21:9878
selfId就是我们配置的controllerDLegerSelfId
所以可以确定的是controller端口就是直接在controller最后面配置的
比如我们配置为
controllerDLegerPeers = n0-127.0.0.1:9870;n1-127.0.0.1:9871;n2-127.0.0.1:9872
controllerDLegerSelfId = n0
他会通过controllerDLegerPeers
和controllerDLegerSelfId
解析出你用n0-127.0.0.1:9870
9870
这个端口
Nameserve 配置
- namesrv-n0.conf
enableControllerInNamesrv = true
#controller config
controllerDLegerGroup = group1
enableElectUncleanMaster = true
controllerDLegerPeers = n0-127.0.0.1:9870;n1-127.0.0.1:9871;n2-127.0.0.1:9872
controllerDLegerSelfId = n0
listenPort = 9000
- namesrv-n1.conf
enableControllerInNamesrv = true
#controller config
controllerDLegerGroup = group1
enableElectUncleanMaster = true
controllerDLegerPeers = n0-127.0.0.1:9870;n1-127.0.0.1:9871;n2-127.0.0.1:9872
controllerDLegerSelfId = n1
listenPort = 9001
- namesrv-n2.conf
enableControllerInNamesrv = true
#controller config
controllerDLegerGroup = group1
enableElectUncleanMaster = true
controllerDLegerPeers = n0-127.0.0.1:9870;n1-127.0.0.1:9871;n2-127.0.0.1:9872
controllerDLegerSelfId = n2
listenPort = 9002
idea启动参数
- Program arguments
org.apache.rocketmq.namesrv.NamesrvStartup -c /Users/xiaozou/Desktop/sofe/java/rocketmq/distribution/conf/controller/local/namesrv-n0.conf
- Envo
ROCKETMQ_HOME=/Users/xiaozou/Desktop/sofe/java/rocketmq/distribution
注意我们需要勾选允许启动多个实例
启动三个Nameserve
依次改用不同的启动参数启动三个nameserver
org.apache.rocketmq.namesrv.NamesrvStartup -c /Users/xiaozou/Desktop/sofe/java/rocketmq/distribution/conf/controller/local/namesrv-n0.conf
org.apache.rocketmq.namesrv.NamesrvStartup -c /Users/xiaozou/Desktop/sofe/java/rocketmq/distribution/conf/controller/local/namesrv-n1.conf
org.apache.rocketmq.namesrv.NamesrvStartup -c /Users/xiaozou/Desktop/sofe/java/rocketmq/distribution/conf/controller/local/namesrv-n2.conf
通过SyncStateSet查看集群状态
我们以源码方式运行MQAdminStartup
getControllerMetaData -a localhost:9870
运行结果如下
可以看到我们成功启动了3个节点的集群
broker+proxy 配置
这里本地要启动一主一从,proxy内置在broker中
先看看配置文件
- broker-a-n0
brokerClusterName = xiaozou
brokerName = broker-a
brokerId = -1
brokerRole = SLAVE
deleteWhen = 04
fileReservedTime = 48
enableControllerMode = true
#fetchNamesrvAddrByAddressServer = true
#fetchControllerAddrByDnsLookup = true
controllerAddr = 127.0.0.1:9870;127.0.0.1:9871;127.0.0.1:9872
#namesrvAddr = 127.0.0.1:9000;127.0.0.1:9001;127.0.0.1:9002
allAckInSyncStateSet=true
listenPort=30911
storePathRootDir=/Users/xiaozou/Desktop/sofe/rocketmq-file/rmqstore/node00/store
autoCreateTopicEnable=false
autoCreateSubscriptionGroup=false
enablePropertyFilter=true
slaveReadEnable=true
waitTimeMillsInSendQueue=1000
- rmq-proxy-n0.json
{
"rocketMQClusterName": "xiaozou",
"namesrvAddr": "127.0.0.1:9000;127.0.0.1:9001;127.0.0.1:9002",
"grpcServerPort": 8081,
"remotingListenPort": 8080
}
- broker-a-n1
brokerClusterName = xiaozou
brokerName = broker-a
brokerId = -1
brokerRole = SLAVE
deleteWhen = 04
fileReservedTime = 48
enableControllerMode = true
#fetchNamesrvAddrByAddressServer = true
#fetchControllerAddrByDnsLookup = true
controllerAddr = 127.0.0.1:9870;127.0.0.1:9871;127.0.0.1:9872
#namesrvAddr = 127.0.0.1:9000;127.0.0.1:9001;127.0.0.1:9002
enableElectUncleanMaster = true
allAckInSyncStateSet=true
listenPort=30916
storePathRootDir=/Users/xiaozou/Desktop/sofe/rocketmq-file/rmqstore/node10/store
autoCreateTopicEnable=false
autoCreateSubscriptionGroup=false
enablePropertyFilter=true
slaveReadEnable=true
waitTimeMillsInSendQueue=1000
- rmq-proxy-n1.json
{
"rocketMQClusterName": "xiaozou",
"namesrvAddr": "127.0.0.1:9000;127.0.0.1:9001;127.0.0.1:9002",
"grpcServerPort": 8082,
"remotingListenPort": 8083
}
启动 proxy+broker
- Program arguments
org.apache.rocketmq.proxy.ProxyStartup
-bc
/Users/xiaozou/Desktop/sofe/java/rocketmq/distribution/conf/controller/local/broker-a-n0.conf
-pc
/Users/xiaozou/Desktop/sofe/java/rocketmq/distribution/conf/controller/local/rmq-proxy-n0.json
-pm
Local
- Environment variables
ROCKETMQ_HOME=/Users/xiaozou/Desktop/sofe/java/rocketmq/distribution
第二个broker的配置
- Program arguments
org.apache.rocketmq.proxy.ProxyStartup
-bc
/Users/xiaozou/Desktop/sofe/java/rocketmq/distribution/conf/controller/local/broker-a-n1.conf
-pc
/Users/xiaozou/Desktop/sofe/java/rocketmq/distribution/conf/controller/local/rmq-proxy-n1.json
-pm
Local
Environment variables
保持和之前一致
看到如下log就是我们启动成功了
启动 dashboard 查看集群
这里我们在本地启动一个dashboard
可以看到我们的broker是一主一从
我们新建一个topic试试
可以看到也是没问题的,至此我们的本地集群就搭建完成了
总结
我们本次以源码的方式启动了3个Nameserve+2个broker
其中还包含3个controller+2个proxy
后续我们需要debug观测RocketMQ
集群的源代码就非常方便了