Ubuntu环境中RocketMQ安装教程
参考教程
https://blog.csdn.net/weixin_56219549/article/details/126143231
1、安装JDK,并配置环境变量(略)
2、下载RocketMQ安装包
RocketMQ下载地址,选择二进制包下载
unzip rocketmq-all-5.0.0-ALPHA-bin-release.zip 使用FTP工具上传到目标服务器,或者使用命令行下载
wget https://dist.apache.org/repos/dist/release/rocketmq/5.3.1/rocketmq-all-5.3.1-bin-release.zip
然后解压,unzip rocketmq-all-5.3.1-bin-release.zip
3、配置环境变量
# 配置rocketmq环境变量
sudo vim /etc/profile
##加入下面内容,注意修改正确路径
export rocketmq=/home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/
export PATH=$PATH:$rocketmq/bin
4、调整合适的JVM内存大小
#进入到bin目录
cd rocketmq-all-5.3.1-bin-release/bin
vim runbroker.sh
vim runserver.sh
修改 /home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/conf/broker.conf
# 集群名称
brokerClusterName = DefaultCluster
# 节点名称
brokerName = dev-broker-a
# broker id节点ID, 0 表示 master, 其他的正整数表示 slave,不能小于0
brokerId = 0
# 在每天的什么时间删除已经超过文件保留时间的 commit log,默认值04
deleteWhen = 04
# 以小时计算的文件保留时间 默认值72小时
fileReservedTime = 72
# Broker角色
brokerRole = ASYNC_MASTER
# 刷盘方式
flushDiskType = ASYNC_FLUSH
# Broker服务地址
brokerIP1=192.168.6.222
#nameServer地址,分号分割
namesrvAddr= 192.168.6.222:9876
#是否允许Broker自动创建Topic
autoCreateTopicEnable = true
#是否允许 Broker 自动创建订阅组
autoCreateSubscriptionGroup = true
5、启动RocketMQ
5.1、启动mqnameserver(&表示后台启动,不能少)
进入到/rocketmq-all-5.3.1-bin-release/bin目录,执行下面命令
nohup sh mqnamesrv &
查看是否成功
tail -1000f nohup.out
或者jps查看
jps
5.2、启动broker
进入到/rocketmq-all-5.3.1-bin-release/bin目录,执行下面命令
nohup sh mqbroker -n localhost:9876 &
自动创建topic:启动broker时加上自动创建topic的参数,如下,其中autoCreateTopicEnable=true表示自动创建topic
nohup sh mqbroker -n localhost:9876 autoCreateTopicEnable=true > ../broker.log &
6、安装控制台dashboard(略)
(推荐)使用systemd管理nameserver和broker
前面分享了如何在机器上搭建rocketmq,其中nameserver和broker都是以nohup的方式启动。在实际运维过程中,这种启动存在一个比较麻烦的问题:比如机器宕机后,重新启动机器,broker和namserver服务无法自动随开机拉起,还需要我们登陆到每台机器上去操作启动服务。又没有比较好的方式管理启动脚本和停止脚本呢?
我们可以编写unit file,将nameserver和broker作为一个systemd下面的一个unit,从而被systemd管理起来。然后就可以通过systemctl start/restart/stop xxx来启停nameserver和broker了,再通过systemctl enable xxx,可以设置nameserver和broker开启自行启动。
[!NOTE]
开始之前,需要先停止上面方式部署的MQ
1)nameserver
启动:cd /home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin && nohup ./mqnamesrv -c namesrv.properties &
停止:cd /home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin && sh mqshutdown namesrv
- broker
启动:cd /home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin && nohup sh mqbroker -c rmq-broker.conf &
停止:cd /home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin && sh mqshutdown broker
6.1、编写脚本
1)nameserver的unit file编写: /usr/lib/systemd/system/rmqnamesrv.service
[Unit]
Description=RocketMQ NameServer
After=network.target
[Service]
Type=simple
WorkingDirectory=/home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin
ExecStart=/home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin/mqnamesrv
ExecStop=/home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin/mqshutdown namesrv
Restart=on-failure
Environment="JAVA_HOME=/home/ec2-user/software/jdk/jdk-17.0.12"
Environment="PATH=/home/ec2-user/software/jdk/jdk-17.0.12/bin:/usr/bin:/bin"
[Install]
WantedBy=multi-user.target
2)broker的unit file编写: /usr/lib/systemd/system/rmqbroker.service
[Unit]
Description=RocketMQ Broker
After=network.target
[Service]
Type=simple
WorkingDirectory=/home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin
ExecStart=/home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin/mqbroker -c /home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/conf/broker.conf
ExecStop=/home/ec2-user/k8s/rocketmq/rocketmq-all-5.3.1-bin-release/bin/mqshutdown broker
Restart=on-failure
Environment="JAVA_HOME=/home/ec2-user/software/jdk/jdk-17.0.12"
Environment="PATH=/home/ec2-user/software/jdk/jdk-17.0.12/bin:/usr/bin:/bin"
[Install]
WantedBy=multi-user.target
6.2、执行命令
#启动
sudo systemctl start rmqnamesrv
#查看状态
sudo systemctl status rmqnamesrv
#停止
sudo systemctl stop rmqnamesrv
#重启
sudo systemctl restart rmqnamesrv
#设置随开机自动启动
sudo systemctl enable rmqnamesrv
看到下图代表成功
#启动
sudo systemctl start rmqbroker
#查看状态
sudo systemctl status rmqbroker
#停止
sudo systemctl stop rmqbroker
#重启
sudo systemctl restart rmqbroker
#设置随开机自动启动
sudo systemctl enable rmqbroker
看到下图代表成功
namesrv和broker设置开机自启动
sudo systemctl enable rmqnamesrv
sudo systemctl enable rmqbroker
看到下图代表成功