RocketMQ 安装

RocketMQ 安装

记录如何安装rocketMQ 以及使用service脚本进行管理

注意:请确保当前机器上安装了JDK 并且有JAVA_HOME 环境变量

一、RocketMQ 下载安装

  1. 从官网上下载最新安装包,下载完成后并解压到/opt 目录中

    [root@VM-4-14-centos ~] tar -zxvf rocketmq-4.9.3.tar.gz -C /opt/
    
  2. 修改runbroker.sh、runserver.sh 将其中的去掉-Xms参数,避免内存不足无法启动
    1

    2

  3. 创建rocketmq 用户,并进行建立软链接参数

    [root@VM-4-14-centos bin] useradd -r -M rocketmq  # 创建rocketmq用户
    [root@VM-4-14-centos bin] ln -s /opt/rocketmq-4.9.3/ /usr/local/rocketmq # 建立软链接
    [root@VM-4-14-centos rocketmq] echo 'export PATH=/usr/local/rocketmq/bin:$PATH' >> /etc/profile && source /etc/profile # 加入path环境变量
    
  4. 将rocketmq.sh 文件放到rocketmq bin目录下,并修改权限

    [root@VM-4-14-centos bin] mv /root/rocketmq.sh /usr/local/rocketmq/bin/ 
    [root@VM-4-14-centos bin] chown rocketmq:rocketmq -R /usr/local/rocketmq/ #修改文件所有者
    [root@VM-4-14-centos bin] chmod +x -R /usr/local/rocketmq/bin/ 
    
  5. 将rockemq service 脚本复制到 /etc/init.d/ 目录下

    [root@VM-4-14-centos ~] cp /root/rocketmq /etc/init.d/
    [root@VM-4-14-centos ~] chmod +x /etc/init.d/rocketmq # 增加执行权限
    [root@VM-4-14-centos ~] chkconfig rocketmq # 服务自启动
    

二、RocketMQ 启动测试

  1. 输入service rocketmq start 启动rocketMQ

    [root@VM-4-14-centos ~] service rocketmq start # rocketmq 启动
     Starting RocketMQ... 
    Name Server Start Success
    Broker Start Success
     Start RocketMQ  ...............................  [OK] 
    
  2. 输入service rocketmq status 查看rocketMQ 状态

    [root@VM-4-14-centos bin] service rocketmq status
     Broker is running on 27614   
     NameServer is running on 27570
    

三、问题排查

如果出现问题可以查看rockeMQ 的日志

  1. 进入/usr/local/rocketmq/log 目录下,查看rocketMQ 日志,所有日志以日期为单位进行分割

    3

四、附录

  1. rocketmq.sh 脚本

    #!/bin/bash
    # this is rocketmq start stop script
    FRONT="\033[34;49;1m"
    BACK="\033[39;49;0m"
    # 应用目录
    ROCKETMQ_HOME=${ROCKETMQ_HOME:-/usr/local/rocketmq}
    # () 括号可以用于命令执行结果,即将该命令执行结果作为参数传给变量
    TODAY=$(date +%y%m%d)
    IP=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
    
    start_server () {
      cd ${ROCKETMQ_HOME}
      nohup sh $ROCKETMQ_HOME/bin/mqnamesrv 1>${ROCKETMQ_HOME}/log/rocketmq_nameserver_${TODAY}.log 2>&1 &
      SERVER_PID="$!"
    
      if [[ $? != 0 ]]; then
        echo -e "${FRONT}Name Server Start Failed${BACK}"
        exit 2
      else
        echo -e "${FRONT}Name Server Start Success${BACK}"  
      fi
    
    }
    
    start_broker () {
      cd ${ROCKETMQ_HOME}
      nohup sh $ROCKETMQ_HOME/bin/mqbroker -n ${IP}:9876 -c $ROCKETMQ_HOME/conf/broker.conf >${ROCKETMQ_HOME}/log/rocketmq_broker_${TODAY}.log 2>&1 &
      BROKER_PID="$!"
    
      if [[ $? != 0 ]]; then
        echo -e "${FRONT}Broker Start Failed${BACK}"
        exit 2
      else
        echo -e "${FRONT}Broker Start Success${BACK}"  
      fi
    
    }
    
    do_stop () {
      cd ${ROCKETMQ_HOME}
      if [[ -f $ROCKETMQ_HOME/bin/mqshutdown ]]; then
    
        sh ./bin/mqshutdown namesrv >> ${ROCKETMQ_HOME}/log/rocketmq_shutdown_${TODAY}.log
        sleep  2s
        sh ./bin/mqshutdown broker >> ${ROCKETMQ_HOME}/log/rocketmq_shutdown_${TODAY}.log
    
        if [[ $? = 0 ]]; then
        echo -e "${FRONT}stop success${BACK}"
        fi
    
      else
        echo -e "${FRONT}no stop script,exit${BACK}"
        exit 2
      fi
    }
    
    do_status () {
      STATUS1=$(jps | grep BrokerStartup)
      if [[ ${STATUS1} != "" ]]; then
        echo -e "$FRONT Broker is running on $(echo ${STATUS1} | sed "s/BrokerStartup/""/g") $BACK " 
      else
        echo -e "$FRONT Broker NOT RUNNING $BACK "
      fi
    
      STATUS2=$(jps | grep NamesrvStartup)
      if [[ ${STATUS2} != "" ]]; then
        echo -e "$FRONT NameServer is running on $(echo ${STATUS2} | sed "s/NamesrvStartup/""/g") $BACK "
      else
        echo -e "$FRONT NameServer NOT RUNNING $BACK "
      fi
    
    }
    
    do_start (){
      start_server
      sleep 1 
      start_broker
    }
    
    case "$1" in
      start)
        do_start
      ;;
      stop)
        do_stop
      ;;
      restart)
        do_stop
       # 由于broker 在停止时是不会立即退出的,所以此处需要将sleep时间略微长一点,如果2s不足可以再延长些
        sleep 2
        do_start
      ;;
      status)
        do_status
      ;;
      *)
        echo "$0 <start|stop|restart|status> [id]"
      ;;
    esac
    
    
  2. rocketmq service 脚本

    #!/bin/sh
    # as it does use of the /proc filesystem.
    # chkconfig:   2345 90 10
    # description:  RocketMQ is high performance Message Queue
    
    ulimit -n 65535
    FRONT="\033[33;49;1m"
    BACK="\033[39;49;0m"
    GREEN_STAR="\033[32;49;1m*\033[39;49;0m"
    RED_STAR="\033[31;49;1m*\033[39;49;0m"
    WARNING="\033[33;49;1m [WARNING] \033[39;49;0m"
    ERROR="\033[31m [ERROR] \033[0m"
    SUCCESS="\033[32m [OK] \033[0m"
    LISTITEM="\033[32m [-] \033[0m"
    POINT="..............................."
    USER=rocketmq
    EXEC=/usr/local/rocketmq/bin/rocketmq.sh
    ROCKETMQ_HOME=${ROCKETMQ_HOME:-/usr/local/rocketmq}
    TODAY=$(date +%y%m%d)
    # source 该文件,将export一些环境变量并传入子shell中
    source /etc/profile
    
    do_status () {
      STATUS1=$(jps | grep BrokerStartup)
      if [[ ${STATUS1} != "" ]]; then
        echo -e "$FRONT Broker is running on $(echo ${STATUS1} | sed "s/BrokerStartup/""/g") $BACK " 
      else
        echo -e "$FRONT Broker NOT RUNNING $BACK "
      fi
    
      STATUS2=$(jps | grep NamesrvStartup)
      if [[ ${STATUS2} != "" ]]; then
        echo -e "$FRONT NameServer is running on $(echo ${STATUS2} | sed "s/NamesrvStartup/""/g") $BACK "
      else
        echo -e "$FRONT NameServer NOT RUNNING $BACK "
      fi
    
    }
    
    case "$1" in
        start)
            #echo "$PIDFILE exists, process is already running or crashed"
            #echo -e "$FRONT Start RocketMQ $BACK $POINT $SUCCESS"
            echo -e "$FRONT Starting RocketMQ... $BACK"
            # -m参数不重置环境变量
            runuser $USER -m -c "$EXEC start"
            reslt_va=$?
            if [[ $reslt_va -eq 0 ]]; then
                echo -e "$FRONT Start RocketMQ $BACK $POINT $SUCCESS"
            else
                echo -e "$FRONT Start RocketMQ $BACK $POINT $ERROR"
            fi
            ;;
        stop)
            #echo "$PIDFILE does not exist........ process is not running"
            echo -e "$FRONT Stop RocketMQ $BACK $POINT $SUCCESS"
            #echo "Stopping ..."
            runuser $USER -m -c "$EXEC stop"
            reslt_va=$?
            if [[ $reslt_va -eq 0 ]];then
                echo -e "$FRONT Stop RocketMQ $BACK $POINT $SUCCESS"
            else
                echo -e "$FRONT Stop RocketMQ $BACK $POINT $ERROR"
            fi
            ;;
        restart)
            #echo "$PIDFILE does not exist........ process is not running"
            echo -e "$FRONT Restart RocketMQ... $BACK"
            runuser $USER -m -c "$EXEC restart"
            reslt_va=$?
            if [[ $reslt_va -eq 0 ]];then
                echo -e "$FRONT Restart RocketMQ $BACK $POINT $SUCCESS"
            else
                echo -e "$FRONT Restart RocketMQ $BACK $POINT $ERROR"
            fi
    	    ;;
        status)
            do_status
    	    ;;    
        *)
            echo "Please use start or stop or status as first argument"
            ;;
    esac
    
    
posted @ 2022-04-06 16:46  surive  阅读(676)  评论(0编辑  收藏  举报