RabbitMQ的安裝及集羣搭建方法

RabbitMQ安裝
1 安裝erlang
下載地址:http://www.erlang.org/downloads
博主這裏採用的是otp_src_19.1.tar.gz (200MB+)node

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

修改/etc/profile文件,添加下面的環境變量:python

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

最後執行source /etc/profile讓文件生效。linux

2 安裝simpleJson
下載地址:https://pypi.python.org/pypi/simplejson/
博主這裏採用的是simplejson-3.10.0.tar.gzgit

[root@hidden util]# tar zxvf simplejson-3.10.0.tar.gz
[root@hidden util]# cd simplejson-3.10.0
[root@hidden 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舉例:web

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

修改/etc/profile文件,添加下面的環境變量:json

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

最後執行source /etc/profile讓文件生效。
運行:rabbitmq-server -detached
以後運行rabbitmqctl status查看rabbitmq是否正常啓動:vim

[root@zhuzhonghua2-fqawb rabbitmq]# rabbitmqctl status
Status of node 'rabbit@zhuzhonghua2-fqawb' ...
[{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, 參考上面。cookie

2 讀取其中一個節點的cookie, 並複製到其餘節點(節點之間經過cookie肯定相互是否可通訊)。
cookie存放在/var/lib/rabbitmq/.erlang.cookie或者$HOME/.erlang.cookie中。app

3 逐個啓動節點 rabbitmq-server -detachedsocket

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 rabbit@hidden1
rabbitmqctl start_app
hidden3上的操做與hidden2的雷同。
最後經過rabbitmqctl cluster_status查看集羣的狀態信息:

[{nodes,[{disc,[rabbit@hidden1,'rabbit@hidden2','rabbit@hidden3']}]},
 {running_nodes,[rabbit@hidden1,'rabbit@hidden2','rabbit@hidden3']},
 {cluster_name,<<"rabbit@hidden1">>},
 {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集羣時,報以下錯誤。

[root@s151 rabbitmq]# rabbitmqctl join_cluster rabbit@s146
Clustering node rabbit@s151 with rabbit@s146 ...
Error: unable to connect to nodes [rabbit@s146]: nodedown

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

attempted to contact: [rabbit@s146]

rabbit@s146:
  * 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: 'rabbitmq-cli-30368@s151'
- 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           : rabbit@s151
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/rabbit@s151.log
sasl log       : /opt/rabbitmq/sbin/../var/log/rabbitmq/rabbit@s151-sasl.log
database dir   : /opt/rabbitmq/sbin/../var/lib/rabbitmq/mnesia/rabbit@s151

其中的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

相關文章
相關標籤/搜索