首页 > 代码库 > linux下搭建rocketmq(附服务脚本)
linux下搭建rocketmq(附服务脚本)
此文档安装的是rocketmq当前最新版本rocketmq v4.0.0,当时阿里已经把rocketmq捐赠给了apache组织,为什么要特别指明这一点,因为在下载、安装和服务脚本中有个别不同的地方。这是本人第一篇博客,如果排版不是很清楚,请评论中指明,以纠正自己的编写习惯。
一、安装rocketmq
1、安装要求
64bit OS, Linux/Unix/Mac is recommended; 64bit JDK 1.7+; Maven 3.2.x; Git
2、克隆和编译
> git clone -b develop https://github.com/apache/incubator-rocketmq.git 会在当前目录下生成“incubator-rocketmq”目录,接下来的部分操作都是在这个目录下进 > cd incubator-rocketmq > mvn -Prelease-all -DskipTests clean install -U 此步骤时间会比较长,请耐心等待,执行成功会生成多个目录。这是编译过程,我们后续操作只需要在编译过程生成的distribution/target/apache-rocketmq目录中进行,通常为了方便,我们把这个目录拷贝出来。 > cd distribution/target/apache-rocketmq
此时需要注意的是:启动的时候先启动Name server 停止的时候先停止Broker
3、尝试启动"Name Server"
> nohup sh bin/mqnamesrv & > tail -f ~/logs/rocketmqlogs/namesrv.log 日志文件会自动生成 The Name Server boot success…
4、尝试启动"Broker"
> nohup sh bin/mqbroker -n localhost:9876 & > tail -f ~/logs/rocketmqlogs/broker.log The broker[%s, 172.30.30.233:10911] boot success...
5、停止"Broker"服务
> sh bin/mqshutdown broker The mqbroker(36695) is running... Send shutdown request to mqbroker(36695) OK
6、停止"Name Server"服务
> sh bin/mqshutdown namesrv The mqnamesrv(36664) is running... Send shutdown request to mqnamesrv(36664) OK
在测试完成之后,说明安装成功,一切都能正常运行,此安装过程是摘自官方文档,有何异议请移步官方文档 https://rocketmq.incubator.apache.org/
二、配置mqbroker
1、 通常我们使用是以集群模式运行Broker
Broker集群有多种配置方式: 1,单Master 优点:除了配置简单没什么优点 缺点:不可靠,该机器重启或宕机,将导致整个服务不可用 2,多Master 优点:配置简单,性能最高 缺点:可能会有少量消息丢失(配置相关),单台机器重启或宕机期间,该机器下未被消费的消息在机器恢复前不可订阅,影响消息实时性 3,多Master多Slave,每个Master配一个Slave,有多对Master-Slave,集群采用异步复制方式,主备有短暂消息延迟,毫秒级 优点:性能同多Master几乎一样,实时性高,主备间切换对应用透明,不需人工干预 缺点:Master宕机或磁盘损坏时会有少量消息丢失 4,多Master多Slave,每个Master配一个Slave,有多对Master-Slave,集群采用同步双写方式,主备都写成功,向应用返回成功 优点:服务可用性与数据可用性非常高 缺点:性能比异步集群略低,当前版本主宕备不能自动切换为主
2、配置文件默认存在的是
[root@mycat conf]# ls 2m-2s-async 2m-noslave broker.conf.bak logback_filtersrv.xml logback_tools.xml 2m-2s-sync brk.conf logback_broker.xml logback_namesrv.xml
其中m表示主节点,s表示从节点,以哪种集群形式运行,去配置文件中修改即可,之后在服务脚本中调用就行了。这里我们只是运行单节点broker,具体的集群模式请再自行琢磨,把默认的broker.conf 做备份,并复制一份配置文件出来,重命名为brk.conf 这个名字是随意起的,建议见名知意,方便在脚本中调用。
3、# cat brk.conf
brokerClusterName = onebroker brokerName = broker-a brokerId = 0 0表示master,1表示slave deleteWhen = 04 fileReservedTime = 48 brokerRole = ASYNC_MASTER flushDiskType = ASYNC_FLUSH namesrvAddr = mycat:9876 指明mqname server storePathRootDir = /data/rocketmq/store storePathCommitLog = /data/rocketmq/store/commitlog autoCreateTopicEnable = true autoCreateSubscriptionGroup = true
4、通常
/data/rocketmq/store和/data/rocketmq/store/commitlog是需要手动创建的
为了安全起见,我们使用mq用户启动和关闭服务
# useradd mq # chown -R mq:mq /data/rocketmq/ # chown -R mq:mq /opt/apache-rocketmq
三、提供服务脚本
# cat /etc/init.d/mqnamesrv
#!/bin/bash # chkconfig: 2345 55 25 # description:MQ manmger . /etc/rc.d/init.d/functions start() { status if [ $? -ne 0 ] then echo ‘start rocketmq-namserv......‘ #####################################需要修改的就是下面一行######################################### su - mq -c "nohup /opt/apache-rocketmq/bin/mqnamesrv &> /data/rocketmq/logs/mq_namser.log &" if [ $? -eq 0 ];then echo "started......" fi fi } stop() { pid=`ps ax | grep -i ‘org.apache.rocketmq.namesrv.NamesrvStartup‘ |grep java | grep -v grep | awk ‘{print $1}‘` if [ -z "$pid" ] ; then echo "No mqnamesrv running." return 2; else echo "The mqnamesrv(${pid}) is running..." kill ${pid} echo "Send shutdown request to mqnamesrv(${pid}) OK" fi } restart() { stop start } status() { pid=`ps ax | grep -i ‘org.apache.rocketmq.namesrv.NamesrvStartup‘ |grep java | grep -v grep | awk ‘{print $1}‘` if [ -z "$pid" ] ; then echo "No mqnamesrv running." return 1; fi echo "The mqnamesrv(${pid}) is running..." return 0 } case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) echo $"Usage: $0 {start|stop|restart|tatus}" esac
# cat /etc/init.d/mqbroker
#!/bin/bash # chkconfig: 2345 95 25 # description:MQ manmger . /etc/rc.d/init.d/functions start() { pid=`ps ax | grep -i ‘org.apache.rocketmq.broker.BrokerStartup‘ |grep java | grep -v grep | awk ‘{print $1}‘` if [ -z "$pid" ] ; then ###调用脚本启动mq-master-1.properties echo ‘start rocketmq-broker......‘ su - mq -c "nohup /opt/apache-rocketmq/bin/mqbroker -c /opt/apache-rocketmq/conf/brk.conf &> /data/rocketmq/logs/mqbroker.log &" sleep 1 newpid=`ps ax | grep -i ‘org.apache.rocketmq.broker.BrokerStartup‘ |grep java | grep -v grep | awk ‘{print $1}‘` echo "restart mqbroker(${newpid}) is running success..." else echo "The mqbroker(${pid}) is running..." fi } stop() { pid=`ps ax | grep -i ‘org.apache.rocketmq.broker.BrokerStartup‘ |grep java | grep -v grep | awk ‘{print $1}‘` if [ -z "$pid" ] ; then echo "No mqbroker running." return 2; else echo "The mqbroker(${pid}) is running..." echo -e "Send shutdown to mqbroker(${pid})\c" kill ${pid} oldpid=$pid while [ ${oldpid} ] do oldpid=`ps ax | grep -i ‘org.apache.rocketmq.broker.BrokerStartup‘ |grep java | grep -v grep | awk ‘{print $1}‘` echo -e ‘!\c‘ sleep 1 done echo -e "\nSend shutdown request to mqbroker(${pid}) OK" fi } status() { pid=`ps ax | grep -i ‘org.apache.rocketmq.broker.BrokerStartup‘ |grep java | grep -v grep | awk ‘{print $1}‘` if [ -z "$pid" ] ; then echo "No mqbroker running." return 1; fi echo "The mqbroker(${pid}) is running..." return 0 } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) status ;; *) echo $"Usage: $0 {start|stop|restart|tatus}" esac
启动服务的时候,要先启动mqnamesrv后启动mqbroker,到此为止,安装和配置过程结束,至于rocketmq还有更多的玩法,待后续研究,希望对大家有所帮助。
本文出自 “tianxi” 博客,请务必保留此出处http://tianxi.blog.51cto.com/12735181/1922653
linux下搭建rocketmq(附服务脚本)