描述项 | 内容 |
---|---|
操作系统 | CentOS Linux release 7.9.2009 (Core) |
JDK | jdk-21.0.3 |
RocketMQ | rocketmq-all-5.2.0 |
1. 环境准备
安装JDK:http://dba.qishuo.xin/?p=3964
2. 下载RocketMQ
下载二进制包
cd /media
wget https://dist.apache.org/repos/dist/release/rocketmq/5.2.0/rocketmq-all-5.2.0-bin-release.zip
3. 解压文件
cd /media
unzip -n rocketmq-all-5.2.0-bin-release.zip -d /usr/local/
mv /usr/local/rocketmq-all-5.2.0-bin-release /usr/local/rocketmq
# 为了解决broker.log日志一直打印/usr/local/rocketmq/conf/acl is not exist,在这里手动创建一个空目录
mkdir -p /usr/local/rocketmq/conf/acl
4. 参数配置
4.1. 内存及JAVA_HOME配置
JAVA_HOME 配置为:/usr/local/java/jdk-21.0.3
内存配置为:-server -Xms256m -Xmx256m -Xmn256m -XX:PermSize=128m-XX:MaxPermSize=128m
(1)在目录/usr/local/rocketmq/bin
下找到文件:runserver.sh
、runbroker.sh
、tools.sh
(2)修改配置runserver.sh:runserver.sh
(3)修改配置runbroker.sh:vim runbroker.sh
4.2. 修改日志保存目录
启动NameServer、Broker服务后,默认的日志会保存在启动服务的用户根目录中。要修改日志的位置,仍然是对runnamesrv.sh
和runserver.sh
两个文件做修改,在末尾处添加如下内容:
#rewrite user home
JAVA_OPT="${JAVA_OPT} -Duser.home=新目录(如:/usr/local/rocketmq)"
5. 编辑启动脚本
5.1. 创建NameServer服务文件
进入/lib/systemd/system
目录下,创建rocketmq-namesrv.service文件(touch rocketmq-namesrv.service
),内容如下:
[Unit]
Description=RocketMQ Namesrv
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=sample
User=root
ExecStart=/usr/local/rocketmq/bin/mqnamesrv
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=0
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
5.2. 创建broker服务文件
进入/lib/systemd/system
目录下,创建rocketmq-broker.service文件(touch rocketmq-broker.service
),内容如下:
[Unit]
Description=rocketmq-broker
After=network.target
[Service]
Type=sample
User=root
ExecStart=/usr/local/rocketmq/bin/mqbroker -n localhost:9876 --enable-proxy
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=0
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
6. 启动NameServer与Broker
6.1. 重新加载systemd守护进程的配置文件
systemctl daemon-reload
6.2. 停止与启动NameServer
systemctl stop rocketmq-namesrv.service
systemctl start rocketmq-namesrv.service
systemctl status rocketmq-namesrv.service
6.3. 停止与启动Broker
systemctl stop rocketmq-broker.service
systemctl start rocketmq-broker.service
systemctl status rocketmq-broker.service
6.4. 设置开机自启动
systemctl enable rocketmq-broker.service
systemctl enable rocketmq-namesrv.service
6.5. 防火墙端口开放
rocketmq 常用默认端口:9876、10911、10909
-
9876:NameServer通信端口。客户端通过该端口与NameServer通信。
-
10911:remotingServer使用的监听端口,主要用途:
- producer发送的消息
- conumser在消费失败或者消费超时发送的消息
- consumer拉取消息
- 10912是主broker用于监听从broker请求的监听端口。
使用中遇到的场景:10911端口未开发,消费者订阅进行消费时,无消息可消费,查看客户端日志发现getConsumerIdListByGroup exception失败。
- 10909:fastRemotingServer使用的监听端口,与remotingServer类似,vipChannelEnabled开启时,消息发送到fastRemotingServer。也可称为vip通道端口。
firewall-cmd --zone=public --add-port=9876/tcp --permanent
firewall-cmd --zone=public --add-port=10909/tcp --permanent
firewall-cmd --zone=public --add-port=10911/tcp --permanent
firewall-cmd --reload
7. 测试RocketMQ
7.1. 发送消息
# 1.设置环境变量
export NAMESRV_ADDR=localhost:9876
# 2.使用安装包的Demo发送消息
cd /usr/local/rocketmq
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
7.2. 接受消息
# 1.设置环境变量
export NAMESRV_ADDR=localhost:9876
# 2.接收消息
cd /usr/local/rocketmq
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
8. RocketMQ的Web管理控制台(Dashboard)
8.1. 下载源码并打成jar包
gitHub地址:https://github.com/apache/rocketmq-dashboard
cd rocketmq-dashboard
mvn clean package -Dmaven.test.skip=true
java -jar target/rocketmq-dashboard-1.0.1-SNAPSHOT.jar
8.2. 启动jar包
nohup java -jar /usr/local/rocketmq/rocketmq-dashboard-1.0.1-SNAPSHOT.jar --server.port=8082 --rocketma.config.namesrvAddr=172.16.5.74:9876 > /usr/local/rocketmq/logs/dashboard.log 2>&1 &
8.3. 登录Web
附录
附录A. 相关联的文章
附录B. 参考
-
官方文档:本地部署 RocketMQ
https://rocketmq.apache.org/zh/docs/quickStart/01quickstart/ -
Liunx环境 安装RocketMQ服务+自定义端口号+搭建集群模式(多主多从)+整合Rocketmq-dashboard控制台
https://blog.csdn.net/qq_39648078/article/details/127304727 -
RocketMQ快速入门:namesrv、broker、dashboard的作用及消息发送、消费流程(三)
https://blog.csdn.net/qq_24950043/article/details/137911371
附录C. RocketMQ ACL的请求流程
附录D. Broker配置文件详解
# 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.
# broker所属集群的名称
brokerClusterName = DefaultCluster
# broker的名称
brokerName = broker-a
# broker的ID, 0表示Master,非0表示Slave
brokerId = 0
# 删除文件时间点,默认是凌晨4点
deleteWhen = 04
# 文件保留时间,默认保留48小时
fileReservedTime = 48
# broker的角色
# ASYNC_MASTER: 异步复制Master
# SYNC_MASTER: 同步双写Master
# SLAVE: slave
brokerRole = ASYNC_MASTER
# 刷盘方式
# ASYNC_FLUSH: 异步刷盘
# SYNC_FLUSH: 同步刷盘
flushDiskType = ASYNC_FLUSH
# NameServer的地址,如果有多个的话,使用分号分隔开
namesrvAddr=172.16.5.74:9876
# 当前broker监听的IP地址
brokerIP1=172.16.5.74
# 在发送消息时,自动创建服务器不存在的topic,默认创建4个队列
defaultTopicQueueNums=4
# 是否允许broker自动创建Topic
autoCreateTopicEnable=true
#是否允许broker自动创建订阅组
autoCreateSubscriptionGroup=true
# broker对外服务的监听端口
listenPort=10911
# 每个commitLog文件的大小默认是1G
mapedFileSizeCommitLog=1073741824
# ConsumeQueue每个文件默认存30W条
mapedFileSizeConsumeQueue=300000
# store的存储路径
storePathRootDir=/usr/local/rocketmq/store
# commitLog的存储路径
storePathCommitLog=/usr/local/rocketmq/store/commitlog
# 消费队列的存储路径
storePathConsumeQueue=/usr/local/rocketmq/store/consumequeue
# 消息索引的存储路径
storePathIndex=/usr/local/rocketmq/store/index
# checkpoint文件的存储路径
storeCheckpoint=/usr/local/rocketmq/store/checkpoint
# abort文件的存储路径
abortFile=/usr/local/rocketmq/store/abort
# 限制的消息大小,默认为4M
maxMessageSize=65536
# 检测物理文件磁盘空间
diskMaxUsedSpaceRatio=75