RabbitMQ集群搭建


注意:集群控制台頁面管理中,

 

千!萬!不!要!隨便點擊任何一個點的reset和集群reset!!

 

這樣在生產環境有一段時間內斷開原有連接,無法連接丟失大量數據的危險!!

 

注意:目前還沒有做集群的鏡像隊列和負載均衡無鏡像下隊列、消息等只保存在集群單點,容易丟失數據!!

掌握技術,考慮問題,要足夠深入,細致,全面!!

 

注意:目前單機版集群,重啟機器后需要按下面的操作重新建立集群,且集群沒有布置成服務,開機啟動等。

參考:

單機集群:

首先參考朱忠華的書里面,帶插件的單機集群搭建、配置!!然后參照下面實際操作!!

 

安裝Java進行程序測試注意:在一個控制台Shell配置好java,source /etc/profile之后,再打開另一個控制台Shell,可能這個Shell還沿用舊的PATH路徑配置,造成版本是java1.7,與程序版本不兼容。需要在這個Shell中再source /etc/profile一下!!

注意安裝配置Java時,Java路徑要配在PATH所有路徑的前面:export PATH=$JAVA_HOME/bin:$PATH,否則可能先找到其他路徑下的Java,造成版本錯誤!! 

 

下面鏈接的文章僅供參考,並沒有按他的做法搭建成功(端口沖突啟動失敗了!!

https://blog.csdn.net/Java_HuiLong/article/details/73718714

需要注意的是Web管理插件端口及其他端口沖突問題!!

https://blog.csdn.net/u013256816/article/details/53264715

多機集群:https://blog.csdn.net/u013256816/article/details/53524766

 

更改主機名:(需要修改完后重啟Linux)

https://blog.csdn.net/qi49125/article/details/77539913

這樣主機名確實永久改變了,然而rabbitmq識別的主機名仍然是localhost!所以下面的Clustering配置,

@主機名,仍然要寫localhost才能創建集群成功!!

 

單機集群注意要點:

1.rabbitmq識別的主機名仍然是localhost,所以要@localhost,如上所述

2.配置完集群后,啟動各節點的rabbitmq_management插件(前提是集群配置時該插件端口設置不沖突!),需要加上-n rabbit1@localhost這樣的參數,並且需要在各自節點的sbin目錄

也就是節點目錄要和-n rabbit1@localhost參數匹配,不能在這個節點目錄配置啟動另一個節點的插件!!而查看集群狀態時則在任何一個節點的目錄都行。

3.集群創建用戶,也要像2中一樣,指定-n rabbit1@localhost這樣的參數,才能創建成功。並且在一個節點上創建的用戶,在其他節點上也有,無需挨個創建,這在各節點的控制台可以看到

這里在任何一個節點目錄創建均可,並且-n rabbit1@localhost參數不需要和節點目錄對應,比如可以在節點3目錄創建節點1的用戶並賦予權限,3個節點均可共享,vhost配置可以在控制台直接設定

發現集群控制台是單點登錄的,一個節點的一個賬號退出,其他節點同一個賬號登錄的均退出,都需要重新登錄。

實際操作:

[root@node1 sbin]# pwd
/usr/local/rabbitmq/sbin
[root@node1 sbin]# RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit1 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" ./rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
[root@node1 sbin]# cd /usr/local/rabbitmq2/sbin
[root@node1 sbin]# RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=rabbit2 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}]" ./rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
[root@node1 sbin]# cd /usr/local/rabbitmq3/sbin
[root@node1 sbin]# RABBITMQ_NODE_PORT=5674 RABBITMQ_NODENAME=rabbit3 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}]" ./rabbitmq-server -detached
Warning: PID file not written; -detached was passed.
[root@node1 sbin]# cd /usr/local/rabbitmq2/sbin
[root@node1 sbin]# ./rabbitmqctl -n rabbit2@node1 stop_app
Stopping rabbit application on node rabbit2@node1
Error: unable to connect to node rabbit2@node1: nodedown

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

attempted to contact: [rabbit2@node1]

rabbit2@node1:
* connected to epmd (port 4369) on node1
* epmd reports node 'rabbit2' running on port 25673
* TCP connection succeeded but Erlang distribution failed

* Hostname mismatch: node "rabbit2@localhost" believes its host is different. Please ensure that hostnames resolve the same way locally and on "rabbit2@localhost"


current node details:
- node name: 'rabbitmq-cli-81@localhost'
- home dir: /root
- cookie hash: ct+K59z4oWF/n8u2DG4BFA==

[root@node1 sbin]# ./rabbitmqctl -n rabbit2@localhost stop_app
Stopping rabbit application on node rabbit2@localhost
[root@node1 sbin]# hostname
node1
[root@node1 sbin]# ./rabbitmqctl -n rabbit2@localhost reset
Resetting node rabbit2@localhost
[root@node1 sbin]# ./rabbitmqctl -n rabbit2@localhost join_cluster rabbit1@localhost
Clustering node rabbit2@localhost with rabbit1@localhost
[root@node1 sbin]# ./rabbitmqctl -n rabbit2@localhost start_app
Starting node rabbit2@localhost
[root@node1 sbin]# cd /usr/local/rabbitmq3/sbin
[root@node1 sbin]# ./rabbitmqctl -n rabbit3@localhost stop_app
Stopping rabbit application on node rabbit3@localhost
[root@node1 sbin]# ./rabbitmqctl -n rabbit3@localhost reset
Resetting node rabbit3@localhost
[root@node1 sbin]# ./rabbitmqctl -n rabbit3@localhost join_cluster rabbit1@localhost
Clustering node rabbit3@localhost with rabbit1@localhost
[root@node1 sbin]# ./rabbitmqctl -n rabbit3@localhost start_app
Starting node rabbit3@localhost
[root@node1 sbin]# cd /usr/local/rabbitmq/sbin
[root@node1 sbin]# ./rabbitmqctl -n rabbit1@localhost cluster_status
Cluster status of node rabbit1@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
{running_nodes,[rabbit3@localhost,rabbit2@localhost,rabbit1@localhost]},
{cluster_name,<<"rabbit1@localhost">>},
{partitions,[]},
{alarms,[{rabbit3@localhost,[]},
{rabbit2@localhost,[]},
{rabbit1@localhost,[]}]}]
[root@node1 sbin]# cd /usr/local/rabbitmq2/sbin
[root@node1 sbin]# ./rabbitmqctl -n rabbit2@localhost cluster_status
Cluster status of node rabbit2@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
{running_nodes,[rabbit3@localhost,rabbit1@localhost,rabbit2@localhost]},
{cluster_name,<<"rabbit1@localhost">>},
{partitions,[]},
{alarms,[{rabbit3@localhost,[]},
{rabbit1@localhost,[]},
{rabbit2@localhost,[]}]}]
[root@node1 sbin]# ./rabbitmqctl -n rabbit3@localhost cluster_status
Cluster status of node rabbit3@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
{running_nodes,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]},
{cluster_name,<<"rabbit1@localhost">>},
{partitions,[]},
{alarms,[{rabbit1@localhost,[]},
{rabbit2@localhost,[]},
{rabbit3@localhost,[]}]}]
[root@node1 sbin]# ps aux|grep rabbit
root 62732 0.7 1.7 3885124 67080 ? Sl 09:31 0:05 /usr/local/lib/erlang/erts-8.3/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 128000 -K true -- -root /usr/local/lib/erlang -progname erl -- -home /root -- -pa /usr/local/rabbitmq/ebin -noshell -noinput -s rabbit boot -sname rabbit1 -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -rabbit tcp_listeners [{"auto",5672}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/usr/local/rabbitmq/var/log/rabbitmq/rabbit1.log"} -rabbit sasl_error_logger {file,"/usr/local/rabbitmq/var/log/rabbitmq/rabbit1-sasl.log"} -rabbit enabled_plugins_file "/usr/local/rabbitmq/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/local/rabbitmq/plugins" -rabbit plugins_expand_dir "/usr/local/rabbitmq/var/lib/rabbitmq/mnesia/rabbit1-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/usr/local/rabbitmq/var/lib/rabbitmq/mnesia/rabbit1" -rabbitmq_management listener [{port,15672}] -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672 -noshell -noinput
root 62982 1.0 1.7 3889912 67172 ? Sl 09:32 0:06 /usr/local/lib/erlang/erts-8.3/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 128000 -K true -- -root /usr/local/lib/erlang -progname erl -- -home /root -- -pa /usr/local/rabbitmq2/ebin -noshell -noinput -s rabbit boot -sname rabbit2 -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -rabbit tcp_listeners [{"auto",5673}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/usr/local/rabbitmq2/var/log/rabbitmq/rabbit2.log"} -rabbit sasl_error_logger {file,"/usr/local/rabbitmq2/var/log/rabbitmq/rabbit2-sasl.log"} -rabbit enabled_plugins_file "/usr/local/rabbitmq2/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/local/rabbitmq2/plugins" -rabbit plugins_expand_dir "/usr/local/rabbitmq2/var/lib/rabbitmq/mnesia/rabbit2-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/usr/local/rabbitmq2/var/lib/rabbitmq/mnesia/rabbit2" -rabbitmq_management listener [{port,15673}] -kernel inet_dist_listen_min 25673 -kernel inet_dist_listen_max 25673 -noshell -noinput
root 63229 1.1 1.6 3887096 64512 ? Sl 09:33 0:06 /usr/local/lib/erlang/erts-8.3/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 128000 -K true -- -root /usr/local/lib/erlang -progname erl -- -home /root -- -pa /usr/local/rabbitmq3/ebin -noshell -noinput -s rabbit boot -sname rabbit3 -boot start_sasl -kernel inet_default_connect_options [{nodelay,true}] -rabbit tcp_listeners [{"auto",5674}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/usr/local/rabbitmq3/var/log/rabbitmq/rabbit3.log"} -rabbit sasl_error_logger {file,"/usr/local/rabbitmq3/var/log/rabbitmq/rabbit3-sasl.log"} -rabbit enabled_plugins_file "/usr/local/rabbitmq3/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/local/rabbitmq3/plugins" -rabbit plugins_expand_dir "/usr/local/rabbitmq3/var/lib/rabbitmq/mnesia/rabbit3-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/usr/local/rabbitmq3/var/lib/rabbitmq/mnesia/rabbit3" -rabbitmq_management listener [{port,15674}] -kernel inet_dist_listen_min 25674 -kernel inet_dist_listen_max 25674 -noshell -noinput
root 65322 0.0 0.0 112648 980 pts/0 S+ 09:42 0:00 grep --color=auto rabbit
[root@node1 sbin]# pwd
/usr/local/rabbitmq2/sbin
[root@node1 sbin]# ./rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
amqp_client
cowlib
cowboy
rabbitmq_web_dispatch
rabbitmq_management_agent
rabbitmq_management

Applying plugin configuration to rabbit@node1... failed.
* Could not contact node rabbit@node1.
Changes will take effect at broker restart.
* Options: --online - fail if broker cannot be contacted.
--offline - do not try to contact broker.
[root@node1 sbin]# ./rabbitmq-plugins -n rabbit1@localhost enable rabbitmq_management
Plugin configuration unchanged.

Applying plugin configuration to rabbit1@localhost... failed.
Error: {enabled_plugins_mismatch,"/usr/local/rabbitmq2/etc/rabbitmq/enabled_plugins",
"/usr/local/rabbitmq/etc/rabbitmq/enabled_plugins"}
[root@node1 sbin]# ./rabbitmq-plugins -n rabbit2@localhost enable rabbitmq_management
Plugin configuration unchanged.

Applying plugin configuration to rabbit2@localhost... started 6 plugins.
[root@node1 sbin]# pwd
/usr/local/rabbitmq2/sbin
[root@node1 sbin]# cd /usr/local/rabbitmq/sbin
[root@node1 sbin]# ./rabbitmq-plugins -n rabbit1@localhost enable rabbitmq_management
The following plugins have been enabled:
amqp_client
cowlib
cowboy
rabbitmq_web_dispatch
rabbitmq_management_agent
rabbitmq_management

Applying plugin configuration to rabbit1@localhost... started 6 plugins.
[root@node1 sbin]# cd /usr/local/rabbitmq3/sbin
[root@node1 sbin]# ./rabbitmq-plugins -n rabbit3@localhost enable rabbitmq_management
The following plugins have been enabled:
amqp_client
cowlib
cowboy
rabbitmq_web_dispatch
rabbitmq_management_agent
rabbitmq_management

Applying plugin configuration to rabbit3@localhost... started 6 plugins.

[root@node1 sbin]# pwd
/usr/local/rabbitmq3/sbin
[root@node1 sbin]# ./rabbitmqctl add_user admin admin
Error: unable to connect to node rabbit@node1: nodedown

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

attempted to contact: [rabbit@node1]

rabbit@node1:
* connected to epmd (port 4369) on node1
* epmd reports: node 'rabbit' not running at all
other nodes on node1: [rabbit1,rabbit2,rabbit3,
'rabbitmq-cli-82']
* suggestion: start the node

current node details:
- node name: 'rabbitmq-cli-82@localhost'
- home dir: /root
- cookie hash: ct+K59z4oWF/n8u2DG4BFA==

[root@node1 sbin]# ./rabbitmqctl -n rabbit1@localhost add_user admin admin
Creating user "admin"
[root@node1 sbin]# ./rabbitmqctl -n rabbit1@localhost set_user_tags admin administrator
Setting tags for user "admin" to [administrator]

 

設置鏡像隊列

注意:1.需要指定集群節點進行設置: -n rabbit2@localhost

2.發現同一個鏡像策略,在任何節點設置一次或多次都同步到所有節點

3.高可用同步方式選擇manual,不讓新加入slave自動同步已有鏡像,避免影響性能(同步時隊列阻塞,且RabbitMQ集群並不能熱擴展,新加入slave基本也是停機加入)

4.鏡像隊列模式選擇了在集群所有節點進行鏡像

5.正則匹配鏡像所有隊列--實際生產環境需要根據性能和業務重要性酌情選擇!!

 

實際操作:

[root@node1 sbin]# pwd
/usr/local/rabbitmq/sbin
[root@node1 sbin]# ./rabbitmqctl set_policy --priority 0 --apply-to queues mirror_queue "[\s\S]*" '{"ha-mode":"all","ha-sync-mode":"manual"}'
Error: unable to connect to node rabbit@node1: nodedown

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

attempted to contact: [rabbit@node1]

rabbit@node1:
* connected to epmd (port 4369) on node1
* epmd reports: node 'rabbit' not running at all
other nodes on node1: [rabbit1,rabbit2,rabbit3,
'rabbitmq-cli-71']
* suggestion: start the node

current node details:
- node name: 'rabbitmq-cli-71@localhost'
- home dir: /root
- cookie hash: ct+K59z4oWF/n8u2DG4BFA==

[root@node1 sbin]# ./rabbitmqctl -n rabbit2@localhost set_policy --priority 0 --apply-to queues mirror_queue "[\s\S]*" '{"ha-mode":"all","ha-sync-mode":"manual"}'
Setting policy "mirror_queue" for pattern "[\\s\\S]*" to "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"manual\"}" with priority "0"
[root@node1 sbin]# ./rabbitmqctl -n rabbit1@localhost set_policy --priority 0 --apply-to queues mirror_queue "[\s\S]*" '{"ha-mode":"all","ha-sync-mode":"manual"}'
Setting policy "mirror_queue" for pattern "[\\s\\S]*" to "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"manual\"}" with priority "0"
[root@node1 sbin]# ./rabbitmqctl -n rabbit1@localhost set_policy --priority 0 --apply-to queues mirror_queue "[\s\S]*" '{"ha-mode":"all","ha-sync-mode":"manual"}'
Setting policy "mirror_queue" for pattern "[\\s\\S]*" to "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"manual\"}" with priority "0"
[root@node1 sbin]# ./rabbitmqctl -n rabbit3@localhost set_policy --priority 0 --apply-to queues mirror_queue "[\s\S]*" '{"ha-mode":"all","ha-sync-mode":"manual"}'
Setting policy "mirror_queue" for pattern "[\\s\\S]*" to "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"manual\"}" with priority "0"

 

關於正則:

https://www.cnblogs.com/exmyth/p/7868582.html

https://blog.csdn.net/lynn_coder/article/details/79502322

https://zhidao.baidu.com/question/1447232660045793060.html

常用正則:

http://www.cnblogs.com/zxin/archive/2013/01/26/2877765.html

 

rpm安裝rabbitmq的一些沖突

1.yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel安裝時出現

PackageKit-0.8.9-11.el7.x86_64 has missing requires of PackageKit-backend及另外一個依賴沖突,參考:

https://www.cnblogs.com/stxs/p/9155858.html

使用yum命令來安裝或更新rpm和yum的依賴包。

$ yum install yum-utils

$ yum install yum-rhn-plugin

$ yum update rpm-build

$ yum update rpm-libs

$ yum update rpm-python

然后

谷歌搜索PackageKit-0.8.9-11.el7.x86_64,在http://buildlogs-seed.centos.org/c7.00.02/PackageKit/20140529191922/0.8.9-11.el7.x86_64/ 找到了相應的rpm包。

將rpm包下載下來后,用rpm命令一一安裝。我這時想起來當時用rpm -qa|grep yum命令時有一條是PackageKit-yum*****,而我現在使用rpm -qa|grep yum所得到的結果沒有PackageKit-yum*****了,我就嘗試從這個網站上下載PackageKit-yum-0.8.9-11.el7.x86_64.rpm 和PackageKit-yum-plugin-0.8.9-11.el7.x86_64.rpm,使用rpm命令安裝,發現安裝了PackageKit-yum-0.8.9-11.el7.x86_64.rpm之后系統就不提示信息了,至此問題得到完滿解決

 

2.上面的問題解決后,出現Processing Conflict: initscripts-9.49.46-1.el7.x86_64 conflicts redhat-release < 7.5-0.11,解決:

刪除沖突的包即可
rpm -e redhat-release-server-7.0-1.el7.x86_64 --nodeps

參考

http://blog.51cto.com/2484643/2140696

http://www.mamicode.com/info-detail-2369782.html

 

 

常用yum和rpm命令參考:

http://blog.chinaunix.net/uid-24677087-id-3935161.html

https://blog.csdn.net/wzyzzu/article/details/41250905

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM