RocketMQ部署
RocketMQ的部署
部署方式
单Master模式
只有一个 Master节点
优点:配置简单,方便部署
缺点:这种方式风险较大,一旦Broker重启或者宕机时,会导致整个服务不可用,不建议线上环境使用
多Master模式
一个集群无 Slave,全是 Master,例如 2 个 Master 或者 3 个 Master
优点:配置简单,单个Master 宕机或重启维护对应用无影响,在磁盘配置为RAID10 时,即使机器宕机不可恢复情况下,由于 RAID10磁盘非常可靠,消息也不会丢(异步刷盘丢失少量消息,同步刷盘一条不丢)。性能最高。多 Master 多 Slave 模式,异步复制
缺点:单台机器宕机期间,这台机器上未被消费的消息在机器恢复之前不可订阅,消息实时性会受到受到影响
多Master多Slave模式(异步复制)---本文稍后以这种方式部署集群为例
每个 Master 配置一个 Slave,有多对Master-Slave, HA,采用异步复制方式,主备有短暂消息延迟,毫秒级。
优点:即使磁盘损坏,消息丢失的非常少,且消息实时性不会受影响,因为Master 宕机后,消费者仍然可以从 Slave消费,此过程对应用透明。不需要人工干预。性能同多 Master 模式几乎一样。
缺点: Master 宕机,磁盘损坏情况,会丢失少量消息。
多Master多Slave模式(同步双写)---文中会说明补充此集群配置,线上使用的话,推荐使用此模式集群
每个 Master 配置一个 Slave,有多对Master-Slave, HA采用同步双写方式,主备都写成功,向应用返回成功。
优点:数据与服务都无单点, Master宕机情况下,消息无延迟,服务可用性与数据可用性都非常高
缺点:性能比异步复制模式略低,大约低 10%左右,发送单个消息的 RT会略高。目前主宕机后,备机不能自动切换为主机,后续会支持自动切换功能
单机模式部署
2. 下载相关软件
3. 配置环境
3.1. 解压RocketMQ
unzip /usr/local/rocketmq-all-4.4.0-bin-release.zip -d /usr/local/rocketmq
3.2. 配置环境变量
vi /etc/profile
export JAVA_HOME=/opt/soft/jdk1.8.0_231
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export MAVEN_HOME=/opt/soft/apache-maven-3.5.4
export PATH=$PATH:$MAVEN_HOME/bin
export ROCKETMQ_HOME=/usr/local/rocketmq/rocketmq-all-4.4.0-bin-release
export PATH=$ROCKETMQ_HOME/bin:$PATH
或者使用追加的命令
cat >> /etc/profile <<EOF
export SCAFFOLD_DB_HOST=cdh1
export SCAFFOLD_DB_USER=root
export SCAFFOLD_DB_PSW=wTJJJJZZZ@SSDDC
export SCAFFOLD_REDIS_HOST=cdh1
export SCAFFOLD_REDIS_PSW=123456
export SCAFFOLD_EUREKA_ZONE_HOSTS=http://cdh1:7777/eureka/
export SCAFFOLD_ROCKETMQ_HOSTS=172.26.9.107:2181
export SCAFFOLD_ZOOKEEPER_HOSTS=cdh1:2181
export LC_ALL=en_US.UTF-8
export ROCKETMQ_HOME=/usr/local/rocketmq/rocketmq-all-4.4.0-bin-release
export JAVA_HOME=/usr/local/java/jdk1.8.0_121
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$ROCKETMQ_HOME/bin:$JAVA_HOME/bin:/usr/bin:/usr/local/mysql/bin:/root/bin
EOF
3.3. 刷新配置文件
source /etc/profile
3.4. 验证环境是否正确
java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
4. RocketMQ启动和关闭
4.1 创建数据存储目录
mkdir -p /usr/local/rocketmq/data
4.2 创建日志目录
mkdir -p /usr/local/rocketmq/logs
4.3 修改broker.conf文件
vim /usr/local/rocketmq/rocketmq-all-4.4.0-bin-release/conf/broker.conf
修改内容如下:
# 所属集群名字(同一主从下: Master 和 Slave 名称要一致)
brokerClusterName = DefaultCluster
# Broker 名字,注意此处不同的配置文件填写的不一样
brokerName = broker-a
# 0 表示 Master,> 0 表示 Slave
brokerId = 0
# Broker 对外服务的监听端口, 如果一台机器上启动了多个Broker,则要设置不同的端口号,避免冲突
listenPort = 10911
# nameServer地址,如果nameServer是多台集群的话,就用分号分割,比如
# namesrvAddr = 192.168.0.1:9876;192.168.0.2:9876
# namesrvAddr = 127.0.0.1:9876
namesrvAddr = 192.168.56.121:9876
brokerIP1 = 192.168.56.121
# 是否允许 Broker 自动创建Topic,建议线上关闭
autoCreateTopicEnable = true
# 是否允许 Broker 自动创建订阅组,建议线上关闭
autoCreateSubscriptionGroup = true
# 与fileReservedTime参数呼应,表明在几点做消息删除动作,默认值04表示凌晨4点
deleteWhen = 04
# 在磁盘上保存消息的时长,单位是小时,自动删除超时的消息
fileReservedTime = 48
# brokerRole有3种:SYNC_MASTER、ASYNC_MASTER、SLAVE
# 关键词 SYNC 和 ASYNC 表示 Master 和 Slave 之间同步消息的机制
# SYNC 的意思是当 Slave 和 Master 消息同步完成后,再返回发送成功的状态
brokerRole = ASYNC_MASTER
# 刷盘方式 ASYNC_FLUSH 异步刷盘; SYNC_FLUSH 同步刷盘
flushDiskType = ASYNC_FLUSH
#数据存储位置
storePathRootDir = /usr/local/rocketmq/data
storePathCommitLog = /usr/local/rocketmq/logs
4.4 修改启动脚本JVM内存大小
4.4.1 vim /usr/local/rocketmq/rocketmq-all-4.4.0-bin-release/bin/runserver.sh
JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m"
4.4.2 vim /usr/local/rocketmq/rocketmq-all-4.4.0-bin-release/bin/runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn128m"
4.5 启动namesrv 和 broker
[root@master bin]# nohup mqnamesrv -n 192.168.56.121:9876 &
[1] 2937
[root@master bin]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@master bin]# nohup sh mqbroker -n 192.168.56.121:9876 autoCreateTopicEnable=true -c /usr/local/rocketmq/rocketmq-all-4.4.0-bin-release/conf/broker.conf &
[2] 2968
[root@master bin]# nohup: 忽略输入并把输出追加到"nohup.out"
# 查看broker启动配置:
[root@master bin]# sh mqbroker -m
[root@master bin]# jps
3030 Jps
2940 NamesrvStartup
2972 BrokerStartup
命令汇总
mqnamesrv -n 172.26.9.107
nohup mqnamesrv -n 172.26.9.107 &
nohup sh mqbroker -n 172.26.9.107:9876 autoCreateTopicEnable=true -c
nohup sh mqbroker -n 172.26.9.107:9876 autoCreateTopicEnable=true -c /usr/local/rocketmq/rocketmq-all-4.4.0-bin-release/conf/broker.conf &
mqbroker -m
jps
设置开机启动
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
#start redis
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
#start nginx
#/usr/bin/su - root -c "/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf"
# 启动 zookeeper
/usr/bin/su - root -c "/work/zookeeper/zookeeper_01/bin/zkServer.sh start"
sleep 5s
/usr/bin/su - root -c "/work/zookeeper/zookeeper_02/bin/zkServer.sh start"
# 启动 Nacos
/usr/bin/su - root -c "/work/nacos/bin/startup.sh -m standalone"
#start springcloud
sleep 10s
/usr/bin/su - root -c "/work/cloud-eureka-1.0-SNAPSHOT/bin/start.sh start"
sleep 20s
/usr/bin/su - root -c "/work/cloud-config-1.0-SNAPSHOT/bin/start.sh start"
# 启动 kafka
#/usr/bin/su - root -c "nohup /work/kafka_2.11-1.0.2/bin/kafka-server-start.sh /work/kafka_2.11-1.0.2/config/server.properties 2>&1 &"
#启动 sentinel
/usr/bin/su - root -c "nohup java -server -Xms64m -Xmx256m -Dserver.port=8849 -Dcsp.sentinel.dashboard.server=localhost:8849 -Dproject.name=sentinel-dashboard -jar /work/sentinel-dashboard-1.7.1.jar 2>&1 &"
sleep 2s
# 启动 rocketmq namenode
#/usr/bin/su - root -c "nohup mqnamesrv -n 192.168.56.121:9876 &"
sleep 30s
# 启动 rocketmq broker
#/usr/bin/su - root -c "nohup sh mqbroker -n 192.168.56.121:9876 autoCreateTopicEnable=true -c /usr/local/rocketmq/rocketmq-all-4.4.0-bin-release/conf/broker.conf &"
4.6 查看启动日志记录
tail -200f ~/logs/rocketmqlogs/namesrv.log
tail -200f ~/logs/rocketmqlogs/broker.log
4.7 消息发送和消费测试
# 设置NameServer地址
export NAMESRV_ADDR=192.168.56.121:9876
/usr/local/rocketmq/rocketmq-all-4.4.0-bin-release/distribution/target/apache-rocketmq
# 测试发送端
sh tools.sh org.apache.rocketmq.example.quickstart.Producer
# 测试消费端
sh tools.sh org.apache.rocketmq.example.quickstart.Consumer
4.8 关闭namesrv 和 broker
[root@master bin]# sh mqshutdown broker
The mqbroker(2972) is running...
Send shutdown request to mqbroker(2972) OK
[root@master bin]# sh mqshutdown namesrv
The mqnamesrv(2940) is running...
Send shutdown request to mqnamesrv(2940) OK
5. rocketmq-console安装和使用
5.1 从github上clone代码
git clone https://github.com/apache/rocketmq-externals.git
5.2 用Maven编译源码
rocketmq-externals/rocketmq-console
mvn clean package -Dmaven.test.skip=true
5.3 生成的jar文件目录
rocketmq-externals/rocketmq-console/target/rocketmq-console-ng-2.0.0.jar
5.4 执行jar文件
复制到/usr/local/rocketmq/,然后执行:
java -jar /usr/local/rocketmq/rocketmq-console-ng-2.0.0.jar --rocketmq.config.namesrvAddr='192.168.56.121:9876'
5.5 关闭防火墙
# 关闭防火墙
systemctl stop firewalld.service
# 查看防火墙状态
firewall-cmd --state
5.6 访问链接
http://192.168.56.121:8080/#/

集群模式部署
此处就RocketMQ的多Master多Slave的模式在Linux服务器部署案例进行详细的说明,如系统部署结构图所示。
1本次部署环境
Linux服务器192.168.162.235、192.168.162.236两台(下文均简称235、236),详细部署环境示意表如下:


2 编辑Hosts
分别修改235 和236 的hosts 文件
sudo vim /etc/hosts
IP NAME
192.168.162.235 nameserver1
192.168.162.235 master1
192.168.162.235 master1-slave1
192.168.162.236 nameserver2
192.168.162.236 master2
192.168.162.236 master2-slave2
注:修改hosts 文件需获得sudo 权限,本机用户是rocketMQ非root用户, 故申请了堡垒机权限(即获得root权限)。
3 下载官方源码
下载官方RocketMQ压缩包,下载地址:http://rocketmq.apache.org/release_notes/release-notes-4.2.0/,并选择Download the 4.2.0 release 选项的 rocketmq-all-4.2.0-bin-release.zip下载。(其他如source为需要自己编译的版本)
4 上传到Linux并解压
分别上传rocketmq-all-4.2.0-bin-release.zip到235和236服务器的/home/rocketMQ/ZHF/rocketMQ-2m2s/目录下:
cd /home/rocketMQ/ZHF/rocketMQ-2m2s/tar –zxvf rocketmq-all-4.2.0-bin-release.zip
- 创建持久化存储目录
Master目录设置:
mkdir /home/rocketMQ/ZHF/rocketMQ-2m2s/store
mkdir /home/rocketMQ/ZHF/rocketMQ-2m2s/store/commitlog
mkdir /home/rocketMQ/ZHF/rocketMQ-2m2s/store/consumequeue
mkdir /home/rocketMQ/ZHF/rocketMQ-2m2s/store/index
Slave目录设置:
mkdir /home/rocketMQ/ZHF/rocketMQ-2m2s/store-s
mkdir /home/rocketMQ/ZHF/rocketMQ-2m2s/store-s/commitlog
mkdir /home/rocketMQ/ZHF/rocketMQ-2m2s/store-s/consumequeue
mkdir /home/rocketMQ/ZHF/rocketMQ-2m2s/store-s/index
6 RocketMQ配置文件
235服务器设置:
sudo vim /home/rocketMQ/ZHF/rocketMQ-2m2s/conf/2m-2s-async/broker-a.properties
sudo vim /home/rocketMQ/ZHF/rocketMQ-2m2s/conf/2m-2s-async/broker-b-s.properties
236服务器设置:
sudo vim /home/rocketMQ/ZHF/rocketMQ-2m2s/conf/2m-2s-async/broker-b.properties
sudo vim /home/rocketMQ/ZHF/rocketMQ-2m2s/conf/2m-2s-async/broker-a-s.properties
broker-a.properties文件配置
# 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
#brokerId=0
#deleteWhen=04
#fileReservedTime=48
#brokerRole=ASYNC_MASTER
#flushDiskType=ASYNC_FLUSH
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=0
#nameServer地址,分号分割
namesrvAddr=nameserver1:9876;nameserver2:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=10911
haListenPort=10912
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=18
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/home/rocketMQ/ZHF/rocketMQ-2m2s/store
#commitLog 存储路径
storePathCommitLog=/home/rocketMQ/ZHF/rocketMQ-2m2s/store/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/home/rocketMQ/ZHF/rocketMQ-2m2s/store/consumequeue
#消息索引存储路径
storePathIndex=/home/rocketMQ/ZHF/rocketMQ-2m2s/store/index
#checkpoint 文件存储路径
storeCheckpoint=/home/rocketMQ/ZHF/rocketMQ-2m2s/store/checkpoint
#abort 文件存储路径
abortFile=/home/rocketMQ/ZHF/rocketMQ-2m2s/store/abort
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
#强制指定本机IP,需要根据每台机器进行修改。官方介绍可为空,系统默认自动识别,但多网卡时IP地址可能读取错误
brokerIP1=192.168.162.235
broker-a-s.properties文件配置
# 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
#brokerId=1
#deleteWhen=04
#fileReservedTime=48
#brokerRole=SLAVE
#flushDiskType=ASYNC_FLUSH
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=1
#nameServer地址,分号分割
namesrvAddr=nameserver1:9876;nameserver2:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=10923
haListenPort=10924
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=18
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/home/rocketMQ/ZHF/rocketMQ-2m2s/store-s
#commitLog 存储路径
storePathCommitLog=/home/rocketMQ/ZHF/rocketMQ-2m2s/store-s/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/home/rocketMQ/ZHF/rocketMQ-2m2s/store-s/consumequeue
#消息索引存储路径
storePathIndex=/home/rocketMQ/ZHF/rocketMQ-2m2s/store-s/index
#checkpoint 文件存储路径
storeCheckpoint=/home/rocketMQ/ZHF/rocketMQ-2m2s/store-s/checkpoint
#abort 文件存储路径
abortFile=/home/rocketMQ/ZHF/rocketMQ-2m2s/store-s/abort
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushConsumeQueueLeastPages=2#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SLAVE
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
#强制指定本机IP,需要根据每台机器进行修改。官方介绍可为空,系统默认自动识别,但多网卡时IP地址可能读取错误
brokerIP1=192.168.162.235
broker-b.properties文件配置
参考broker-a.properties
broker-b-s.properties文件配置
参考broker-a-s.properties
7 启动参数设置
RocketMQ启动文件位于/home/rocketMQ/ZHF/rocketMQ-2m2s/bin/目录下,Linux中nameserver启动文件为:mqnamesrv,broker启动文件为:mqbroker,mqnamesrv和mqbroker启动文件分别调用了runserver.sh和runbroker.sh文件,这两个文件分别设置了nameserver和broker的启动内存,目前内存启动参数分别为nameserver启动内存4G,最大内存4G,新生代2G,broker启动内存8G,最大内存8G,新生代4G。
8 端口及防火墙设置
RokcetMQ启动默认使用3个端口9875,10911,10912,三个端口分别代表nameserver服务器端口,broker端口,broker HA端口。需注意的是在多Master多Slave模式下10911和10912是Master的使用端口,但Slave端口的设置与Master的端口不同,具体端口约束为:Slave - Master > 2,否则可能导致同一台服务器无法同时启动Master和Slave。
如果服务器启动了防火墙,为了端口不被屏蔽,需将Master和Slave对应端口加入到iptables表以开放对应端口号,添加完成后重启防火墙。命令行开放端口操作如下:
分别打开235和236终端,在root用户下执行命令:
开放端口:
/sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 9876 -j ACCEPT
/sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 10911 -j ACCEPT
/sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 10912 -j ACCEPT
/sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 10923 -j ACCEPT
/sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 10924 -j ACCEPT
保存:
/etc/rc.d/init.d/iptables save
重启:
/etc/init.d/iptables restart
查看端口开放情况:
/sbin/iptables -L -n
9 启动Nameserver
分别启动235、236的Nameserver
cd /home/rocketMQ/ZHF/rocketMQ-2m2s/bin/nohup sh mqnamesrv &
10.启动Broker
235上Master启动:
nohup sh mqbroker -c /home/rocketMQ/ZHF/rocketMQ-2m2s/conf/2m-2s-async/broker-a.properties
236上Master启动:
nohup sh mqbroker -c /home/rocketMQ/ZHF/rocketMQ-2m2s/conf/2m-2s-async/broker-b.properties
235上对应236Master的Slave启动:
nohup sh mqbroker -c /home/rocketMQ/ZHF/rocketMQ-2m2s/conf/2m-2s-async/broker-b-s.properti
236上对应235Master的Slave启动:
nohup sh mqbroker -c /home/rocketMQ/ZHF/rocketMQ-2m2s/conf/2m-2s-async/broker-a-s.properti
至此,Nameserver、Broker启动完成,可以用jobs命令查看当前运行进程,如下是服务端相关shutdown,即在bin目录下:
sh mqshutdown namesrvsh
sh mqshutdown broker
六、RocketMQ监控平台部署
Apache版的RocketMQ管理界面部署工具可以从github上下载源码,地址:https://github.com/apache/rocketmq-externals,部署流程如下:
- 修改配置文件,关联rocketMQ集群到管理界面
首先解压并进入解压后rockemq-externals-master目录rocketmq-externals-master/rocketmq-externals-master/rocketmq-console/src/main/resources,修改目录下application.properties配置文件内容如下图:

- 编译rocketmq-console
mvn clean package -Dmaven.test.skip=true
编译需用maven命令进行编译,如下图,显示BIUD SUCCESS,则编译成功,成功后会在rocketmq-externals-master/rocketmq-console/target目录下产生一个rocketmq-console-ng-1.0.0.jar文件。

- 将编译好的rocketmq-console-ng-0.0.jar包上传linux服务器
这里上传服务器地址为192.168.162.235,路径为:/home/rocketMQ/ZHF/
- 运行jar包
java -jar target/rocketmq-console-ng-1.0.0.jar
运行显示下图则启动成功:
- 访问管理界面
浏览器输入http://192.168.162.235:8080/回车显示监控界面如下:

6. Java示例代码
Consumer
package com.zy.study01;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import java.util.List;
public class Consumer {
public static void main(String[] args) {
/**
* Consumer Group,非常重要的概念,后续会慢慢补充
*/
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_demo");
//指定NameServer地址,多个地址以 ; 隔开
consumer.setNamesrvAddr(Contents.CDH_1_9876); //修改为自己的
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
try {
consumer.subscribe(Contents.TOPIC_TEST, "*");
} catch (MQClientException e) {
e.printStackTrace();
return;
}
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
for (int i = 0; i < msgs.size(); i++) {
MessageExt msg = msgs.get(i);
System.out.println(msg.getTopic() + " " + msg.getTags() + " " + new String(msg.getBody()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
try {
consumer.start();
} catch (MQClientException e) {
e.printStackTrace();
}
}
}
Producer
package com.zy.study01;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
public class Producer {
public static void main(String[] args) {
DefaultMQProducer producer = new DefaultMQProducer("producer_demo");
//指定NameServer地址
producer.setNamesrvAddr(Contents.CDH_1_9876); //修改为自己的
producer.setInstanceName("Instance1");
producer.setRetryTimesWhenSendFailed(3);
/**
* Producer对象在使用之前必须要调用start初始化,初始化一次即可
* 注意:切记不可以在每次发送消息时,都调用start方法
*/
try {
producer.start();
System.out.println("product start ...");
} catch (MQClientException e) {
e.printStackTrace();
}
try {
//构建消息
Message msg = new Message(Contents.TOPIC_TEST /* Topic */, "TagA" /* Tag */,
("hello rocket").getBytes(RemotingHelper.DEFAULT_CHARSET));
//发送同步消息
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("发送完成");
}
}


浙公网安备 33010602011771号