RMQ的结构分为四个部分:生产者、消费者、nameserver、brokerserver
1.nameserver:接收broker的请求注册broker路由信息。收client的请求根据某个topic获取所有到broker的路由信息。
2.brokerserver:消息的接收和推送,
3.生产者:发送消息,将消息推送给brokerserver。
4.消费者:接收消息,从brokerserver上获取消息。
实战 : 架构示意图
一、准备工作
两台服务器同步安装:
192.168.10.231
192.168.10.232
1.0、安装jdk(1.8+)
1.1、安装maven(3.2.5)
1.2、安装git (1.7.1)
二、搭建rocketMq
2.1、安装rocketmq
下载源码: http://rocketmq.apache.org/release_notes/release-notes-4.3.0/
> cd /usr/local/src/
> wget https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.3.0/rocketmq-all-4.3.0-source-release.zip
> unzip rocketmq-all-4.3.0-source-release.zip
> cd rocketmq-all-4.3.0/
> mvn -Prelease-all -DskipTests clean install -U
> cd distribution/target/
> cp -r apache-rcketmq /usr/local/
2.2、环境变量设置
> vi /etc/profile
#最后一行加入
#set rocketmq
export ROCKETMQ_HOME=/usr/local/apache-rocketmq
export PATH=$PATH::$ROCKETMQ_HOME/bin
> source /etc /profile
#主机名设置,最后一行加入
> vi /etc/hosts
192.168.10.231 mqnameserver1
192.168.10.232 mqnameserver2
192.168.10.231 rocketmq-master1
192.168.10.232 rocketmq-master2
#在 Master1服务器上
> sed -i '/HOSTNAME/d' /etc/sysconfig/network
> echo 'HOSTNAME=rocketmq-master1' >> /etc/sysconfig/network
> hostname rocketmq-master1
#在 Master2服务器上
> sed -i '/HOSTNAME/d' /etc/sysconfig/network
> echo 'HOSTNAME=rocketmq-master2' >> /etc/sysconfig/network
> hostname rocketmq-master2
2.3、配置文件设置
#在 Master1服务器上
> vi /usr/local/apache-rocketmq/conf/2m-noslave/broker-a.properties
brokerClusterName=zhsydCluster
brokerName=broker-a
brokerId=0
namesrvAddr=mqnameserver1:9876;mqnameserver2:9876
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
sendMessageThreadPoolNums=128
pullMessageThreadPoolNums=128
storePathRootDir=/data/apache-rocketmq/store
storePathCommitLog=/data/apache-rocketmq/store/commitlog
#然后创建目录:
> mkdir -p /data/apache-rocketmq/store/commitlog /data/logs
> cd /usr/local/apache-rocketmq/conf && sed -i 's#${user.home}#/data#g' *.xml
#在 Master2服务器上
> vi /usr/local/apache-rocketmq/conf/2m-noslave/broker-b.properties
brokerClusterName=zhsydCluster
brokerName=broker-b
brokerId=0
namesrvAddr=mqnameserver1:9876;mqnameserver2:9876
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
sendMessageThreadPoolNums=128
pullMessageThreadPoolNums=128
storePathRootDir=/data/apache-rocketmq/store
storePathCommitLog=/data/apache-rocketmq/store/commitlog
#然后创建目录:
> mkdir -p /data/apache-rocketmq/store/commitlog /data/logs
> cd /usr/local/apache-rocketmq/conf && sed -i 's#${user.home}#/data#g' *.xml
2.4、启动服务
runbroker.sh需要根据内存大小进行适当地调整
JAVA_OPT_1="-server-Xms2g -Xmx2g -Xmn1g -XX:PermSize=1g -XX:MaxPermSize=1g"
#(1)、启动NameServer【master1、master2】
> cd /usr/local/apache-rocketmq/bin && nohup sh mqnamesrv &
#(2)、启动BrokerServer A【master1】
> cd /usr/local/apache-rocketmq/bin
> nohup sh mqbroker -c ../conf/2m-noslave/broker-a.properties >/dev/null 2>&1 &
#(3)、启动BrokerServer B【master2】
> cd /usr/local/apache-rocketmq/bin
> nohup sh mqbroker -c ../conf/2m-noslave/broker-b.properties >/dev/null 2>&1 &
> netstat -ntlp
> jps
三、RocketMQ服务健康监控
3.1、依赖组件安装
> yum -y install nmap
3.2、nameserver监控【master1、master2】
> mkdir -p /data/scripts
> vi /data/scripts/check_nameserver_health.sh
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
SRV_PORT="9876" ## 端口号
SRV_PROT="tcp" ## 协议类型
SRV_NAME="rocketmq_nameserver" ## 服务名
## 是否已正确扫描
SCAN_FLAG=0
for ETH_NAME in `/sbin/ifconfig | grep eth1 | awk '{print $1}'`
do
ETH1_ADDR=`/sbin/ifconfig ${ETH_NAME} | awk -F ':' '/inet addr/{print $2}' | sed 's/[a-zA-Z ]//g'`
[[ -z "${ETH1_ADDR}" ]] && continue || break
done
TMP_SRV_PROT=`echo ${SRV_PROT} | tr '[A-Z]' '[a-z]'`
if [[ "${TMP_SRV_PROT}" == "tcp" ]]; then
PROT_OPT="S"
elif [[ "${TMP_SRV_PROT}" == "udp" ]]; then
PROT_OPT="U"
else
echo "未知的协议类型!" && exit1
fi
## 最多扫描3次,成功一次即可,以避免网络抖动而导致误判
for ((i=0; i<3; i++)); do
RETVAL=`/usr/bin/nmap -n -s${PROT_OPT} -p ${SRV_PORT} ${ETH1_ADDR} | grep open`
[[ -n "${RETVAL}" ]] && SCAN_FLAG=1;break || sleep 10
done
if [[ ${SCAN_FLAG} -ne 1 ]]; then
[[ -n `ps aux | grep java | grep namesrv` ]] && kill -9 `ps aux | grep java | awk '/namesrv/{print $2}'`
cd /usr/local/apache-rocketmq/bin && nohup sh mqnamesrv &
fi
3.3、brokerserver监控
> vi /data/scripts/check_brokerserver_health.sh
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
SRV_PORT="10911" ## 端口号
SRV_PROT="tcp" ## 协议类型
SRV_NAME="rocketmq_brokerserver" ## 服务名
## 是否已正确扫描
SCAN_FLAG=0
for ETH_NAME in `/sbin/ifconfig | grep eth1 | awk '{print $1}'`
do
ETH1_ADDR=`/sbin/ifconfig ${ETH_NAME} | awk -F ':' '/inet addr/{print $2}' | sed 's/[a-zA-Z ]//g'`
[[ -z "${ETH1_ADDR}" ]] && continue || break
done
TMP_SRV_PROT=`echo ${SRV_PROT} | tr '[A-Z]' '[a-z]'`
if [[ "${TMP_SRV_PROT}" == "tcp" ]]; then
PROT_OPT="S"
elif [[ "${TMP_SRV_PROT}" == "udp" ]]; then
PROT_OPT="U"
else
echo "未知的协议类型!" && exit1
fi
## 最多扫描3次,成功一次即可,以避免网络抖动而导致误判
for ((i=0; i<3; i++)); do
RETVAL=`/usr/bin/nmap -n -s${PROT_OPT} -p ${SRV_PORT} ${ETH1_ADDR} | grep open`
[[ -n "${RETVAL}" ]] && SCAN_FLAG=1;break || sleep 10
done
if [[ ${SCAN_FLAG} -ne 1 ]]; then
[[ -n `ps aux | grep java | grep broker` ]] && kill -9 `ps aux | grep java | awk '/broker/{print $2}'`
cd /usr/local/apache-rocketmq/bin && nohup sh mqbroker -c ../conf/2m-noslave/broker-a.properties &
fi
【注意】
以上broker-a.properties部分,在master2上的名称为“broker-b.properties”
3.4、crontab信息添加
> touch /var/run/check_rocketmq_nameserver.lock
> touch /var/run/check_rocketmq_brokerserver.lock
> echo 'touch /var/run/check_rocketmq_nameserver.lock' >> /etc/rc.d/rc.local
> echo 'touch /var/run/check_rocketmq_brokerserver.lock' >> /etc/rc.d/rc.local
> crontab -e
*/2 * * * * (flock --timeout=0 /var/run/check_rocketmq_nameserver.lock /data/scripts/check_nameserver_health.sh >/dev/null 2>&1)
*/2 * * * * (flock --timeout=0 /var/run/check_rocketmq_brokerserver.lock /data/scripts/check_brokerserver_health.sh >/dev/null 2>&1)
3.5、rocketmq-console 安装
地址: https://github.com/apache/rocketmq-externals/tree/master/rocketmq-console
> cd /usr/local/src/
> git clone https://github.com/apache/rocketmq-externals.git
> cd rocketmq-externals/rocketmq-console/
> vi src/main/resources/application.properties
server.contextPath=
server.port=9898
#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.10.231:9876;192.168.10.232:9876
#if you use rocketmq version < 3.5.8, rocketmq.config.isVIPChannel should be false.default true
rocketmq.config.isVIPChannel=
#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
#mvn 构建:
> mvn clean package -Dmaven.test.skip=true
> java -jar target/rocketmq-console-ng-1.0.0.jar
访问 : http://192.168.10.231:9898
四、RocketMQ测试使用
4.1、生产者
4.2、消费者
五、RocketMQ代码集成
5.1、修改配置文件
queue-config.properties
application-queue-producer.xml
application-queue-consumer.xml
六、附录
6.1、参考文档
https://rocketmq.incubator.apache.org/docs/quick-start/
https://github.com/apache/incubator-rocketmq
http://sofar.blog.51cto.com/353572/1540874
6.2、windows版安装
6.2.1、下载maven和git,安装较简单
Maven: http://maven.apache.org/download.cgi
Git : https://git-scm.com/download/win
6.2.2、编译
> git clone -b develop https://github.com/apache/rocketmq.git
> cd rocketmq
> mvn -Prelease-all -DskipTests clean install -U
> cd distribution/target/
#将编译好的apache-rcketmq 目录拷贝到任意位置(例如D:\)
6.2.3、配置
设置环境变量 (同java配置一样)
a.新建 : ROCKETMQ_HOME
b.path末尾加入 : ;%ROCKETMQ_HOME%bin
配置 runbroker.cmd
set "JAVA_OPT=%JAVA_OPT% -server -Xms1g -Xmx1g -Xmn512m"
修改倒数第二行(bug)
set "JAVA_OPT=%JAVA_OPT% -cp "%CLASSPATH%""
修改 D:apache-rocketmqconf 加入
namesrvAddr=192.168.20.211:9876
6.2.4、运行
进入 D:/apache-rocketmq/bin
启动 mqnamesrv: mqnamesrv
启动 mqbroker: mqbroker -n 192.168.20.211:9876
查看是否成功:jps
版权属于:sunjianhua
本文链接:https://sunjianhua.cn/archives/centos-rocketmq.html
转载时须注明出处及本声明,如果不小心侵犯了您的权益,请联系邮箱:NTA2MTkzNjQ1QHFxLmNvbQ==