RabbitMQ的安装及集群搭建方法

Posted 边学边做

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ的安装及集群搭建方法相关的知识,希望对你有一定的参考价值。

RabbitMQ安装
1 安装erlang
下载地址:http://www.erlang.org/downloads
博主这里采用的是otp_src_19.1.tar.gz (200MB+)

[[email protected] util]# tar zxvf  otp_src_19.1.tar.gz
[[email protected] util]# cd otp_src_19.1 
[[email protected] otp_src_19.1]# ./configure --prefix=/opt/erlang
[[email protected] otp_src_19.1]# make 
[[email protected] otp_src_19.1]# make install

修改/etc/profile文件,添加下面的环境变量:

ERLANG_HOME=/opt/erlang
export PATH=$PATH:$ERLANG_HOME/bin
export ERLANG_HOME

最后执行source /etc/profile让文件生效。

2 安装simpleJson
下载地址:https://pypi.python.org/pypi/simplejson/
博主这里采用的是simplejson-3.10.0.tar.gz

[[email protected] util]# tar zxvf simplejson-3.10.0.tar.gz
[[email protected] util]# cd simplejson-3.10.0
[[email protected] simplejson-3.10.0]# python setup.py install

3 安装rabbitmq
下载地址:http://www.rabbitmq.com/releases/rabbitmq-server/
博主这里用的版本就有很多了,比如2.7.0, 3.4.0, 3.4.1, 3.4.2, 3.4.3, 3.5.7, 3.6.0等等,不过都是用的rabbitmq-server-generic-unix-..*.tar.gz的版本。
这里以3.5.7举例:

[[email protected] util]# tar zvxf rabbitmq-server-generic-unix-3.5.7.tar.gz -C /opt
[[email protected] util]# cd /opt
[[email protected] util]# mv rabbitmq_server-3.5.7 rabbitmq

修改/etc/profile文件,添加下面的环境变量:

export PATH=$PATH:/opt/rabbitmq/sbin

最后执行source /etc/profile让文件生效。
运行:rabbitmq-server -detached
之后运行rabbitmqctl status查看rabbitmq是否正常启动:

[[email protected] rabbitmq]# rabbitmqctl status
Status of node ‘[email protected]‘ ...
[{pid,25020},
 {running_applications,
     [{rabbitmq_management,"RabbitMQ Management Console","3.5.7"},
      {rabbitmq_management_agent,"RabbitMQ Management Agent","3.5.7"},
      {rabbit,"RabbitMQ","3.5.7"},
      {mnesia,"MNESIA  CXC 138 12","4.14.1"},
      {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.5.7"},
      {webmachine,"webmachine","1.10.3-rmq3.5.7-gite9359c7"},
      {mochiweb,"MochiMedia Web Server","2.7.0-rmq3.5.7-git680dba8"},
      {os_mon,"CPO  CXC 138 46","2.4.1"},
      {amqp_client,"RabbitMQ AMQP Client","3.5.7"},
      {inets,"INETS  CXC 138 49","6.3.3"},
      {xmerl,"XML parser","1.3.12"},
      {sasl,"SASL  CXC 138 11","3.0.1"},
      {stdlib,"ERTS  CXC 138 10","3.1"},
      {kernel,"ERTS  CXC 138 10","5.1"}]},
 {os,{unix,linux}},
 {erlang_version,
     "Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:64] [hipe] [kernel-poll:true]\n"},
 {memory,
     [{total,60110136},
      {connection_readers,0},
      {connection_writers,0},
      {connection_channels,0},
      {connection_other,5664},
      {queue_procs,1028160},
      {queue_slave_procs,1576056},
      {plugins,8008840},
      {other_proc,6115560},
      {mnesia,197760},
      {mgmt_db,14219256},
      {msg_index,58288},
      {other_ets,1181200},
      {binary,633896},
      {code,17650127},
      {atom,711593},
      {other_system,8723736}]},
 {alarms,[]},
 {listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]},
 {vm_memory_high_watermark,0.4},
 {vm_memory_limit,3301929779},
 {disk_free_limit,50000000},
 {disk_free,35754786816},
 {file_descriptors,
     [{total_limit,924},{total_used,6},{sockets_limit,829},{sockets_used,1}]},
 {processes,[{limit,1048576},{used,195}]},
 {run_queue,0},
 {uptime,139660}]

RabbitMQ集群搭建方法
这里展示的是多机集群的部署,如果机器受限,可以选择单机集群部署,详细可以参考《RabbitMQ单机多实例配置》。
这里选用三台主机,主机名分别是hidden1, hidden2, hidden3
1 在这三台机器中安装rabbitmq-server, 参考上面。

2 读取其中一个节点的cookie, 并复制到其他节点(节点之间通过cookie确定相互是否可通信)。
cookie存放在/var/lib/rabbitmq/.erlang.cookie或者$HOME/.erlang.cookie中。

3 逐个启动节点 rabbitmq-server -detached

4 查看各节点的状态: rabbitmqctl status, rabbitmqctl cluster_status

5 配置各节点的hosts文件( vim /etc/hosts)
xxx.xxx.xxx.xxx hidden1
xxx.xxx.xxx.xxx hidden2
xxx.xxx.xxx.xxx hidden3

6 建立集群
以hidden1为主节点,在hidden2上:
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster [email protected]
rabbitmqctl start_app
hidden3上的操作与hidden2的雷同。
最后通过rabbitmqctl cluster_status查看集群的状态信息:

[{nodes,[{disc,[[email protected],‘[email protected]‘,‘[email protected]‘]}]},
 {running_nodes,[[email protected],‘[email protected]‘,‘[email protected]‘]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]}]

7 在访问web(http://xxx.xxx.xxx.xxx:15672)时, 如果在Overview中的Nodes部分看到“Node statistics not available”的信息,说明在该节点上web管理插件还未启用。直接运行rabbitmq-plugins enable rabbitmq_management即可。

rabbitmq搭建集群踩坑记
三台节点启动之后,想要使用s146作为集群主节点,在s151上将该节点加入s146集群时,报如下错误。

[[email protected] rabbitmq]# rabbitmqctl join_cluster [email protected]
Clustering node [email protected] with [email protected] ...
Error: unable to connect to nodes [[email protected]]: nodedown

DIAGNOSTICS
===========

attempted to contact: [[email protected]]

[email protected]:
  * connected to epmd (port 4369) on s146
  * epmd reports node ‘rabbit‘ running on port 25672
  * TCP connection succeeded but Erlang distribution failed
  * suggestion: hostname mismatch?
  * suggestion: is the cookie set correctly?
  * suggestion: is the Erlang distribution using TLS?

current node details:
- node name: ‘[email protected]‘
- home dir: /root
- cookie hash: X8M4gNn7BBuuU5WV1cHdhw==

我在网上看到了很多人都碰到了这个错误,错误提示中也说了三种解决方案。
suggestion: hostname mismatch?
hostname配置是否正确,对于hostname的配置可以参考https://my.oschina.net/mingshashan/blog/741988

suggestion: is the cookie set correctly?
.erlang.cookie设置是否正确,.erlang.cookie文件里面的字符串相当于一个密钥一样,一个节点想要加入某个集群,必须要和这个机器中的所有节点的.erlang.cookie内容一样,所以通常将主节点的.erlang.cookie文件分发给其它节点即可,关键是这个节点可能出现在两个位置/var/lib/rabbitmq/.erlang.cookie或者$HOME/.erlang.cookie。这样一来如何确定自己的.erlang.cookie文件在哪儿呢?这个要看rabbitmq的日志文件,如下所示:

=INFO REPORT==== 9-Apr-2018::14:57:21 ===
node           : [email protected]
home dir       : /root
config file(s) : /opt/rabbitmq/sbin/../etc/rabbitmq/rabbitmq.config (not found)
cookie hash    : HfR3DHABJqfPDV+hBgdzXQ==
log            : /opt/rabbitmq/sbin/../var/log/rabbitmq/[email protected]
sasl log       : /opt/rabbitmq/sbin/../var/log/rabbitmq/[email protected]
database dir   : /opt/rabbitmq/sbin/../var/lib/rabbitmq/mnesia/[email protected]

其中的home dir就是你的.erlang.cookie文件存放的目录,而这个cookie hash就是.erlang.cookie里面的值hash转换得到的,你可以比较cookie hash的值是不是一致来判断你的.erlang.cookie是否一致。我之前一直以为我的.erlang.cookie文件在/var/lib/rabbitmq/下,所以屡次操作都失败了,看了日志后发现节点的cookie hash不一致,才发现原来一直将.erlang.cookie这个文件的位置搞错了,所以看日志很重要。

suggestion: is the Erlang distribution using TLS?
这种情况目前好像还没碰到过。

参考: https://blog.csdn.net/u013256816/article/details/53524766
https://blog.csdn.net/u012062455/article/details/79867080

以上是关于RabbitMQ的安装及集群搭建方法的主要内容,如果未能解决你的问题,请参考以下文章

rabbitmq3.8.2安装步骤及集群配置

Docker搭建RabbitMQ集群

带你从头进行RabbitMQ安装集群搭建镜像队列配置和代码验证

RabbitMQ——集群搭建步骤详解

RabbitMQ——集群搭建步骤详解

rabbitmq集群搭建方法简介(测试机linux centos)