Distributed Rabbitmq brokers的實現方式有三種,分別是clustering、federation、shovel。本節圍繞clustering(集羣)講述。html
搭建Rabbitmq集羣的方法有不少種,參考Ways of Forming a Cluster,在此做者使用env variables來搭建集羣。node
Rabbitmq是經過ip和port來爲客戶端提供服務的,因此配置Rabbitmq實例的基本要求就是綁定ip:port(默認爲localhost:5672),若是單機部署過mysql、Redis等工具,想必這個原理很好理解了。若是不理解請繼續看示例:python
# 啓動第一個節點 $ RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit1 rabbitmq-server -detached #啓動第二個節點 RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit2 rabbitmq-server -detached Warning: PID file not written; -detached was passed. # 此時查看端口狀態會發現第二個節點並無起來!!!
此處報錯,查看日誌:mysql
$ less /var/log/rabbitmq/rabbit2.log Error description: init:do_boot/3 init:start_em/1 rabbit:start_it/1 line 446 rabbit:broker_start/0 line 322 rabbit:start_apps/2 line 542 app_utils:manage_applications/6 line 126 lists:foldl/3 line 1263 rabbit:'-handle_app_error/1-fun-0-'/3 line 638 throw:{could_not_start,rabbitmq_management, {rabbitmq_management, {bad_return, {{rabbit_mgmt_app,start,[normal,[]]}, {'EXIT', {{could_not_start_listener, [{port,15672}], {shutdown, {failed_to_start_child,ranch_acceptors_sup, {listen_error,rabbit_web_dispatch_sup_15672,eaddrinuse}}}}, {gen_server,call, [rabbit_web_dispatch_registry, {add,rabbit_mgmt, [{port,15672}], #Fun<rabbit_web_dispatch.0.82427196>, [{'_',[], [{[],[],cowboy_static, {priv_file,rabbitmq_management,"www/index.html"}}, {[<<"api">>,<<"overview">>],[],rabbit_mgmt_wm_overview,[]}, {[<<"api">>,<<"cluster-name">>], [],rabbit_mgmt_wm_cluster_name,[]}, {[<<"api">>,<<"nodes">>],[],rabbit_mgmt_wm_nodes,[]}, {[<<"api">>,<<"nodes">>,node],[],rabbit_mgmt_wm_node,[]},
總的來講就是Rabbitmq_management啓動失敗,查資料後緣由以下:web管理插件端口占用,因此還要指定其web插件佔用的端口號。git
# 更改參數後啓動 $ RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}]" RABBITMQ_NODENAME=rabbit2 rabbitmq-server -detached # 查看端口狀態 $ netstat -lntp tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 10253/beam.smp tcp 0 0 0.0.0.0:15673 0.0.0.0:* LISTEN 13922/beam.smp tcp 0 0 127.0.0.1:9797 0.0.0.0:* LISTEN 632/python2 tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 10253/beam.smp tcp 0 0 0.0.0.0:25673 0.0.0.0:* LISTEN 13922/beam.smp tcp6 0 0 :::4369 :::* LISTEN 10150/epmd tcp6 0 0 :::5672 :::* LISTEN 10253/beam.smp tcp6 0 0 :::5673 :::* LISTEN 13922/beam.smp # rabbit一、rabbit2啓動成功 # 啓動第三個節點 RABBITMQ_NODE_PORT=5674 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}]" RABBITMQ_NODENAME=rabbit3 rabbitmq-server -detached
如今三個節點都已啓動,狀態:github
$ netstat -lntp tcp 0 0 0.0.0.0:4369 0.0.0.0:* LISTEN 10150/epmd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 773/sshd tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 10253/beam.smp tcp 0 0 0.0.0.0:15673 0.0.0.0:* LISTEN 13922/beam.smp tcp 0 0 0.0.0.0:15674 0.0.0.0:* LISTEN 14910/beam.smp tcp 0 0 127.0.0.1:9797 0.0.0.0:* LISTEN 632/python2 tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 10253/beam.smp tcp 0 0 0.0.0.0:25673 0.0.0.0:* LISTEN 13922/beam.smp tcp 0 0 0.0.0.0:25674 0.0.0.0:* LISTEN 14910/beam.smp tcp6 0 0 :::4369 :::* LISTEN 10150/epmd tcp6 0 0 :::22 :::* LISTEN 773/sshd tcp6 0 0 :::5672 :::* LISTEN 10253/beam.smp tcp6 0 0 :::5673 :::* LISTEN 13922/beam.smp tcp6 0 0 :::5674 :::* LISTEN 14910/beam.smp
我把rabbit1做爲主節點,剩下兩個設置爲子節點(主節點不動,配置兩個子節點便可)。web
將rabbit2加入集羣:redis
$ rabbitmqctl -n rabbit2 stop_app $ rabbitmqctl -n rabbit2 reset $ rabbitmqctl -n rabbit2 join_cluster rabbit1@`hostname -s` $ rabbitmqctl -n rabbit2 start_app
將rabbit3加入集羣(同理):sql
$ rabbitmqctl -n rabbit3 stop_app $ rabbitmqctl -n rabbit3 reset $ rabbitmqctl -n rabbit3 join_cluster rabbit1@`hostname -s` $ rabbitmqctl -n rabbit3 start_app
查看集羣狀態:api
$ rabbitmqctl cluster_status -n rabbit1@host3 Cluster status of node rabbit1@host3 ... [{nodes,[{disc,[rabbit1@host3,rabbit2@host3,rabbit3@host3]}]}, {running_nodes,[rabbit3@host3,rabbit2@host3,rabbit1@host3]}, {cluster_name,<<"rabbit1@host3">>}, {partitions,[]}, {alarms,[{rabbit3@host3,[]},{rabbit2@host3,[]},{rabbit1@host3,[]}]}]
在UI_Management頁面查看集羣狀態(server_ip:port,在此能夠經過1567二、1567三、15674任何一個端口進行訪問):
若是想添加新的節點,只須要執行本節操做步驟便可!
刪除節點:
$ rabbitmqctl forget_cluster_node [--offline] <existing_cluster_member_node> # 測試未成功
參考:
文件描述符:https://ro-che.info/articles/2017-03-26-increase-open-files-limit
新版 rabbitmq.conf 模板:https://github.com/rabbitmq/rabbitmq-server/blob/v3.7.x/docs/rabbitmq.conf.example
listeners.tcp.default = host_ip:5673 # 默認端口 loopback_users = none # 取消guest用戶登陸限制
舊版 rabbitmq.config 模板:https://github.com/rabbitmq/rabbitmq-server/blob/v3.7.x/docs/rabbitmq.config.example
advaced.config 模板:https://github.com/rabbitmq/rabbitmq-server/blob/master/docs/advanced.config.example
插件:http://www.rabbitmq.com/plugins.html
/usr/lib/rabbitmq/lib/rabbitmq_server-3.7.9/plugins
rabbitmq-env.conf 配置:http://www.rabbitmq.com/configure.html#define-environment-variables
/etc/hosts
本次用兩個節點搭建rabbitmq集羣:
主機 | 系統 | Rabbitmq-server版本 | 節點 |
---|---|---|---|
host1 | Centos 7.2 | 3.7.9 | node1 |
host2 | Centos 7.2 | 3.7.9 | node2 |
啓動node1:
$ systemctl start rabbitmq-server
啓動node2:
# 啓動前須要先將node1的erlang_cookie拷貝到node2,保持一致 $ cat /var/lib/rabbitmq/.erlang.cookie $ systemctl start rabbitmq-server
erlang.cookie是erlang實現分佈式的必要文件,erlang分佈式的每一個節點上要保持相同的.erlang.cookie文件,同時保證文件的權限是400。
將node2 加入到node1節點,node2須要執行如下操做:
reset:目的是清除節點上的歷史數據(若是不清除,沒法將節點加入到集羣)
$ rabbitmqctl stop_app $ rabbitmqctl reset
join
$ rabbitmqctl join_cluster rabbit@redis01 Clustering node rabbit@infra01 with rabbit@redis01 $ rabbitmqctl start_app # 查看集羣狀態 $ rabbitmqctl cluster_status Cluster status of node rabbit@infra01 ... [{nodes,[{disc,[rabbit@infra01,rabbit@redis01]}]}, {running_nodes,[rabbit@redis01,rabbit@infra01]}, {cluster_name,<<"rabbit@redis01">>}, {partitions,[]}, {alarms,[{rabbit@redis01,[]},{rabbit@infra01,[]}]}]
rabbitmqctl命令:http://www.rabbitmq.com/rabbitmqctl.8.html
rabbitmqadmin:https://rabbit-new.chunyu.me/cli/index.html
加入開機啓動: systemctl enable rabbitmq-server
開始搭建集羣的時候報錯:
Clustering node rabbit@host2 with rabbit@host1 Error: unable to perform an operation on node 'rabbit@redis01'. Please see diagnostics information and suggestions below. Most common reasons for this are: * Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues) * CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server) * Target node is not running In addition to the diagnostics info below: * See the CLI, clustering and networking guides on http://rabbitmq.com/documentation.html to learn more * Consult server logs on node rabbit@redis01 DIAGNOSTICS =========== attempted to contact: [rabbit@host1] rabbit@host1: * connected to epmd (port 4369) on host1 * epmd reports node 'rabbit' uses port 25672 for inter-node and CLI tool traffic * TCP connection succeeded but Erlang distribution failed * Authentication failed (rejected by the remote node), please check the Erlang cookie Current node details: * node name: 'rabbitmqcli-23827-rabbit@host2' * effective user's home directory: /var/lib/rabbitmq * Erlang cookie hash: t9ttNYffM0xwbMi8k2DA4w==
報錯緣由:node1節點和node2節點的erlang.cookie不一致 解決辦法:各個節點統一使用node1節點的erlang.cookie(文檔中已說明)