部署RabbitMQ

  • A+
所属分类:Linux
高性能企业级服务器首台5折

RabbitMQ 是一个开源的 AMQP 实现,服务器端用 Erlang 语言编写,支持多种客户端,如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP 等,支持 AJAX。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。

部署方式

阿里云服务器下部署 RabbitMQ 提供两种部署方式:

一般推荐镜像部署适合新手使用更加快捷方便,安装包部署以及手动部署适合对 Linux 命令有基本了解的用户,可以满足用户个性化部署的要求。本教程主要介绍镜像和手工部署的方式。

镜像部署

  1. 单击 RabbitMQ 环境 ( CentOS7.3 Erlang19.3 ) 进入镜像详情页。
  2. 单击 立即购买,按提示步骤购买 ECS 实例。
  3. 登录 ECS 管理控制台
  4. 在左边导航栏里,单击 实例,进入 ECS 实例列表页。
  5. 选择所购 ECS 实例所在的地域,并找到所购 ECS 实例,在 IP 地址 列获取该实例的公网 IP 地址。
  6. 在浏览器地址栏中输入公网 IP 地址,下载操作文档。下载操作文档
  7. 使用 putty 登录 Linux 服务器,请参考连接Linux实例;忘记 root 密码,请参考重置实例密码
  8. 初始化 rabbitmq。
    1. 1
      <span class="pln"> cd </span><span class="pun">/</span><span class="pln">root</span><span class="pun">/</span><span class="pln">oneinstack</span>
    2. 1
       <span class="pun">./</span><span class="pln">init_rabbitmq</span><span class="pun">.</span><span class="pln">sh</span>

    初始化 rabbitmq

  9. 进入管理页面,浏览器访问 
    1
    http://公网IP:15672

     。在浏览器中访问实例

手工部署

  • 系统平台:CentOS 7.3
  • rabbitmq版本:rabbitmq-server -3.6.9
  • erlang版本:erlang19.3
  • JDK版本:JDK1.8.0_121

前提准备

创建一般用户 rabbitmq,运行 rabbitmq。

  1. 1
    <span class="pln">useradd rabbitmq</span>

设置 Linux 主机名。

CentOS 7 修改 /etc/hostname,CentOS 6 修改 /etc/sysconfig/network,下面以 CentOS 7 为例:

  1. 1
    <span class="pln">echo rabbit1 </span><span class="pun">&gt;</span> <span class="str">/etc/</span><span class="pln">hostname</span>
  2. 1
    <span class="pln">hostname rabbit1</span>
  3. 1
    <span class="kwd">exit</span>  <span class="com">#退出重新登录</span>

修改 hostname

安装依赖包

  1. 1
    <span class="pln">yum </span><span class="pun">-</span><span class="pln">y install make gcc gcc</span><span class="pun">-</span><span class="pln">c</span><span class="pun">++</span><span class="pln"> m4 ncurses</span><span class="pun">-</span><span class="pln">devel openssl</span><span class="pun">-</span><span class="pln">devel unixODBC</span><span class="pun">-</span><span class="pln">devel</span>

安装依赖包

源代码下载

  • 下载 Erlang:
    1
    wget http://erlang.org/download/otp_src_19.3.tar.gz

  • 下载 rabbitmq:
    1
    wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.9/rabbitmq-server-generic-unix-3.6.9.tar.xz

安装 Erlang

  1. 1
    <span class="pln">tar xzf otp_src_19</span><span class="pun">.</span><span class="lit">3.tar</span><span class="pun">.</span><span class="pln">gz </span><span class="com">#解压</span>
  2. 1
    <span class="pln">cd otp_src_19</span><span class="pun">.</span><span class="lit">3</span>
  3. 1
    <span class="pun">./</span><span class="pln">configure </span><span class="pun">--</span><span class="pln">prefix</span><span class="pun">=</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">erlang </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">shared</span><span class="pun">-</span><span class="pln">zlib </span><span class="pun">--</span><span class="kwd">with</span><span class="pun">-</span><span class="pln">ssl </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">threads </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">smp</span><span class="pun">-</span><span class="pln">support </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">kernel</span><span class="pun">-</span><span class="pln">poll </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">hipe </span><span class="pun">--</span><span class="pln">without</span><span class="pun">-</span><span class="pln">javac</span>
  4. 1
    <span class="pln">make </span><span class="pun">&amp;&amp;</span><span class="pln"> make install</span>

解压 RabbitMQ

  1. 解压 rabbitmq-server-generic-unix-3.6.9.tar.xz 。
    1. 1
      <span class="pln">tar xvJf rabbitmq</span><span class="pun">-</span><span class="pln">server</span><span class="pun">-</span><span class="kwd">generic</span><span class="pun">-</span><span class="pln">unix</span><span class="pun">-</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">9.tar</span><span class="pun">.</span><span class="pln">xz</span>
    2. 1
      <span class="pln">mv rabbitmq_server</span><span class="pun">-</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">9</span>  <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">rabbitmq</span>
  2. rabbitmq 环境变量配置。
    1. 1
      <span class="pln">sed </span><span class="pun">-</span><span class="pln">i </span><span class="str">'s@^ERL_DIR=.*@ERL_DIR=/usr/local/erlang/bin/@'</span> <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">-</span><span class="pln">defaults</span>
    2. 1
      <span class="pln">sed </span><span class="pun">-</span><span class="pln">i </span><span class="str">'s@^LOG_BASE=.*@LOG_BASE=/usr/local/rabbitmq/var/log/rabbitmq@'</span> <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">-</span><span class="pln">defaults</span>
    3. 1
      <span class="pln">mkdir </span><span class="pun">-</span><span class="pln">p </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/{</span><span class="pln">lib</span><span class="pun">,</span><span class="pln">log</span><span class="pun">}/</span><span class="pln">rabbitmq</span>
  3. 一般用户(rabbitmq)运行 RabbitMQ。
    1. 1
      <span class="pln">wget http</span><span class="pun">:</span><span class="com">//pkgs.fedoraproject.org/cgit/rpms/rabbitmq-server.git/plain/rabbitmq-script-wrapper</span>
    2. 1
      <span class="pln">sed </span><span class="pun">-</span><span class="pln">i </span><span class="str">'s@cd /var/lib/rabbitmq@cd /usr/local/rabbitmq/var/lib/rabbitmq@g'</span><span class="pln"> rabbitmq</span><span class="pun">-</span><span class="pln">script</span><span class="pun">-</span><span class="pln">wrapper  </span><span class="com">#更改rabbitmq数据存储目录</span>
    3. 1
      <span class="pln">sed </span><span class="pun">-</span><span class="pln">i </span><span class="str">'s@/usr/lib/rabbitmq/bin/@/usr/local/rabbitmq/sbin/@g'</span><span class="pln"> rabbitmq</span><span class="pun">-</span><span class="pln">script</span><span class="pun">-</span><span class="pln">wrapper</span>
    4. 1
      <span class="pln">chmod </span><span class="pun">+</span><span class="pln">x rabbitmq</span><span class="pun">-</span><span class="pln">script</span><span class="pun">-</span><span class="pln">wrapper</span>
    5. 1
      <span class="pln">cp rabbitmq</span><span class="pun">-</span><span class="pln">script</span><span class="pun">-</span><span class="pln">wrapper </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">rabbitmqctl</span>
    6. 1
      <span class="pln">cp rabbitmq</span><span class="pun">-</span><span class="pln">script</span><span class="pun">-</span><span class="pln">wrapper </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">-</span><span class="pln">server</span>
    7. 1
      <span class="pln">cp rabbitmq</span><span class="pun">-</span><span class="pln">script</span><span class="pun">-</span><span class="pln">wrapper </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">-</span><span class="pln">plugins</span>
    8. 1
      <span class="pln">chown </span><span class="pun">-</span><span class="pln">R rabbitmq</span><span class="pun">.</span><span class="pln">rabbitmq </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="kwd">var</span>
  4. rabbitmq 日志割接。
    1. 1
      <span class="pln">cat </span><span class="pun">&gt;&gt;</span> <span class="str">/etc/</span><span class="pln">logrotate</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">-</span><span class="pln">server </span><span class="pun">&lt;&lt;</span><span class="pln"> EOF</span>
    2. 1
      <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="com">/*.log {</span>
    3. 1
      <span class="com">weekly</span>
    4. 1
      <span class="com">missingok</span>
    5. 1
      <span class="com">rotate 20</span>
    6. 1
      <span class="com">compress</span>
    7. 1
      <span class="com">delaycompress</span>
    8. 1
      <span class="com">notifempty</span>
    9. 1
      <span class="com">sharedscripts</span>
    10. 1
      <span class="com">postrotate</span>
    11. 1
      <span class="com"> /sbin/service rabbitmq-server rotate-logs &gt; /dev/null</span>
    12. 1
      <span class="com">endscript</span>
    13. 1
      <span class="com">}</span>
    14. 1
      <span class="com">EOF</span>
  5. rabbitmq 启动脚本。
    1. 1
      <span class="pln">vi </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">-</span><span class="pln">server</span>
    2. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash">!/bin/sh</span></span>
    3. 1
      <span class="com"><span class="hljs-meta">#</span></span>
    4. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> rabbitmq-server RabbitMQ broker</span></span>
    5. 1
      <span class="com"><span class="hljs-meta">#</span></span>
    6. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> chkconfig: - 80 05</span></span>
    7. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> description: Enable AMQP service provided by RabbitMQ</span></span>
    8. 1
      <span class="com"><span class="hljs-meta">#</span></span>
    9. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"><span class="hljs-comment">## BEGIN INIT INFO</span></span></span>
    10. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> Provides:          rabbitmq-server</span></span>
    11. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> Required-Start:    <span class="hljs-variable">$remote_fs</span> <span class="hljs-variable">$network</span></span></span>
    12. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> Required-Stop:     <span class="hljs-variable">$remote_fs</span> <span class="hljs-variable">$network</span></span></span>
    13. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> Description:       RabbitMQ broker</span></span>
    14. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> Short-Description: Enable AMQP service provided by RabbitMQ broker</span></span>
    15. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"><span class="hljs-comment">## END INIT INFO</span></span></span>
    16. 1
      <span class="com"><span class="hljs-meta">#</span><span class="bash"> Source <span class="hljs-keyword">function</span> library.</span></span>
    17. 1
      <span class="pun">.</span> <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">functions</span>
    18. 1
      <span class="pln">PATH</span><span class="pun">=</span><span class="str">/sbin:/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">sbin</span><span class="pun">:</span><span class="str">/bin:/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">:</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">erlang</span><span class="pun">/</span><span class="pln">bin</span>
    19. 1
      <span class="pln">NAME</span><span class="pun">=</span><span class="pln">rabbitmq</span><span class="pun">-</span><span class="pln">server</span>
    20. 1
      <span class="pln">DAEMON</span><span class="pun">=</span><span class="str">/usr/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">$</span><span class="pun">{</span><span class="pln">NAME</span><span class="pun">}</span>
    21. 1
      <span class="pln">CONTROL</span><span class="pun">=</span><span class="str">/usr/</span><span class="pln">sbin</span><span class="pun">/</span><span class="pln">rabbitmqctl</span>
    22. 1
      <span class="pln">DESC</span><span class="pun">=</span><span class="pln">rabbitmq</span><span class="pun">-</span><span class="pln">server</span>
    23. 1
      <span class="pln">USER</span><span class="pun">=</span><span class="pln">rabbitmq</span>
    24. 1
      <span class="pln">ROTATE_SUFFIX</span><span class="pun">=</span>
    25. 1
      <span class="pln">INIT_LOG_DIR</span><span class="pun">=</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">rabbitmq</span>
    26. 1
      <span class="pln">PID_FILE</span><span class="pun">=</span><span class="str">/var/</span><span class="pln">run</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="pln">pid</span>
    27. 1
      <span class="pln">START_PROG</span><span class="pun">=</span><span class="str">"daemon"</span>
    28. 1
      <span class="pln">LOCK_FILE</span><span class="pun">=</span><span class="str">/var/</span><span class="kwd">lock</span><span class="pun">/</span><span class="pln">subsys</span><span class="pun">/</span><span class="pln">$NAME</span>
    29. 1
      <span class="pln">test </span><span class="pun">-</span><span class="pln">x $DAEMON </span><span class="pun">||</span> <span class="kwd">exit</span> <span class="lit">0</span>
    30. 1
      <span class="pln">test </span><span class="pun">-</span><span class="pln">x $CONTROL </span><span class="pun">||</span> <span class="kwd">exit</span> <span class="lit">0</span>
    31. 1
      <span class="pln">RETVAL</span><span class="pun">=</span><span class="lit">0</span>
    32. 1
      <span class="kwd">set</span> <span class="pun">-</span><span class="pln">e</span>
    33. 1
      <span class="pun">[</span> <span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">default</span><span class="pun">/</span><span class="pln">$</span><span class="pun">{</span><span class="pln">NAME</span><span class="pun">}</span> <span class="pun">]</span> <span class="pun">&amp;&amp;</span> <span class="pun">.</span> <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">default</span><span class="pun">/</span><span class="pln">$</span><span class="pun">{</span><span class="pln">NAME</span><span class="pun">}</span>
    34. 1
      <span class="pun">[</span> <span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">sysconfig</span><span class="pun">/</span><span class="pln">$</span><span class="pun">{</span><span class="pln">NAME</span><span class="pun">}</span> <span class="pun">]</span> <span class="pun">&amp;&amp;</span> <span class="pun">.</span> <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">sysconfig</span><span class="pun">/</span><span class="pln">$</span><span class="pun">{</span><span class="pln">NAME</span><span class="pun">}</span>
    35. 1
      <span class="pln">ensure_pid_dir </span><span class="pun">()</span> <span class="pun">{</span>
    36. 1
      <span class="pln"> PID_DIR</span><span class="pun">=</span><span class="str">`dirname ${PID_FILE}`</span>
    37. 1
       <span class="kwd">if</span> <span class="pun">[</span> <span class="pun">!</span> <span class="pun">-</span><span class="pln">d $</span><span class="pun">{</span><span class="pln">PID_DIR</span><span class="pun">}</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    38. 1
      <span class="pln">     mkdir </span><span class="pun">-</span><span class="pln">p $</span><span class="pun">{</span><span class="pln">PID_DIR</span><span class="pun">}</span>
    39. 1
      <span class="pln">     chown </span><span class="pun">-</span><span class="pln">R $</span><span class="pun">{</span><span class="pln">USER</span><span class="pun">}:</span><span class="pln">$</span><span class="pun">{</span><span class="pln">USER</span><span class="pun">}</span><span class="pln"> $</span><span class="pun">{</span><span class="pln">PID_DIR</span><span class="pun">}</span>
    40. 1
      <span class="pln">     chmod </span><span class="lit">755</span><span class="pln"> $</span><span class="pun">{</span><span class="pln">PID_DIR</span><span class="pun">}</span>
    41. 1
       <span class="kwd">fi</span>
    42. 1
      <span class="pun">}</span>
    43. 1
      <span class="pln">remove_pid </span><span class="pun">()</span> <span class="pun">{</span>
    44. 1
      <span class="pln"> rm </span><span class="pun">-</span><span class="pln">f $</span><span class="pun">{</span><span class="pln">PID_FILE</span><span class="pun">}</span>
    45. 1
      <span class="pln"> rmdir </span><span class="str">`dirname ${PID_FILE}`</span> <span class="pun">||</span> <span class="pun">:</span>
    46. 1
      <span class="pun">}</span>
    47. 1
      <span class="pln">start_rabbitmq </span><span class="pun">()</span> <span class="pun">{</span>
    48. 1
      <span class="pln"> status_rabbitmq quiet</span>
    49. 1
       <span class="kwd">if</span> <span class="pun">[</span><span class="pln"> $RETVAL </span><span class="pun">=</span> <span class="lit">0</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    50. 1
      <span class="pln">     echo </span><span class="typ">RabbitMQ</span> <span class="kwd">is</span><span class="pln"> currently running</span>
    51. 1
       <span class="kwd">else</span>
    52. 1
      <span class="pln">     RETVAL</span><span class="pun">=</span><span class="lit">0</span>
    53. 1
           <span class="com"># RABBIT_NOFILES_LIMIT from /etc/sysconfig/rabbitmq-server is not handled</span>
    54. 1
           <span class="com"># automatically</span>
    55. 1
           <span class="kwd">if</span> <span class="pun">[</span> <span class="str">"$RABBITMQ_NOFILES_LIMIT"</span> <span class="pun">];</span> <span class="kwd">then</span>
    56. 1
      <span class="pln">             ulimit </span><span class="pun">-</span><span class="pln">n $RABBITMQ_NOFILES_LIMIT</span>
    57. 1
           <span class="kwd">fi</span>
    58. 1
      <span class="pln">     ensure_pid_dir</span>
    59. 1
           <span class="kwd">set</span> <span class="pun">+</span><span class="pln">e</span>
    60. 1
      <span class="pln">     RABBITMQ_PID_FILE</span><span class="pun">=</span><span class="pln">$PID_FILE $START_PROG $DAEMON \</span>
    61. 1
               <span class="pun">&gt;</span> <span class="str">"${INIT_LOG_DIR}/startup_log"</span><span class="pln"> \</span>
    62. 1
               <span class="lit">2</span><span class="pun">&gt;</span> <span class="str">"${INIT_LOG_DIR}/startup_err"</span><span class="pln"> \</span>
    63. 1
               <span class="lit">0</span><span class="pun">&lt;&amp;-</span> <span class="pun">&amp;</span>
    64. 1
      <span class="pln">     $CONTROL wait $PID_FILE </span><span class="pun">&gt;</span><span class="str">/dev/</span><span class="kwd">null</span> <span class="lit">2</span><span class="pun">&gt;&amp;</span><span class="lit">1</span>
    65. 1
      <span class="pln">     RETVAL</span><span class="pun">=</span><span class="pln">$</span><span class="pun">?</span>
    66. 1
           <span class="kwd">set</span> <span class="pun">-</span><span class="pln">e</span>
    67. 1
           <span class="kwd">case</span> <span class="str">"$RETVAL"</span> <span class="kwd">in</span>
    68. 1
               <span class="lit">0</span><span class="pun">)</span>
    69. 1
      <span class="pln">             echo SUCCESS</span>
    70. 1
                   <span class="kwd">if</span> <span class="pun">[</span> <span class="pun">-</span><span class="pln">n </span><span class="str">"$LOCK_FILE"</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    71. 1
      <span class="pln">                 touch $LOCK_FILE</span>
    72. 1
                   <span class="kwd">fi</span>
    73. 1
                   <span class="pun">;;</span>
    74. 1
               <span class="pun">*)</span>
    75. 1
      <span class="pln">             remove_pid</span>
    76. 1
      <span class="pln">             echo FAILED </span><span class="pun">-</span><span class="pln"> check $</span><span class="pun">{</span><span class="pln">INIT_LOG_DIR</span><span class="pun">}/</span><span class="pln">startup_\{log</span><span class="pun">,</span><span class="pln"> _err\}</span>
    77. 1
      <span class="pln">             RETVAL</span><span class="pun">=</span><span class="lit">1</span>
    78. 1
                   <span class="pun">;;</span>
    79. 1
           <span class="kwd">esac</span>
    80. 1
       <span class="kwd">fi</span>
    81. 1
      <span class="pun">}</span>
    82. 1
      <span class="pln">stop_rabbitmq </span><span class="pun">()</span> <span class="pun">{</span>
    83. 1
      <span class="pln"> status_rabbitmq quiet</span>
    84. 1
       <span class="kwd">if</span> <span class="pun">[</span><span class="pln"> $RETVAL </span><span class="pun">=</span> <span class="lit">0</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    85. 1
           <span class="kwd">set</span> <span class="pun">+</span><span class="pln">e</span>
    86. 1
      <span class="pln">     $CONTROL stop $</span><span class="pun">{</span><span class="pln">PID_FILE</span><span class="pun">}</span> <span class="pun">&gt;</span><span class="pln"> $</span><span class="pun">{</span><span class="pln">INIT_LOG_DIR</span><span class="pun">}/</span><span class="pln">shutdown_log </span><span class="lit">2</span><span class="pun">&gt;</span><span class="pln"> $</span><span class="pun">{</span><span class="pln">INIT_LOG_DIR</span><span class="pun">}/</span><span class="pln">shutdown_err</span>
    87. 1
      <span class="pln">     RETVAL</span><span class="pun">=</span><span class="pln">$</span><span class="pun">?</span>
    88. 1
           <span class="kwd">set</span> <span class="pun">-</span><span class="pln">e</span>
    89. 1
           <span class="kwd">if</span> <span class="pun">[</span><span class="pln"> $RETVAL </span><span class="pun">=</span> <span class="lit">0</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    90. 1
      <span class="pln">         remove_pid</span>
    91. 1
               <span class="kwd">if</span> <span class="pun">[</span> <span class="pun">-</span><span class="pln">n </span><span class="str">"$LOCK_FILE"</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    92. 1
      <span class="pln">             rm </span><span class="pun">-</span><span class="pln">f $LOCK_FILE</span>
    93. 1
               <span class="kwd">fi</span>
    94. 1
           <span class="kwd">else</span>
    95. 1
      <span class="pln">         echo FAILED </span><span class="pun">-</span><span class="pln"> check $</span><span class="pun">{</span><span class="pln">INIT_LOG_DIR</span><span class="pun">}/</span><span class="pln">shutdown_log</span><span class="pun">,</span><span class="pln"> _err</span>
    96. 1
           <span class="kwd">fi</span>
    97. 1
       <span class="kwd">else</span>
    98. 1
      <span class="pln">     echo </span><span class="typ">RabbitMQ</span> <span class="kwd">is</span> <span class="kwd">not</span><span class="pln"> running</span>
    99. 1
      <span class="pln">     RETVAL</span><span class="pun">=</span><span class="lit">0</span>
    100. 1
       <span class="kwd">fi</span>
    101. 1
      <span class="pun">}</span>
    102. 1
      <span class="pln">status_rabbitmq</span><span class="pun">()</span> <span class="pun">{</span>
    103. 1
       <span class="kwd">set</span> <span class="pun">+</span><span class="pln">e</span>
    104. 1
       <span class="kwd">if</span> <span class="pun">[</span> <span class="str">"$1"</span> <span class="pun">!=</span> <span class="str">"quiet"</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    105. 1
      <span class="pln">     $CONTROL status </span><span class="lit">2</span><span class="pun">&gt;&amp;</span><span class="lit">1</span>
    106. 1
       <span class="kwd">else</span>
    107. 1
      <span class="pln">     $CONTROL status </span><span class="pun">&gt;</span> <span class="str">/dev/</span><span class="kwd">null</span> <span class="lit">2</span><span class="pun">&gt;&amp;</span><span class="lit">1</span>
    108. 1
       <span class="kwd">fi</span>
    109. 1
       <span class="kwd">if</span> <span class="pun">[</span><span class="pln"> $</span><span class="pun">?</span> <span class="pun">!=</span> <span class="lit">0</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    110. 1
      <span class="pln">     RETVAL</span><span class="pun">=</span><span class="lit">3</span>
    111. 1
       <span class="kwd">fi</span>
    112. 1
       <span class="kwd">set</span> <span class="pun">-</span><span class="pln">e</span>
    113. 1
      <span class="pun">}</span>
    114. 1
      <span class="pln">rotate_logs_rabbitmq</span><span class="pun">()</span> <span class="pun">{</span>
    115. 1
       <span class="kwd">set</span> <span class="pun">+</span><span class="pln">e</span>
    116. 1
      <span class="pln"><span class="hljs-meta"> $</span><span class="bash">CONTROL rotate_logs <span class="hljs-variable">$</span></span></span><span class="pun"><span class="bash"><span class="hljs-variable">{</span></span></span><span class="pln"><span class="bash"><span class="hljs-variable">ROTATE_SUFFIX</span></span></span><span class="pun"><span class="bash"><span class="hljs-variable">}</span></span></span>
    117. 1
       <span class="kwd">if</span> <span class="pun">[</span><span class="pln"> $</span><span class="pun">?</span> <span class="pun">!=</span> <span class="lit">0</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    118. 1
      <span class="pln">     RETVAL</span><span class="pun">=</span><span class="lit">1</span>
    119. 1
       <span class="kwd">fi</span>
    120. 1
       <span class="kwd">set</span> <span class="pun">-</span><span class="pln">e</span>
    121. 1
      <span class="pun">}</span>
    122. 1
      <span class="pln">restart_running_rabbitmq </span><span class="pun">()</span> <span class="pun">{</span>
    123. 1
      <span class="pln"> status_rabbitmq quiet</span>
    124. 1
       <span class="kwd">if</span> <span class="pun">[</span><span class="pln"> $RETVAL </span><span class="pun">=</span> <span class="lit">0</span> <span class="pun">]</span> <span class="pun">;</span> <span class="kwd">then</span>
    125. 1
      <span class="pln">     restart_rabbitmq</span>
    126. 1
       <span class="kwd">else</span>
    127. 1
      <span class="pln">     echo </span><span class="typ">RabbitMQ</span> <span class="kwd">is</span> <span class="kwd">not</span><span class="pln"> runnning</span>
    128. 1
      <span class="pln">     RETVAL</span><span class="pun">=</span><span class="lit">0</span>
    129. 1
       <span class="kwd">fi</span>
    130. 1
      <span class="pun">}</span>
    131. 1
      <span class="pln">restart_rabbitmq</span><span class="pun">()</span> <span class="pun">{</span>
    132. 1
      <span class="pln"> stop_rabbitmq</span>
    133. 1
      <span class="pln"> start_rabbitmq</span>
    134. 1
      <span class="pun">}</span>
    135. 1
      <span class="kwd">case</span> <span class="str">"$1"</span> <span class="kwd">in</span>
    136. 1
      <span class="pln"> start</span><span class="pun">)</span>
    137. 1
      <span class="pln">     echo </span><span class="pun">-</span><span class="pln">n </span><span class="str">"Starting $DESC: "</span>
    138. 1
      <span class="pln">     start_rabbitmq</span>
    139. 1
      <span class="pln">     echo </span><span class="str">"$NAME."</span>
    140. 1
           <span class="pun">;;</span>
    141. 1
      <span class="pln"> stop</span><span class="pun">)</span>
    142. 1
      <span class="pln">     echo </span><span class="pun">-</span><span class="pln">n </span><span class="str">"Stopping $DESC: "</span>
    143. 1
      <span class="pln">     stop_rabbitmq</span>
    144. 1
      <span class="pln">     echo </span><span class="str">"$NAME."</span>
    145. 1
           <span class="pun">;;</span>
    146. 1
      <span class="pln"> status</span><span class="pun">)</span>
    147. 1
      <span class="pln">     status_rabbitmq</span>
    148. 1
           <span class="pun">;;</span>
    149. 1
      <span class="pln"> rotate</span><span class="pun">-</span><span class="pln">logs</span><span class="pun">)</span>
    150. 1
      <span class="pln">     echo </span><span class="pun">-</span><span class="pln">n </span><span class="str">"Rotating log files for $DESC: "</span>
    151. 1
      <span class="pln">     rotate_logs_rabbitmq</span>
    152. 1
           <span class="pun">;;</span>
    153. 1
      <span class="pln"> force</span><span class="pun">-</span><span class="pln">reload</span><span class="pun">|</span><span class="pln">reload</span><span class="pun">|</span><span class="pln">restart</span><span class="pun">)</span>
    154. 1
      <span class="pln">     echo </span><span class="pun">-</span><span class="pln">n </span><span class="str">"Restarting $DESC: "</span>
    155. 1
      <span class="pln">     restart_rabbitmq</span>
    156. 1
      <span class="pln">     echo </span><span class="str">"$NAME."</span>
    157. 1
           <span class="pun">;;</span>
    158. 1
       <span class="kwd">try</span><span class="pun">-</span><span class="pln">restart</span><span class="pun">)</span>
    159. 1
      <span class="pln">     echo </span><span class="pun">-</span><span class="pln">n </span><span class="str">"Restarting $DESC: "</span>
    160. 1
      <span class="pln">     restart_running_rabbitmq</span>
    161. 1
      <span class="pln">     echo </span><span class="str">"$NAME."</span>
    162. 1
           <span class="pun">;;</span>
    163. 1
       <span class="pun">*)</span>
    164. 1
      <span class="pln">     echo </span><span class="str">"Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}"</span> <span class="pun">&gt;&amp;</span><span class="lit">2</span>
    165. 1
      <span class="pln">     RETVAL</span><span class="pun">=</span><span class="lit">1</span>
    166. 1
           <span class="pun">;;</span>
    167. 1
      <span class="kwd">esac</span>
    168. 1
      <span class="kwd">exit</span><span class="pln"> $RETVAL</span>

    保存后,添加执行权限,并设置自启动。

    1. 1
      <span class="pln">chmod </span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">-</span><span class="pln">server</span>
    2. 1
      <span class="pln">chkconfig </span><span class="pun">--</span><span class="pln">add rabbitmq</span><span class="pun">-</span><span class="pln">server</span>
    3. 1
      <span class="pln">chkconfig rabbitmq</span><span class="pun">-</span><span class="pln">server on</span>
  6. 修改 rabbitmq.config 。特别注意默认用户名密码,请自行修改 default_user,default_pass,loopback_users。
    1. 1
      <span class="pln">cat </span><span class="pun">&gt;</span> <span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">.</span><span class="pln">config </span><span class="pun">&lt;&lt;</span><span class="pln"> EOF</span>
    2. 1
      <span class="pun">[</span>
    3. 1
      <span class="pun">{</span><span class="pln">rabbit</span><span class="pun">,</span> <span class="pun">[</span>
    4. 1
       <span class="pun">{</span><span class="pln">tcp_listeners</span><span class="pun">,[{</span><span class="str">"0.0.0.0"</span><span class="pun">,</span><span class="lit">5672</span><span class="pun">}]},</span>
    5. 1
       <span class="pun">{</span><span class="pln">tcp_listen_options</span><span class="pun">,</span> <span class="pun">[</span><span class="pln">binary</span><span class="pun">,</span> <span class="pun">{</span><span class="pln">packet</span><span class="pun">,</span><span class="pln">raw</span><span class="pun">},</span>
    6. 1
                                     <span class="pun">{</span><span class="pln">reuseaddr</span><span class="pun">,</span><span class="kwd">true</span><span class="pun">},</span>
    7. 1
                                     <span class="pun">{</span><span class="pln">backlog</span><span class="pun">,</span><span class="lit">128</span><span class="pun">},</span>
    8. 1
                                     <span class="pun">{</span><span class="pln">nodelay</span><span class="pun">,</span><span class="kwd">true</span><span class="pun">},</span>
    9. 1
                                     <span class="pun">{</span><span class="pln">exit_on_close</span><span class="pun">,</span><span class="kwd">false</span><span class="pun">},</span>
    10. 1
                                     <span class="pun">{</span><span class="pln">keepalive</span><span class="pun">,</span><span class="kwd">true</span><span class="pun">}]},</span>
    11. 1
       <span class="pun">{</span><span class="pln">default_vhost</span><span class="pun">,</span>       <span class="pun">&lt;&lt;</span><span class="str">"/"</span><span class="pun">&gt;&gt;},</span>
    12. 1
       <span class="pun">{</span><span class="pln">default_user</span><span class="pun">,</span>        <span class="pun">&lt;&lt;</span><span class="str">"guest"</span><span class="pun">&gt;&gt;},</span>
    13. 1
       <span class="pun">{</span><span class="pln">default_pass</span><span class="pun">,</span>        <span class="pun">&lt;&lt;</span><span class="str">"guest"</span><span class="pun">&gt;&gt;},</span>
    14. 1
       <span class="pun">{</span><span class="pln">loopback_users</span><span class="pun">,</span> <span class="pun">[</span><span class="str">"guest"</span><span class="pun">]},</span>
    15. 1
       <span class="pun">{</span><span class="pln">default_permissions</span><span class="pun">,</span> <span class="pun">[&lt;&lt;</span><span class="str">".*"</span><span class="pun">&gt;&gt;,</span> <span class="pun">&lt;&lt;</span><span class="str">".*"</span><span class="pun">&gt;&gt;,</span> <span class="pun">&lt;&lt;</span><span class="str">".*"</span><span class="pun">&gt;&gt;]}</span>
    16. 1
      <span class="pun">]}</span>
    17. 1
      <span class="pun">].</span>
    18. 1
      <span class="pln">EOF</span>
  7. 开启 rabbitmq manager。
    1. 1
      <span class="pln">cat </span><span class="pun">&gt;</span> <span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rabbitmq</span><span class="pun">/</span><span class="pln">enabled_plugins </span><span class="pun">&lt;&lt;</span><span class="pln"> EOF</span>
    2. 1
      <span class="pun">[</span><span class="pln">rabbitmq_management</span><span class="pun">].</span>
    3. 1
      <span class="pln">EOF</span>
  8. 启动 rabbitmq 。
    1. 1
      <span class="pln">service rabbitmq</span><span class="pun">-</span><span class="pln">server start</span>

    启动 rabbitmq

  9. 进入管理页面。浏览器访问 http://公网IP:15672。

    在浏览器中访问 rabbitmq

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: