四台服务器
nameIP服务 配置
rocketmq-a
192.168.33.11broker-a,namesrv,consolecpu:2,内存:2G
rocketmq-a-s
192.168.33.12broker-a-s,namesrv,console
cpu:2,内存:2G
rocketmq-b
192.168.33.13
broker-b,namesrv,console
cpu:2,内存:2G
rocketmq-b-s
192.168.33.14
broker-b-s,namesrv,console
cpu:2,内存:2G
运行初始化脚本
搭建java-1.8环境,略
RocketMQ部署类型
单个Master
单机模式, 即只有一个Broker, 如果Broker宕机了, 会导致RocketMQ服务不可用, 不推荐使用.
多Master模式
组成一个集群, 集群每个节点都是Master节点, 配置简单, 性能也是最高, 某节点宕机重启不会影响RocketMQ服务, 缺点就是如果某个节点宕机了, 会导致该节点未被消费的消息在在节点恢复前不可订阅.
多Master多Slave模式,异步复制
每个Master配置一个Slave, 多对Master-Slave, Master与Slave消息采用异步复制方式, 主从消息一致会有毫秒级的延迟. 优点是弥补了多Master模式下节点宕机后在恢复前不可订阅的问题, 在Master宕机后, 消费者还可以从Slave节点进行消费, 缺点就是如果Master宕机, 磁盘损坏的情况下, 如果没有即使将消息复制到Slave, 会导致有少量消息丢失.
多Master多Slave模式,同步双写
每个Master配置一个Slave,有多对Master-Slave,HA采用同步双写方式,主备都写成功,向应用返回成功。数据与服务都无单点,Master宕机情况下,消息无延迟,服务可用性与数据可用性都非常高。缺点就是性能比异步复制模式略低,大约低10%左右,发送单个消息的RT会略高。
由于项目对于安全性要求较高,所以选择多Master多Slave模式,同步双写模式
首先编译源码包
rocketmq-a节点
```
mkdir/data/
cd/data
#maven环境
wgethttp://mirrors.hust.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
tarxvfapache-maven-3.6.3-bin.tar.gz-C/data
cat>>/etc/profile<<EOF
exportMAVEN_HOME=/data/maven
exportPATH=\$PATH:\$MAVEN_HOME/bin
EOF
source/etc/profile
mvn--version
#rocketmqlogs路径在这里写死了,需要改源码:user.home改为/data,根据自己需要 未能解决
#vim /data/rocketmq-release-4.5.2/client/src/main/java/org/apache/rocketmq/client/log/ClientLogger.java
#String clientLogRoot = System.getProperty(CLIENT_LOG_ROOT, System.getProperty("/data") + "/logs/rocketmqlogs")
#编译
wgethttps://codeload.github.com/apache/rocketmq/zip/release-4.5.2
unziprocketmq-release-4.5.2.zip
cdrocketmq-release-4.5.2
mvn-Prelease-all-DskipTestscleaninstall-U
#编译完成后文件路径:./distribution/target/rocketmq-4.5.2.tar.gz
#分发文件
scp./distribution/target/rocketmq-4.5.2.tar.gz192.168.33.12:/data
scp./distribution/target/rocketmq-4.5.2.tar.gz192.168.33.13:/data
scp./distribution/target/rocketmq-4.5.2.tar.gz192.168.33.14:/data
mv./distribution/target/rocketmq-4.5.2.tar.gz/data
```
配置broker,namesrv
rocketmq-a节点
```
useradd-s/sbin/nologinrocketmq
tarxvfrocketmq-4.5.2.tar.gz
ln-srocketmq-4.5.2rocketmq
mkdir-pv/data/rocketmq/store/{commitlog,consumequeue,index}
#配置namesrv
cd/data/rocketmq/conf
vimnamesrv.properties#默认没有这个文件,新建一个
listenPort=5432
#配置broker
cd/data/rocketmq/conf/2m-2s-sync
vimbroker-a.properties
#集群名字,识别标识,必须统一
brokerClusterName=Ty-rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样 例如:在a.properties 文件中写 broker-a 在b.properties 文件中写 broker-b
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=0
#nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3)
namesrvAddr=192.168.33.11:5432;192.168.33.12:5432;192.168.33.13:5432;192.168.33.14:5432#默认9876,修改只为验证配置文件
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数。由于是4个broker节点,所以设置为4
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=10911
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=80
#存储路径
storePathRootDir=/data/rocketmq/store
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/store/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/data/rocketmq/store/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq/store/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq/store/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq/store/abort
#限制的消息大小
maxMessageSize=65536
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
#编写service文件
vim/usr/lib/systemd/system/rocketmq.service
[Unit]
After=network-online.target
[Service]
#配置文件的名字,根据每个服务器的角色更改
ExecStart=/data/rocketmq/bin/mqbroker-c/data/rocketmq/conf/2m-2s-sync/broker-a.properties
StandardOutput=journal
StandardError=inherit
LimitNOFILE=65536
TimeoutStopSec=0
KillSignal=SIGTERM
SendSIGKILL=no
SuccessExitStatus=0
Restart=always
User=rocketmq
Group=rocketmq
WorkingDirectory=/data/rocketmq/
[Install]
WantedBy=multi-user.target
scp/usr/lib/systemd/system/rocketmq.service192.168.33.12:/usr/lib/systemd/system/
scp/usr/lib/systemd/system/rocketmq.service192.168.33.13:/usr/lib/systemd/system/
scp/usr/lib/systemd/system/rocketmq.service192.168.33.14:/usr/lib/systemd/system/
#修改日志目录,默认在用户家目录下
sed-i's#${user.home}#/data#g'/data/rocketmq/conf/*.xml
```
rocketqm-a-s节点
```
cd/data
useradd-s/sbin/nologinrocketmq
tarxvfrocketmq-4.5.2.tar.gz
ln-srocketmq-4.5.2rocketmq
mkdir-pv/data/rocketmq/store/{commitlog,consumequeue,index}
#配置namesrv,不配置没所谓
cd/data/rocketmq/conf
vimnamesrv.properties#默认没有这个文件,新建一个
listenPort=5432
#配置broker
cd/data/rocketmq/conf/2m-2s-sync
vimbroker-a-s.properties#仅三个地方不一样
brokerName=broker-a#名称需要和主节点保持一致
brokerId=1#ID=0为master,大于0为slave
brokerRole=SLAVE#配置为从
vim/usr/lib/systemd/system/rocketmq.service
ExecStart=/data/rocketmq/bin/mqbroker-c/data/rocketmq/conf/2m-2s-sync/broker-a-s.properties#修改配置文件为a-s
sed-i's#${user.home}#/data#g'/data/rocketmq/conf/*.xml
```
rocketqm-b节点
```
cd/data
tarxvfrocketmq-4.5.2.tar.gz
ln-srocketmq-4.5.2rocketmq
mkdir-pv/data/rocketmq/store/{commitlog,consumequeue,index}
#配置namesrv
cd/data/rocketmq/conf
vimnamesrv.properties#默认没有这个文件,新建一个
listenPort=5432
#配置broker
cd/data/rocketmq/conf/2m-2s-sync
vimbroker-b.properties#仅三个地方不一样
brokerName=broker-b#名称需要和主节点保持一致
brokerId=0#ID=0为master,大于0为slave
brokerRole=SYNC_MASTER#配置为主
vim/usr/lib/systemd/system/rocketmq.service
ExecStart=/data/rocketmq/bin/mqbroker-c/data/rocketmq/conf/2m-2s-sync/broker-b.properties#修改配置文件为a-s
sed-i's#${user.home}#/data#g'/data/rocketmq/conf/*.xml #日志路径
```
rocketqm-b-s节点
```
cd/data
tarxvfrocketmq-4.5.2.tar.gz
ln-srocketmq-4.5.2rocketmq
mkdir-pv/data/rocketmq/store/{commitlog,consumequeue,index}
#配置namesrv
cd/data/rocketmq/conf
vimnamesrv.properties#默认没有这个文件,新建一个
listenPort=5432
#配置broker
cd/data/rocketmq/conf/2m-2s-sync
vimbroker-b-s.properties#仅三个地方不一样
brokerName=broker-b#名称需要和主节点保持一致
brokerId=1#ID=0为master,大于0为slave
brokerRole=SLAVE#配置为从
vim/usr/lib/systemd/system/rocketmq.service
ExecStart=/bin/sh-c'/data/rocketmq/bin/mqbroker -c /data/rocketmq/conf/2m-2s-sync/broker-b-s.properties'#修改配置文件为a-s
sed-i's#${user.home}#/data/rocketmq#g'/data/rocketmq/conf/*.xml
```
四台服务器首先启动namesrv
rocketmq-a
```
vim/usr/lib/systemd/system/namesrv.service
[Unit]
After=network-online.target
[Service]
ExecStart=/data/rocketmq/bin/mqnamesrv
StandardOutput=journal
StandardError=inherit
LimitNOFILE=65536
TimeoutStopSec=0
KillSignal=SIGTERM
SendSIGKILL=no
SuccessExitStatus=0
Restart=always
User=rocketmq
Group=rocketmq
WorkingDirectory=/data/rocketmq/
[Install]
WantedBy=multi-user.target
systemctlenablenamesrv.service&&systemctlstartnamesrv.service
scp/usr/lib/systemd/system/namesrv.service192.168.33.12:/usr/lib/systemd/system/
scp/usr/lib/systemd/system/namesrv.service192.168.33.13:/usr/lib/systemd/system/
scp/usr/lib/systemd/system/namesrv.service192.168.33.14:/usr/lib/systemd/system/
```
其他几台服务器启动namesrv.service
```
systemctl enable namesrv.service
systemctlstartnamesrv.service
```
所有节点,修改broker内存使用
根据环境决定,这是本机测试内存较小使用的数值
```
vim/data/rocketmq/bin/runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m"
```
所有节点启动rocketmq.service
``
systemctlenablerocketmq&&systemctlstartrocketmq
```
rocketMq监控平台rocketmq-console高可用部署
rocketmq-a节点
高可用设想,在所有节点上部署rocketmq-console,使用F5进行调度实现高可用
```
cd/data
wgethttps://github.com/apache/rocketmq-externals/archive/rocketmq-console-1.0.0.tar.gz
tarxvfrocketmq-console-1.0.0.tar.gz
vim/data/rocketmq-externals-rocketmq-console-1.0.0/rocketmq-console/src/main/resources/application.properties
server.contextPath=
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.33.11:5432;192.168.33.12:5432;192.168.33.13:5432;192.168.33.14:5432#namesrv的地址ip:port;
#if you use rocketmq version < 3.5.8, rocketmq.config.isVIPChannel should be false.default true
rocketmq.config.isVIPChannel=#Rocket如果开启了VIP通道,VIP通道端口为10911-2=10909。若Rocket服务器未启动端口10909,则报connect to <:10909> failed。
#rocketmq-console's data path:dashboard/monitor
rocketmq.config.dataPath=/tmp/rocketmq-console/data
#set it false if you don't want use dashboard.default true
rocketmq.config.enableDashBoardCollect=true
#修改日志路径
vim/data/rocketmq-externals-rocketmq-console-1.0.0/rocketmq-console/src/main/resources/logback.xml
<file>/data/logs/consolelogs/rocketmq-console.log</file>
<fileNamePattern>/data/logs/consolelogs/rocketmq-console-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
cd/data/rocketmq-externals-rocketmq-console-1.0.0/rocketmq-console
mvncleanpackage-Dmaven.test.skip=true
#编译好的包:/data/rocketmq-externals-rocketmq-console-1.0.0/rocketmq-console/target/rocketmq-console-ng-1.0.0.jar
vim/usr/lib/systemd/system/rocketmq-console.service
[Unit]
After=network-online.target
[Service]
ExecStart=java-jar/data/rocketmq-externals-rocketmq-console-1.0.0/rocketmq-console/target/rocketmq-console-ng-1.0.0.jar
StandardOutput=journal
StandardError=inherit
LimitNOFILE=65536
TimeoutStopSec=0
KillSignal=SIGTERM
SendSIGKILL=no
SuccessExitStatus=0
Restart=always
User=rocketmq
Group=rocketmq
WorkingDirectory=/data//rocketmq-console/
[Install]
WantedBy=multi-user.target
systemctlenablerocketmq-console.service
systemctlstartrocketmq-console.service
```