背景

最近公司很多业务需要使用RocketMQ,阿里的云服务昂贵且运维不及时,所以业务团队找到我们部门,由我们部门负责RocketMQ集群的技术支持,每个业务都需要使用独立的集群,而且开发、测试、线上三个环境都需要搭建,靠“人工智能”确实有点太重复造轮子了,K8S环境的话,Operator我们还没开始弄成熟,着急上线的话,也担心出问题,于是决定写个自动部署RocketMQ集群的脚本。对于大部分中间件来说,搭建集群无非就是复制、粘贴、改改配置等,RocketMQ也不例外,于是开始尝试写脚本。

准备工作

要写脚本,首先要熟悉RocketMQ集群的搭建流程,要了解手动搭建流程的话可以看我的另一篇文章:https://blog.csdn.net/sinat_14840559/article/details/108391651 。可以发现,需要部署的有nameserver和broker,console和exporter需要手动打包,而且不是必须的,部署也方便,可以后面再手动部署,Systemd根据自己的情况选择,我是选择所有的应用,基本都由Systemd管理。搭建RocketMQ集群大部分工作都是修改配置文件、日志文件,所以我的思路是把各个配置文件中待修改的地方替换为指定字符(我都替换完了,拿走即用),通过脚本来做替换,涉及的文件如下:

下载二进制包

下载链接:https://archive.apache.org/dist/rocketmq/4.7.1/rocketmq-all-4.7.1-bin-release.zip

解压、文件夹重命名为:rocketmq471

NameServer

详细的文件在附录中

  • 修改nameserver.conf待替换参数为指定占位符
    路径:$RMQ_HOME/conf/nameserver.conf
  • 修改日志配置文件中的待修改参数为指定占位符,
    路径:$RMQ_HOME/conf/logback_namesrv.xml
  • 修改启动脚本中的待修改的参数为指定占位符
  • 修改systemd文件中待修改的参数为指定占位符

Broker

详细的文件在附录中

  • 修改broker.conf待修改的参数配置为特定占位符
    路径:$RMQ_HOME/conf/broker.conf
  • 修改启动脚本中的待修改的参数为指定占位符
  • 修改日志配置文件中的待修改参数为指定占位符
    路径: $RMQ_HOME/conf/logback_broker.xml、$RMQ_HOME/conf/logback_tool.xml

都修改完之后,再将文件夹打包成rocketmq471.zip

以上,完成各个配置文件中参数的替换,这个操作,对于第一次来说有点繁琐,但是之后一定会一劳永逸,再需要搭建,直接执行接下来的脚本即可。

脚本文件

申明:本次脚本主要针对3主6从的集群,用到三台服务器做交叉部署,主要的逻辑就是批量替换占位符,你也可以根据你的集群规模修改,很简单,复制粘贴即可。我用的是非root用户启动的,根据自己的需求修改。

NameServer

auto_cfg_rmq_namesrv.sh

#!/bin/bash

#验证参数个数
if [[ $# -ne 4 ]]; then
	echo "参数个数不正确,请检查"
	exit 8
fi

#接收参数
base_home=$1
nameserver_path=$2
port=$3
cluster_name=$4

#打印参数
echo "==================参数信息===================="
echo "base_home: $base_home"
echo "nameserver_path: $nameserver_path"
echo "port: $port"
echo "cluster_name: $cluster_name"
echo "============================================"

#询问用户是否继续
read -p "是否继续(y/n): " input_char

if [ $input_char == 'y' -o $input_char == 'Y' ]; then
	echo "参数已确认,即将进行部署..."
elif [ $input_char == 'n' -o $input_char == 'N' ]; then
	echo "已取消部署"
	exit 8
else
	echo "输入错误,请输入正确字符"
	exit 8
fi

echo "-----deploy $nameserver_path start...-----"

#文件准备
cd $base_home 
mkdir $nameserver_path
cp rocketmq471.zip shutdown-namesrv.sh start-namesrv.sh restart-namesrv.sh $nameserver_path
cd $nameserver_path
chmod +x *.sh
unzip rocketmq471.zip

namesrv_home=$base_home/$nameserver_path

#修改nameserver.conf
cd rocketmq471/conf
sed -i "s?#NAMESRV_HOME#?$namesrv_home?" nameserver.conf
sed -i "s?#PORT#?$port?" nameserver.conf

#修改日志配置
sed -i "s?#BASE_LOG_PATH#?$namesrv_home?" logback_namesrv.xml

#修改脚本
cd $namesrv_home
sed -i "s?#NAMESRV_HOME#?$namesrv_home?" start-namesrv.sh
sed -i "s?#NAMESRV_HOME#?$namesrv_home?" shutdown-namesrv.sh
sed -i "s?#NAMESRV_HOME#?$namesrv_home?" restart-namesrv.sh

#配置Systemd
service_name=$cluster_name-$nameserver_path.service
cd $base_home
cp example.service /etc/systemd/system/$service_name
cd /etc/systemd/system/
sed -i "s?#SERVICE_NAME#?$service_name?" $service_name
sed -i "s?#BROKER_HOME#?$namesrv_home?" $service_name
sed -i "s?#START_SH#?$namesrv_home/start-namesrv.sh?" $service_name
sed -i "s?#STOP_SH#?$namesrv_home/shutdown-namesrv.sh?" $service_name
sed -i "s?#RELOAD_SH#?$namesrv_home/restart-namesrv.sh?" $service_name
#设置开机生效
systemctl enable $service_name

#授权启动用户
cd $base_home
chown -R rocketmq:rocketmq $namesrv_home

#启动
systemctl start $service_name

echo "-----deploy $nameserver_path finished-----"

参数介绍

  • base_home:集群的主目录(绝对路径)
  • nameserver_path:nameserver的目录名(nameserver的文件夹名称)
  • port:端口号
  • cluster_name:集群名称

Broker

#!/bin/bash

#验证参数个数
if [[ $# -ne 19 ]]; then
	echo "参数个数不正确,请检查"
	exit 8
fi

#====接收参数====
#公共参数
ip=$1
base_home=$2
nameserver=$3
cluster_name=$4
#broker1相关
broker1_path=$5
broker1_name=$6
broker1_port=$7
broker1_id=$8
broker1_role=$9
#broker2相关
broker2_path=${10}
broker2_name=${11}
broker2_port=${12}
broker2_id=${13}
broker2_role=${14}
#broker3相关
broker3_path=${15}
broker3_name=${16}
broker3_port=${17}
broker3_id=${18}
broker3_role=${19}

#打印参数
echo "=======================公共参数========================="
echo "ip: $ip"
echo "base_home: $base_home"
echo "nameserver: $nameserver"
echo "cluster_name: $cluster_name"
echo "=====================第一个Broker======================="
echo "broker1_path: $broker1_path"
echo "broker1_name: $broker1_name"
echo "broker1_port: $broker1_port"
echo "broker1_id:   $broker1_id"
echo "broker1_role: $broker1_role"
echo "=====================第二个Broker======================="
echo "broker2_path: $broker2_path"
echo "broker2_name: $broker2_name"
echo "broker2_port: $broker2_port"
echo "broker2_id:   $broker2_id"
echo "broker2_role: $broker2_role"
echo "=====================第三个Broker======================="
echo "broker3_path: $broker3_path"
echo "broker3_name: $broker3_name"
echo "broker3_port: $broker3_port"
echo "broker3_id:   $broker3_id"
echo "broker3_role: $broker3_role"
echo "======================================================="

#询问用户是否继续
read -p "是否继续(y/n): " input_char

if [ $input_char == 'y' -o $input_char == 'Y' ]; then
	echo "参数已确认,即将进行部署..."
elif [ $input_char == 'n' -o $input_char == 'N' ]; then
	echo "已取消部署"
	exit 8
else
	echo "输入错误,请输入正确字符"
	exit 8
fi

echo "-----deploy $broker1_path start...-----"

#第一个Broker
cd $base_home 
mkdir $broker1_path
cp rocketmq471.zip restart-broker.sh shutdown-broker.sh start-broker.sh $broker1_path
cd $broker1_path
chmod +x *.sh
unzip rocketmq471.zip
cd rocketmq471/bin

broker1_home=$base_home/$broker1_path

#替换runbroker.sh中GC日志目录
gc_log_dir=$broker1_home/logs/gc.log
sed -i "s?#GC_LOG_DIR#?$gc_log_dir?" runbroker.sh
chmod +x runbroker.sh

#修改broker.conf
cd ../conf
sed -i "s?#PORT#?$broker1_port?" broker.conf
sed -i "s?#BROKER_HOME#?$broker1_home?" broker.conf
sed -i "s?#CLUSTER_NAME#?$cluster_name?" broker.conf
sed -i "s?#NAMESRV_ADDR#?$nameserver?" broker.conf
sed -i "s?#BROKER_NAME#?$broker1_name?" broker.conf
sed -i "s?#BROKER_IP#?$ip?" broker.conf
sed -i "s?#BROKER_ID#?$broker1_id?" broker.conf
sed -i "s?#BROKER_ROLE#?$broker1_role?" broker.conf

#修改日志配置
sed -i "s?#BASE_LOG_PATH#?$broker1_home?" logback_broker.xml
sed -i "s?#BASE_LOG_PATH#?$broker1_home?" logback_tools.xml

#修改脚本
cd $broker1_home
sed -i "s?#BROKER_HOME#?$broker1_home?" start-broker.sh
sed -i "s?#BROKER_HOME#?$broker1_home?" restart-broker.sh
sed -i "s?#BROKER_HOME#?$broker1_home?" shutdown-broker.sh

#配置Systemd
broker1_service_name=$cluster_name-$broker1_path.service
cd $base_home
cp example.service /etc/systemd/system/$broker1_service_name
cd /etc/systemd/system/
sed -i "s?#SERVICE_NAME#?$broker1_service_name?" $broker1_service_name
sed -i "s?#BROKER_HOME#?$broker1_home?" $broker1_service_name
sed -i "s?#START_SH#?$broker1_home/start-broker.sh?" $broker1_service_name
sed -i "s?#STOP_SH#?$broker1_home/shutdown-broker.sh?" $broker1_service_name
sed -i "s?#RELOAD_SH#?$broker1_home/restart-broker.sh?" $broker1_service_name
systemctl enable $broker1_service_name

#授权启动用户
cd $base_home
chown -R rocketmq:rocketmq $broker1_home

#启动
systemctl start $broker1_service_name

echo "-----deploy $broker1_path finished-----"

#===============================================================

echo "-----deploy $broker2_path start...-----"

#第二个Broker
cd $base_home 
mkdir $broker2_path
cp rocketmq471.zip restart-broker.sh shutdown-broker.sh start-broker.sh $broker2_path
cd $broker2_path
chmod +x *.sh
unzip rocketmq471.zip
cd rocketmq471/bin

broker2_home=$base_home/$broker2_path

#替换runbroker.sh中GC日志目录
gc_log_dir=$broker2_home/logs/gc.log
sed -i "s?#GC_LOG_DIR#?$gc_log_dir?" runbroker.sh
chmod +x runbroker.sh

#修改broker.conf
cd ../conf
sed -i "s?#PORT#?$broker2_port?" broker.conf
sed -i "s?#BROKER_HOME#?$broker2_home?" broker.conf
sed -i "s?#CLUSTER_NAME#?$cluster_name?" broker.conf
sed -i "s?#NAMESRV_ADDR#?$nameserver?" broker.conf
sed -i "s?#BROKER_NAME#?$broker2_name?" broker.conf
sed -i "s?#BROKER_IP#?$ip?" broker.conf
sed -i "s?#BROKER_ID#?$broker2_id?" broker.conf
sed -i "s?#BROKER_ROLE#?$broker2_role?" broker.conf

#修改日志配置
sed -i "s?#BASE_LOG_PATH#?$broker2_home?" logback_broker.xml
sed -i "s?#BASE_LOG_PATH#?$broker2_home?" logback_tools.xml

#修改脚本
cd $broker2_home
sed -i "s?#BROKER_HOME#?$broker2_home?" start-broker.sh
sed -i "s?#BROKER_HOME#?$broker2_home?" restart-broker.sh
sed -i "s?#BROKER_HOME#?$broker2_home?" shutdown-broker.sh

#配置Systemd
broker2_service_name=$cluster_name-$broker2_path.service
cd $base_home
cp example.service /etc/systemd/system/$broker2_service_name
cd /etc/systemd/system/
sed -i "s?#SERVICE_NAME#?$broker2_service_name?" $broker2_service_name
sed -i "s?#BROKER_HOME#?$broker2_home?" $broker2_service_name
sed -i "s?#START_SH#?$broker2_home/start-broker.sh?" $broker2_service_name
sed -i "s?#STOP_SH#?$broker2_home/shutdown-broker.sh?" $broker2_service_name
sed -i "s?#RELOAD_SH#?$broker2_home/restart-broker.sh?" $broker2_service_name
systemctl enable $broker2_service_name

#授权启动用户
cd $base_home
chown -R rocketmq:rocketmq $broker2_home

#启动
systemctl start $broker2_service_name

echo "-----deploy $broker2_path finished-----"

#===============================================================

echo "-----deploy $broker3_path start...-----"

#第三个Broker
cd $base_home 
mkdir $broker3_path
cp rocketmq471.zip restart-broker.sh shutdown-broker.sh start-broker.sh $broker3_path
cd $broker3_path
chmod +x *.sh
unzip rocketmq471.zip
cd rocketmq471/bin

broker3_home=$base_home/$broker3_path

#替换runbroker.sh中GC日志目录
gc_log_dir=$broker3_home/logs/gc.log
sed -i "s?#GC_LOG_DIR#?$gc_log_dir?" runbroker.sh
chmod +x runbroker.sh

#修改broker.conf
cd ../conf
sed -i "s?#PORT#?$broker3_port?" broker.conf
sed -i "s?#BROKER_HOME#?$broker3_home?" broker.conf
sed -i "s?#CLUSTER_NAME#?$cluster_name?" broker.conf
sed -i "s?#NAMESRV_ADDR#?$nameserver?" broker.conf
sed -i "s?#BROKER_NAME#?$broker3_name?" broker.conf
sed -i "s?#BROKER_IP#?$ip?" broker.conf
sed -i "s?#BROKER_ID#?$broker3_id?" broker.conf
sed -i "s?#BROKER_ROLE#?$broker3_role?" broker.conf

#修改日志配置
sed -i "s?#BASE_LOG_PATH#?$broker3_home?" logback_broker.xml
sed -i "s?#BASE_LOG_PATH#?$broker3_home?" logback_tools.xml

#修改脚本
cd $broker3_home
sed -i "s?#BROKER_HOME#?$broker3_home?" start-broker.sh
sed -i "s?#BROKER_HOME#?$broker3_home?" restart-broker.sh
sed -i "s?#BROKER_HOME#?$broker3_home?" shutdown-broker.sh

#配置Systemd
broker3_service_name=$cluster_name-$broker3_path.service
cd $base_home
cp example.service /etc/systemd/system/$broker3_service_name
cd /etc/systemd/system/
sed -i "s?#SERVICE_NAME#?$broker3_service_name?" $broker3_service_name
sed -i "s?#BROKER_HOME#?$broker3_home?" $broker3_service_name
sed -i "s?#START_SH#?$broker3_home/start-broker.sh?" $broker3_service_name
sed -i "s?#STOP_SH#?$broker3_home/shutdown-broker.sh?" $broker3_service_name
sed -i "s?#RELOAD_SH#?$broker3_home/restart-broker.sh?" $broker3_service_name
systemctl enable $broker3_service_name

#授权启动用户
cd $base_home
chown -R rocketmq:rocketmq $broker3_home

#启动
systemctl start $broker3_service_name

echo "-----deploy $broker3_path finished-----"

#删除部署相关文件
cd $base_home
rm -f example.service *-namesrv.sh *-broker.sh *.zip

echo "-----deploy file delete finished-----"

参数介绍

公共参数:

  • ip:本机IP,对外访问
  • base_home:集群的主目录(绝对路径)
  • nameserver:nameserver地址,单引号包上
  • cluster_name:集群名称

Broker相关:

  • broker1_path:nameserver的目录名(nameserver的文件夹名称)
  • broker1_name:broker的名称
  • broker1_port:broker端口
  • broker1_id:broker id(0-master;>0-slave 一个主从关系内,不要重复)
  • broker1_role:broker角色(ASYNC_MASTER、SYNC_MASTER、SLAVE)

其他broker参数如上

这个脚本的参数比较多,原因是3个broker,每个broker的配置都通过参数传递,也可以直接在脚本中写死,但是不灵活。

如何使用

上传文件

注意:脚本中没有写SCP传输的功能,所以需要将文件上传到三台服务器,当然这也用不了多少功夫。

  • 创建文件夹

    创建一个主目录,之后所有和该集群相关的broker、nameserver文件夹都在该目录下,也就是参数中的base_home

    如下图所示,base_home就是:/neworiental/rocketmq-zrh

  • 上传部署相关的准备文件

部署

假如我需要部署的集群信息如下:

Broker

节点机器端口
broker-a172.22.36.6820911
broker-a-s1172.22.36.6920911
broker-a-s2172.22.36.7020911
broker-b172.22.36.6920915
broker-b-s1172.22.36.6820915
broker-b-s2172.22.36.7020915
broker-c172.22.36.7020919
broker-c-s1172.22.36.6820919
broker-c-s2172.22.36.6920919

nameserver

节点机器端口
nameserver172.22.36.689976
nameserver172.22.36.699976
nameserver172.22.36.709976

先部署NameServer

需要在172.22.36.68、172.22.36.69、172.22.36.70三台机器上分别执行:

./auto_cfg_rmq_namesrv.sh /neworiental/rocketmq-zrh rocketmq-nameserver 9976 rocketmq-test



这样就完成了nameserver的部署。出于保险起见,可以检查一下文件是否修改成功。

再部署Broker

由于机器内存不够,我就不演示了,也可以将每个broker内存改小来测试

在172.22.36.68上执行:

./auto_cfg_rmq_broker.sh 172.22.36.68 /neworiental/rocketmq-zrh '172.22.36.68:9976;172.22.36.69:9976;172.22.36.70:9976' rocketmq-zrh rocketmq-a broker-a 20911 0 ASYNC_MASTER rocketmq-b-s1 broker-b 20915 1 SLAVE roketmq-c-s1 broker-c 20919 1 SLAVE

在172.22.36.69上执行:

./auto_cfg_rmq_broker.sh 172.22.36.69 /neworiental/rocketmq-zrh '172.22.36.68:9976;172.22.36.69:9976;172.22.36.70:9976' rocketmq-zrh rocketmq-b broker-b 20915 0 ASYNC_MASTER rocketmq-a-s1 broker-a 20911 1 SLAVE roketmq-c-s1 broker-c 20919 1 SLAVE

在172.22.36.70上执行:

./auto_cfg_rmq_broker.sh 172.22.36.70 /neworiental/rocketmq-zrh '172.22.36.68:9976;172.22.36.69:9976;172.22.36.70:9976' rocketmq-zrh rocketmq-c broker-c 20919 0 ASYNC_MASTER rocketmq-a-s2 broker-a 20911 2 SLAVE roketmq-b-s2 broker-b 20915 2 SLAVE

部署的时候先规划好,这样出问题的几率小。

以上就算大功告成了,第一次会麻烦点,但是对于需要经常搭建集群的人来说,会事半功倍。

附录

NameServer相关

1、修改nameserver.conf待替换参数为指定占位符

路径:$RMQ_HOME/conf/nameserver.conf

kvConfigPath=#NAMESRV_HOME#/data/kvConfig.json
listenPort=#PORT#
useEpollNativeSelector=true

2、修改日志配置文件中的待修改参数为指定占位符

路径:$RMQ_HOME/conf/logback_namesrv.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="DefaultAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/namesrv_default.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/namesrv_default.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>5</maxIndex>
        </rollingPolicy>
        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>

    <appender name="RocketmqNamesrvAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/namesrv.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/namesrv.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>5</maxIndex>
        </rollingPolicy>
        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqNamesrvAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqNamesrvAppender_inner"/>
        <discardingThreshold>0</discardingThreshold>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <append>true</append>
        <encoder>
            <pattern>%d{yyy-MM-dd HH\:mm\:ss,SSS} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>

    <logger name="RocketmqNamesrv" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqNamesrvAppender"/>
    </logger>

    <logger name="RocketmqCommon" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqNamesrvAppender"/>
    </logger>

    <logger name="RocketmqRemoting" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqNamesrvAppender"/>
    </logger>

    <logger name="RocketmqNamesrvConsole" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="STDOUT"/>
    </logger>

    <root>
        <level value="INFO"/>
        <appender-ref ref="DefaultAppender"/>
    </root>
</configuration>

3、修改启动脚本中的待修改的参数为指定占位符

启动:start-namesrv.sh

#!/bin/sh
. /etc/profile

nameserver_home=#NAMESRV_HOME#
nohup sh ${nameserver_home}/rocketmq471/bin/mqnamesrv -c ${nameserver_home}/rocketmq471/conf/nameserver.conf >/dev/null 2>&1 &
echo "startup nameserver..."

停止:shutdown-namesrv.sh

#!/bin/sh
. /etc/profile

echo "shutdown rocketmq-nameserver..."
sh #NAMESRV_HOME#/rocketmq471/bin/mqshutdown namesrv

重启:restart-namesrv.sh

#!/bin/bash
. /etc/profile

PID=`ps -ef | grep '#NAMESRV_HOME#' | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
  echo "killing #NAMESRV_HOME# : $PID"
  kill $PID
fi

sleep 1

nameserver_home=#NAMESRV_HOME#
nohup sh ${nameserver_home}/rocketmq471/bin/mqnamesrv -c ${nameserver_home}/rocketmq471/conf/nameserver.conf >/dev/null 2>&1 &
echo "startup nameserver..."

4、修改systemd文件中待修改的参数为指定占位符

[Unit]
Description=#SERVICE_NAME#
After=network.target

[Service]
User=rocketmq
Group=rocketmq
Type=forking
WorkingDirectory=#BROKER_HOME#
ExecStart=#START_SH#
ExecStop=#STOP_SH#
ExecReload=#RELOAD_SH#
LimitNOFILE=102536
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Broker相关

1、修改broker.conf待修改的参数配置为特定占位符

路径:$RMQ_HOME/conf/broker.conf

listenPort = #PORT#
rocketmqHome = #BROKER_HOME#/rocketmq471
brokerClusterName = #CLUSTER_NAME#
namesrvAddr= #NAMESRV_ADDR#
brokerName = #BROKER_NAME#
brokerIP1 = #BROKER_IP#
brokerIP2= #BROKER_IP#
brokerId = #BROKER_ID#
storePathRootDir = #BROKER_HOME#/store
storePathCommitLog = #BROKER_HOME#/store/commitlog
storePathConsumerQueue = #BROKER_HOME#/store/consumequeue
deleteWhen = 04
fileReservedTime = 168
brokerRole = #BROKER_ROLE#
flushDiskType = ASYNC_FLUSH
autoCreateTopicEnable = false
autoCreateSubscriptionGroup = false
useEpollNativeSelector = true

2、修改启动脚本中的待修改的参数为指定占位符

启动:start-broker.sh

#!/bin/bash
. /etc/profile

broker_home=#BROKER_HOME#
nohup sh ${broker_home}/rocketmq471/bin/mqbroker -c ${broker_home}/rocketmq471/conf/broker.conf >/dev/null 2>&1 &
echo "starting broker..."

停止:shutdown-broker.sh

#!/bin/bash
. /etc/profile

PID=`ps -ef | grep '#BROKER_HOME#' | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
  echo "killing #BROKER_HOME# : $PID"
  kill $PID
fi

重启:restart-broker.sh

#!/bin/bash
. /etc/profile

broker_home=#BROKER_HOME#

PID=`ps -ef | grep '#BROKER_HOME#' | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
  echo "killing ${broker_home} : $PID"
  kill $PID
fi

sleep 1

nohup sh ${broker_home}/rocketmq471/bin/mqbroker -c ${broker_home}/rocketmq471/conf/broker.conf >/dev/null 2>&1 &
echo "startup broker..."

3、修改日志配置文件中的待修改参数为指定占位符

1)logback_broker.xml

路径:$RMQ_HOME/conf/logback_broker.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="DefaultAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/broker_default.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/broker_default.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>

    <appender name="RocketmqBrokerAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/broker.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/broker.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>20</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>128MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqBrokerAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqBrokerAppender_inner"/>
    </appender>

    <appender name="RocketmqProtectionAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/protection.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/protection.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqProtectionAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqProtectionAppender_inner"/>
    </appender>

    <appender name="RocketmqWaterMarkAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/watermark.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/watermark.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqWaterMarkAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqWaterMarkAppender_inner"/>
    </appender>

    <appender name="RocketmqStoreAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/store.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/store.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>128MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqStoreAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqStoreAppender_inner"/>
    </appender>

    <appender name="RocketmqRemotingAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/remoting.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/remoting.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqRemotingAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqRemotingAppender_inner"/>
    </appender>

    <appender name="RocketmqStoreErrorAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/storeerror.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/storeerror.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqStoreErrorAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqStoreErrorAppender_inner"/>
    </appender>


    <appender name="RocketmqTransactionAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/transaction.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/transaction.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqTransactionAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqTransactionAppender_inner"/>
    </appender>

    <appender name="RocketmqRebalanceLockAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/lock.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/lock.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>5</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqRebalanceLockAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqRebalanceLockAppender_inner"/>
    </appender>

    <appender name="RocketmqFilterAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/filter.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/filter.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqFilterAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqFilterAppender_inner"/>
    </appender>

    <appender name="RocketmqStatsAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/stats.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/stats.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>5</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>

    <appender name="RocketmqCommercialAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/commercial.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/commercial.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>500MB</maxFileSize>
        </triggeringPolicy>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <append>true</append>
        <encoder>
            <pattern>%d{yyy-MM-dd HH\:mm\:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>

    <logger name="RocketmqBroker" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqBrokerAppender"/>
    </logger>

    <logger name="RocketmqProtection" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqProtectionAppender"/>
    </logger>

    <logger name="RocketmqWaterMark" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqWaterMarkAppender"/>
    </logger>

    <logger name="RocketmqCommon" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqBrokerAppender"/>
    </logger>

    <logger name="RocketmqStore" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqStoreAppender"/>
    </logger>

    <logger name="RocketmqStoreError" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqStoreErrorAppender"/>
    </logger>

    <logger name="RocketmqTransaction" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqTransactionAppender"/>
    </logger>

    <logger name="RocketmqRebalanceLock" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqRebalanceLockAppender"/>
    </logger>

    <logger name="RocketmqRemoting" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqRemotingAppender"/>
    </logger>

    <logger name="RocketmqStats" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqStatsAppender"/>
    </logger>

    <logger name="RocketmqCommercial" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqCommercialAppender"/>
    </logger>

    <logger name="RocketmqFilter" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqFilterAppender"/>
    </logger>

    <logger name="RocketmqConsole" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="STDOUT"/>
    </logger>

    <root>
        <level value="INFO"/>
        <appender-ref ref="DefaultAppender"/>
    </root>
</configuration>

2)logback_tools.xml

路径:$RMQ_HOME/conf/logback_tools.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="DefaultAppender"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/tools_default.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/tools_default.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>5</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>

    <appender name="RocketmqToolsAppender_inner"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>#BASE_LOG_PATH#/logs/tools.log</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>#BASE_LOG_PATH#/logs/otherdays/tools.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>5</maxIndex>
        </rollingPolicy>
        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>100MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyy-MM-dd HH:mm:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>
    <appender name="RocketmqToolsAppender" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="RocketmqToolsAppender_inner"/>
    </appender>


    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <append>true</append>
        <encoder>
            <pattern>%d{yyy-MM-dd HH\:mm\:ss,GMT+8} %p %t - %m%n</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>

    <logger name="RocketmqTools" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqToolsAppender"/>
    </logger>

    <logger name="RocketmqCommon" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqBrokerAppender"/>
    </logger>

    <logger name="RocketmqStore" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqBrokerAppender"/>
    </logger>

    <logger name="RocketmqRemoting" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="RocketmqBrokerAppender"/>
    </logger>

    <root>
        <level value="INFO"/>
        <appender-ref ref="DefaultAppender"/>
    </root>
</configuration>

The End.

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐