本次記錄安裝RabbitMQ的過程,只針對MAC下單機版安裝、單機集羣安裝方法以及配置haproxy負載均衡。html
消息發送原理:
應用程序和Rabbit Server之間會建立一個TCP鏈接,一旦TCP打開,並經過了認證,認證就是你試圖鏈接Rabbit以前發送的Rabbit服務器鏈接信息和用戶名和密碼,有點像程序鏈接數據庫,使用Java有兩種鏈接認證的方式,後面代碼會詳細介紹,一旦認證經過你的應用程序和Rabbit就建立了一條AMQP信道(Channel)。
信道是建立在「真實」TCP上的虛擬鏈接,AMQP命令都是經過信道發送出去的,每一個信道都會有一個惟一的ID,不管是發佈消息,訂閱隊列或者介紹消息都是經過信道完成的。
爲何不經過TCP直接發送命令?
對於操做系統來講建立和銷燬TCP會話是很是昂貴的開銷,假設高峯期每秒有成千上萬條鏈接,每一個鏈接都要建立一條TCP會話,這就形成了TCP鏈接的巨大浪費,並且操做系統每秒能建立的TCP也是有限的,所以很快就會遇到系統瓶頸。
若是咱們每一個請求都使用一條TCP鏈接,既知足了性能的須要,又能確保每一個鏈接的私密性,這就是引入信道概念的緣由。
RabbitMQ基礎概念:
ConnectionFactory(鏈接管理器)、Channel(信道)、Exchange(交換器)、Queue(隊列)、RoutingKey(路由鍵)、BindingKey(綁定鍵)。
MAC下配置 brew 命令行: brew install rabbitmq 會幫助安裝依賴的各個環境, 如Erlang等, 方便安裝和卸載。node
基礎命令:linux
啓動 : ./rabbitmq-server -detached (後臺啓動)git
查看狀態:./rabbitmqctl statusgithub
查看web管理界面:http://localhost:15672/#/web
中止:./rabbitmqctl stop 正則表達式
命令結果集:算法
zhouguangfengdeMacBook-Pro:sbin feng$ ls cuttlefish rabbitmq-defaults rabbitmq-diagnostics rabbitmq-env rabbitmq-plugins rabbitmq-server rabbitmqadmin rabbitmqctl zhouguangfengdeMacBook-Pro:sbin feng$ ./rabbitmq-server -detached Warning: PID file not written; -detached was passed. zhouguangfengdeMacBook-Pro:sbin feng$ ./rabbitmqctl status Status of node rabbit@localhost ... [{pid,16051}, {running_applications, [{rabbitmq_management,"RabbitMQ Management Console","3.7.5"}, {rabbitmq_management_agent,"RabbitMQ Management Agent","3.7.5"}, {rabbitmq_mqtt,"RabbitMQ MQTT Adapter","3.7.5"}, {rabbitmq_stomp,"RabbitMQ STOMP plugin","3.7.5"}, {rabbitmq_amqp1_0,"AMQP 1.0 support for RabbitMQ","3.7.5"}, {amqp_client,"RabbitMQ AMQP Client","3.7.5"}, {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.7.5"}, {rabbit,"RabbitMQ","3.7.5"}, {rabbit_common, "Modules shared by rabbitmq-server and rabbitmq-erlang-client", "3.7.5"}, {ranch_proxy_protocol,"Ranch Proxy Protocol Transport","1.5.0"}, {cowboy,"Small, fast, modern HTTP server.","2.2.2"}, {ranch,"Socket acceptor pool for TCP protocols.","1.5.0"}, {ssl,"Erlang/OTP SSL application","8.2.6"}, {public_key,"Public key infrastructure","1.5.2"}, {asn1,"The Erlang ASN1 compiler version 5.0.5","5.0.5"}, {recon,"Diagnostic tools for production use","2.3.2"}, {jsx,"a streaming, evented json parsing toolkit","2.8.2"}, {cowlib,"Support library for manipulating Web protocols.","2.1.0"}, {os_mon,"CPO CXC 138 46","2.4.4"}, {xmerl,"XML parser","1.3.16"}, {crypto,"CRYPTO","4.2.2"}, {sasl,"SASL CXC 138 11","3.1.2"}, {amqp10_common, "Modules shared by rabbitmq-amqp1.0 and rabbitmq-amqp1.0-client", "3.7.5"}, {inets,"INETS CXC 138 49","6.5.1"}, {mnesia,"MNESIA CXC 138 12","4.15.3"}, {lager,"Erlang logging framework","3.5.1"}, {goldrush,"Erlang event stream processor","0.1.9"}, {compiler,"ERTS CXC 138 10","7.1.5"}, {syntax_tools,"Syntax tools","2.1.4"}, {stdlib,"ERTS CXC 138 10","3.4.5"}, {kernel,"ERTS CXC 138 10","5.4.3"}]}, {os,{unix,darwin}}, {erlang_version, "Erlang/OTP 20 [erts-9.3.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:64] [hipe] [kernel-poll:true] [dtrace]\n"}, {memory, [{connection_readers,0}, {connection_writers,0}, {connection_channels,0}, {connection_other,32472}, {queue_procs,67296}, {queue_slave_procs,0}, {plugins,2195128}, {other_proc,27648040}, {metrics,203960}, {mgmt_db,294896}, {mnesia,84800}, {other_ets,2473304}, {binary,209024}, {msg_index,30064}, {code,29314811}, {atom,1131721}, {other_system,14138316}, {allocated_unused,17768616}, {reserved_unallocated,0}, {strategy,rss}, {total,[{erlang,77823832},{rss,90247168},{allocated,95592448}]}]}, {alarms,[]}, {listeners, [{clustering,25672,"::"}, {amqp,5672,"127.0.0.1"}, {stomp,61613,"::"}, {mqtt,1883,"::"}, {http,15672,"::"}]}, {vm_memory_calculation_strategy,rss}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,3435973836}, {disk_free_limit,50000000}, {disk_free,168907264000}, {file_descriptors, [{total_limit,156},{total_used,4},{sockets_limit,138},{sockets_used,0}]}, {processes,[{limit,1048576},{used,413}]}, {run_queue,0}, {uptime,75}, {kernel,{net_ticktime,60}}] zhouguangfengdeMacBook-Pro:sbin feng$ ./rabbitmqctl stop Stopping and halting node rabbit@localhost ... zhouguangfengdeMacBook-Pro:sbin feng$
RabbitMQ默認Port Access:參考http://www.rabbitmq.com/clustering.html#selinux-portsshell
http: RabbitMQ默認web管理界面端口數據庫
amqp:RabbitMQ默認客戶端通訊端口
因爲在一個機器啓動多個服務,會存在端口衝突問題,啓動的時候須要設置不一樣的端口和節點名稱。這裏啓動三個節點(rabbit_node_one、rabbit_node_two、rabbit_node_three)組合一個集羣。
一、分別啓動三個節點:
rabbit_node_one: 使用默認端口
RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit_node_one ./rabbitmq-server -detached
rabbit_node_two:
RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}] -rabbitmq_stomp tcp_listeners [61614] -rabbitmq_mqtt tcp_listeners [1884]" RABBITMQ_NODENAME=rabbit_node_two ./rabbitmq-server -detached
rabbit_node_three:
RABBITMQ_NODE_PORT=5674 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}] -rabbitmq_stomp tcp_listeners [61615] -rabbitmq_mqtt tcp_listeners [1885]" RABBITMQ_NODENAME=rabbit_node_three ./rabbitmq-server -detached
二、集羣配置: rabbit_node_one 爲主節點
配置rabbit_node_two 爲持久化節點 從節點:
./rabbitmqctl -n rabbit_node_two stop_app
./rabbitmqctl -n rabbit_node_two reset
./rabbitmqctl -n rabbit_node_two join_cluster rabbit_node_one
./rabbitmqctl -n rabbit_node_two start_app
rabbit_node_three 爲內存節點 從節點::
./rabbitmqctl -n rabbit_node_three stop_app
./rabbitmqctl -n rabbit_node_three reset
./rabbitmqctl -n rabbit_node_three join_cluster rabbit_node_one --ram
./rabbitmqctl -n rabbit_node_three start_app
查詢集羣狀態:
./rabbitmqctl cluster_status -n rabbit_node_one
命令結果集:
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit_node_one ./rabbitmq-server -detached Warning: PID file not written; -detached was passed. zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}] -rabbitmq_stomp tcp_listeners [61614] -rabbitmq_mqtt tcp_listeners [1884]" RABBITMQ_NODENAME=rabbit_node_two ./rabbitmq-server -detached Warning: PID file not written; -detached was passed. zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ RABBITMQ_NODE_PORT=5674 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}] -rabbitmq_stomp tcp_listeners [61615] -rabbitmq_mqtt tcp_listeners [1885]" RABBITMQ_NODENAME=rabbit_node_three ./rabbitmq-server -detached Warning: PID file not written; -detached was passed. zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ps aux | grep rabbitmq zhouguangfeng 22499 0.1 0.9 5981744 79132 ?? S 10:42上午 0:04.65 /usr/local/Cellar/erlang/20.3.6/lib/erlang/erts-9.3.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 1280000 -K true -- -root /usr/local/Cellar/erlang/20.3.6/lib/erlang -progname erl -- -home /Users/zhouguangfeng -- -pa /usr/local/Cellar/rabbitmq/3.7.5/ebin -noshell -noinput -s rabbit boot -sname rabbit_node_three -boot /usr/local/opt/erlang/lib/erlang/bin/start_clean -kernel inet_default_connect_options [{nodelay,true}] -rabbit tcp_listeners [{"127.0.0.1",5674}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit lager_log_root "/usr/local/var/log/rabbitmq" -rabbit lager_default_file "/usr/local/var/log/rabbitmq/rabbit_node_three.log" -rabbit lager_upgrade_file "/usr/local/var/log/rabbitmq/rabbit_node_three_upgrade.log" -rabbit enabled_plugins_file "/usr/local/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/local/Cellar/rabbitmq/3.7.5/plugins" -rabbit plugins_expand_dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_three-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_three" -rabbitmq_management listener [{port,15674}] -rabbitmq_stomp tcp_listeners [61615] -rabbitmq_mqtt tcp_listeners [1885] -kernel inet_dist_listen_min 25674 -kernel inet_dist_listen_max 25674 -noshell -noinput zhouguangfeng 22167 0.0 0.9 5978272 79400 ?? S 10:42上午 0:05.36 /usr/local/Cellar/erlang/20.3.6/lib/erlang/erts-9.3.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 1280000 -K true -- -root /usr/local/Cellar/erlang/20.3.6/lib/erlang -progname erl -- -home /Users/zhouguangfeng -- -pa /usr/local/Cellar/rabbitmq/3.7.5/ebin -noshell -noinput -s rabbit boot -sname rabbit_node_one -boot /usr/local/opt/erlang/lib/erlang/bin/start_clean -kernel inet_default_connect_options [{nodelay,true}] -rabbit tcp_listeners [{"127.0.0.1",5672}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit lager_log_root "/usr/local/var/log/rabbitmq" -rabbit lager_default_file "/usr/local/var/log/rabbitmq/rabbit_node_one.log" -rabbit lager_upgrade_file "/usr/local/var/log/rabbitmq/rabbit_node_one_upgrade.log" -rabbit enabled_plugins_file "/usr/local/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/local/Cellar/rabbitmq/3.7.5/plugins" -rabbit plugins_expand_dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_one-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_one" -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672 -noshell -noinput zhouguangfeng 22719 0.0 0.0 4295984 528 s001 U+ 10:42上午 0:00.00 grep rabbitmq zhouguangfeng 22315 0.0 1.0 5974940 79984 ?? S 10:42上午 0:05.42 /usr/local/Cellar/erlang/20.3.6/lib/erlang/erts-9.3.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 1280000 -K true -- -root /usr/local/Cellar/erlang/20.3.6/lib/erlang -progname erl -- -home /Users/zhouguangfeng -- -pa /usr/local/Cellar/rabbitmq/3.7.5/ebin -noshell -noinput -s rabbit boot -sname rabbit_node_two -boot /usr/local/opt/erlang/lib/erlang/bin/start_clean -kernel inet_default_connect_options [{nodelay,true}] -rabbit tcp_listeners [{"127.0.0.1",5673}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit lager_log_root "/usr/local/var/log/rabbitmq" -rabbit lager_default_file "/usr/local/var/log/rabbitmq/rabbit_node_two.log" -rabbit lager_upgrade_file "/usr/local/var/log/rabbitmq/rabbit_node_two_upgrade.log" -rabbit enabled_plugins_file "/usr/local/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/local/Cellar/rabbitmq/3.7.5/plugins" -rabbit plugins_expand_dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_two-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_two" -rabbitmq_management listener [{port,15673}] -rabbitmq_stomp tcp_listeners [61614] -rabbitmq_mqtt tcp_listeners [1884] -kernel inet_dist_listen_min 25673 -kernel inet_dist_listen_max 25673 -noshell -noinput zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_two stop_app Stopping rabbit application on node rabbit_node_two@zhouguangfengdeMacBook-Pro ... zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_two reset Resetting node rabbit_node_two@zhouguangfengdeMacBook-Pro ... zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_two join_cluster rabbit_node_one Clustering node rabbit_node_two@zhouguangfengdeMacBook-Pro with rabbit_node_one zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_two start_app Starting node rabbit_node_two@zhouguangfengdeMacBook-Pro ... completed with 6 plugins. Resetting node rabbit_node_three@zhouguangfengdeMacBook-Pro ... zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl cluster_status -n rabbit_node_one Cluster status of node rabbit_node_one@zhouguangfengdeMacBook-Pro ... [{nodes,[{disc,['rabbit_node_one@zhouguangfengdeMacBook-Pro', 'rabbit_node_two@zhouguangfengdeMacBook-Pro']}]}, {running_nodes,['rabbit_node_two@zhouguangfengdeMacBook-Pro', 'rabbit_node_one@zhouguangfengdeMacBook-Pro']}, {cluster_name,<<"rabbit_node_one@zhouguangfengdeMacBook-Pro">>}, {partitions,[]}, {alarms,[{'rabbit_node_two@zhouguangfengdeMacBook-Pro',[]}, {'rabbit_node_one@zhouguangfengdeMacBook-Pro',[]}]}] zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_three reset zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_three join_cluster rabbit_node_one --ram Clustering node rabbit_node_three@zhouguangfengdeMacBook-Pro with rabbit_node_one zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_three start_app Starting node rabbit_node_three@zhouguangfengdeMacBook-Pro ... completed with 6 plugins. zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl cluster_status -n rabbit_node_one Cluster status of node rabbit_node_one@zhouguangfengdeMacBook-Pro ... [{nodes,[{disc,['rabbit_node_one@zhouguangfengdeMacBook-Pro', 'rabbit_node_two@zhouguangfengdeMacBook-Pro']}, {ram,['rabbit_node_three@zhouguangfengdeMacBook-Pro']}]}, {running_nodes,['rabbit_node_three@zhouguangfengdeMacBook-Pro', 'rabbit_node_two@zhouguangfengdeMacBook-Pro', 'rabbit_node_one@zhouguangfengdeMacBook-Pro']}, {cluster_name,<<"rabbit_node_one@zhouguangfengdeMacBook-Pro">>}, {partitions,[]}, {alarms,[{'rabbit_node_three@zhouguangfengdeMacBook-Pro',[]}, {'rabbit_node_two@zhouguangfengdeMacBook-Pro',[]}, {'rabbit_node_one@zhouguangfengdeMacBook-Pro',[]}]}] zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$
訪問web頁面: http://localhost:15672/#/ http://localhost:15673/#/ http://localhost:15674/#/
能夠看到集羣中三個節點已經正常啓動,rabbit_node_three 設置爲RAM內存節點,其餘爲磁盤節點(Disk and RAM Nodes)。RabbitMQ要求在集羣中至少有一個磁盤節點,全部其餘節點能夠是內存節點,當節點加入或者離開集羣時,必需要將該變動通知到至少一個磁盤節點。若是集羣中惟一的一個磁盤節點崩潰的話,集羣仍然能夠保持運行,可是沒法進行其餘操做(增刪改查),直到節點恢復。
測試集羣節點down掉狀況:對rabbit_node_two 進行kill操做 和 恢復操做, 查詢集羣狀況
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl cluster_status -n rabbit_node_one Cluster status of node rabbit_node_one@zhouguangfengdeMacBook-Pro ... [{nodes,[{disc,['rabbit_node_one@zhouguangfengdeMacBook-Pro', 'rabbit_node_two@zhouguangfengdeMacBook-Pro']}, {ram,['rabbit_node_three@zhouguangfengdeMacBook-Pro']}]}, {running_nodes,['rabbit_node_three@zhouguangfengdeMacBook-Pro', 'rabbit_node_two@zhouguangfengdeMacBook-Pro', 'rabbit_node_one@zhouguangfengdeMacBook-Pro']}, {cluster_name,<<"rabbit_node_one@zhouguangfengdeMacBook-Pro">>}, {partitions,[]}, {alarms,[{'rabbit_node_three@zhouguangfengdeMacBook-Pro',[]}, {'rabbit_node_two@zhouguangfengdeMacBook-Pro',[]}, {'rabbit_node_one@zhouguangfengdeMacBook-Pro',[]}]}] zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ps aux | grep rabbitmq zhouguangfeng 41776 0.0 0.0 4295984 536 s001 U+ 11:05上午 0:00.00 grep rabbitmq zhouguangfeng 22499 0.0 0.7 5988224 58552 ?? S 10:42上午 0:12.23 /usr/local/Cellar/erlang/20.3.6/lib/erlang/erts-9.3.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 1280000 -K true -- -root /usr/local/Cellar/erlang/20.3.6/lib/erlang -progname erl -- -home /Users/zhouguangfeng -- -pa /usr/local/Cellar/rabbitmq/3.7.5/ebin -noshell -noinput -s rabbit boot -sname rabbit_node_three -boot /usr/local/opt/erlang/lib/erlang/bin/start_clean -kernel inet_default_connect_options [{nodelay,true}] -rabbit tcp_listeners [{"127.0.0.1",5674}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit lager_log_root "/usr/local/var/log/rabbitmq" -rabbit lager_default_file "/usr/local/var/log/rabbitmq/rabbit_node_three.log" -rabbit lager_upgrade_file "/usr/local/var/log/rabbitmq/rabbit_node_three_upgrade.log" -rabbit enabled_plugins_file "/usr/local/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/local/Cellar/rabbitmq/3.7.5/plugins" -rabbit plugins_expand_dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_three-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_three" -rabbitmq_management listener [{port,15674}] -rabbitmq_stomp tcp_listeners [61615] -rabbitmq_mqtt tcp_listeners [1885] -kernel inet_dist_listen_min 25674 -kernel inet_dist_listen_max 25674 -noshell -noinput zhouguangfeng 22315 0.0 0.7 5984200 61532 ?? S 10:42上午 0:15.88 /usr/local/Cellar/erlang/20.3.6/lib/erlang/erts-9.3.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 1280000 -K true -- -root /usr/local/Cellar/erlang/20.3.6/lib/erlang -progname erl -- -home /Users/zhouguangfeng -- -pa /usr/local/Cellar/rabbitmq/3.7.5/ebin -noshell -noinput -s rabbit boot -sname rabbit_node_two -boot /usr/local/opt/erlang/lib/erlang/bin/start_clean -kernel inet_default_connect_options [{nodelay,true}] -rabbit tcp_listeners [{"127.0.0.1",5673}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit lager_log_root "/usr/local/var/log/rabbitmq" -rabbit lager_default_file "/usr/local/var/log/rabbitmq/rabbit_node_two.log" -rabbit lager_upgrade_file "/usr/local/var/log/rabbitmq/rabbit_node_two_upgrade.log" -rabbit enabled_plugins_file "/usr/local/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/local/Cellar/rabbitmq/3.7.5/plugins" -rabbit plugins_expand_dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_two-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_two" -rabbitmq_management listener [{port,15673}] -rabbitmq_stomp tcp_listeners [61614] -rabbitmq_mqtt tcp_listeners [1884] -kernel inet_dist_listen_min 25673 -kernel inet_dist_listen_max 25673 -noshell -noinput zhouguangfeng 22167 0.0 0.7 5984748 56532 ?? S 10:42上午 0:13.23 /usr/local/Cellar/erlang/20.3.6/lib/erlang/erts-9.3.1/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 1280000 -K true -- -root /usr/local/Cellar/erlang/20.3.6/lib/erlang -progname erl -- -home /Users/zhouguangfeng -- -pa /usr/local/Cellar/rabbitmq/3.7.5/ebin -noshell -noinput -s rabbit boot -sname rabbit_node_one -boot /usr/local/opt/erlang/lib/erlang/bin/start_clean -kernel inet_default_connect_options [{nodelay,true}] -rabbit tcp_listeners [{"127.0.0.1",5672}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit lager_log_root "/usr/local/var/log/rabbitmq" -rabbit lager_default_file "/usr/local/var/log/rabbitmq/rabbit_node_one.log" -rabbit lager_upgrade_file "/usr/local/var/log/rabbitmq/rabbit_node_one_upgrade.log" -rabbit enabled_plugins_file "/usr/local/etc/rabbitmq/enabled_plugins" -rabbit plugins_dir "/usr/local/Cellar/rabbitmq/3.7.5/plugins" -rabbit plugins_expand_dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_one-plugins-expand" -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/usr/local/var/lib/rabbitmq/mnesia/rabbit_node_one" -kernel inet_dist_listen_min 25672 -kernel inet_dist_listen_max 25672 -noshell -noinput zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ kill -9 22315 zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl cluster_status -n rabbit_node_one Cluster status of node rabbit_node_one@zhouguangfengdeMacBook-Pro ... [{nodes,[{disc,['rabbit_node_one@zhouguangfengdeMacBook-Pro', 'rabbit_node_two@zhouguangfengdeMacBook-Pro']}, {ram,['rabbit_node_three@zhouguangfengdeMacBook-Pro']}]}, {running_nodes,['rabbit_node_three@zhouguangfengdeMacBook-Pro', 'rabbit_node_one@zhouguangfengdeMacBook-Pro']}, {cluster_name,<<"rabbit_node_one@zhouguangfengdeMacBook-Pro">>}, {partitions,[]}, {alarms,[{'rabbit_node_three@zhouguangfengdeMacBook-Pro',[]}, {'rabbit_node_one@zhouguangfengdeMacBook-Pro',[]}]}] zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_two start_app Starting node rabbit_node_three@zhouguangfengdeMacBook-Pro ... zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl cluster_status -n rabbit_node_one Cluster status of node rabbit_node_one@zhouguangfengdeMacBook-Pro ... [{nodes,[{disc,['rabbit_node_one@zhouguangfengdeMacBook-Pro', 'rabbit_node_two@zhouguangfengdeMacBook-Pro']}, {ram,['rabbit_node_three@zhouguangfengdeMacBook-Pro']}]}, {running_nodes,['rabbit_node_three@zhouguangfengdeMacBook-Pro', 'rabbit_node_two@zhouguangfengdeMacBook-Pro','rabbit_node_one@zhouguangfengdeMacBook-Pro']}, {cluster_name,<<"rabbit_node_one@zhouguangfengdeMacBook-Pro">>}, {partitions,[]}, {alarms,[{'rabbit_node_three@zhouguangfengdeMacBook-Pro',[]}, {'rabbit_node_one@zhouguangfengdeMacBook-Pro',[]}]}] zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$
能夠看到rabbit_node_two節點down以後,集羣正常工做。 當節點恢復以後,主動加入集羣。
一、配置RabbitMQ鏡像功能
使用Rabbit鏡像功能,須要基於RabbitMQ策略來實現,策略是用來控制和修改羣集範圍的某個vhost隊列行爲和Exchange行爲
zhouguangfengdeMacBook-Pro:~ zhouguangfeng$ cd /usr/local/Cellar/rabbitmq/3.7.5/sbin/ zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ls cuttlefish rabbitmq-diagnostics rabbitmq-plugins rabbitmqadmin run.sh rabbitmq-defaults rabbitmq-env rabbitmq-server rabbitmqctl zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl set_policy -p hrsystem ha-allqueue "^" '{"ha-mode":"all"}' -n rabbit_node_one Setting policy "ha-allqueue" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "hrsystem" ... zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$
這行命令建立一個名稱爲hrsystem的vhost,策略名稱爲ha-allqueue,策略模式爲 all 即複製到全部節點,包含新增節點,策略正則表達式爲 「^」 表示全部匹配全部隊列名稱。
詳情參考:http://www.rabbitmq.com/ha.html
二、安裝haproxy,使用mac下安裝工具brew : brew install haproxy
查看安裝版本和目錄:
zhouguangfengdeMacBook-Pro:/ zhouguangfeng$ haproxy -version HA-Proxy version 1.8.12-8a200c7 2018/06/27 Copyright 2000-2018 Willy Tarreau <willy@haproxy.org> zhouguangfengdeMacBook-Pro:/ zhouguangfeng$ cd /usr/local/Cellar/haproxy/1.8.12/ .brew/ INSTALL_RECEIPT.json README homebrew.mxcl.haproxy.plist CHANGELOG LICENSE bin/ share/ zhouguangfengdeMacBook-Pro:/ zhouguangfeng$ cd /usr/local/Cellar/haproxy/1.8.12/bin/ zhouguangfengdeMacBook-Pro:bin zhouguangfeng$ ls haproxy run.sh zhouguangfengdeMacBook-Pro:bin zhouguangfeng$
配置haproxy.cfg 並啓動:
zhouguangfengdeMacBook-Pro:bin zhouguangfeng$ cd /usr/local/etc/ zhouguangfengdeMacBook-Pro:bin zhouguangfeng$ mkdir haproxy zhouguangfengdeMacBook-Pro:bin zhouguangfeng$ cd haproxy/ zhouguangfengdeMacBook-Pro:haproxy zhouguangfeng$ vim haproxy.cfg -- 內容以下 haproxy.cfg zhouguangfengdeMacBook-Pro:haproxy zhouguangfeng$ cat haproxy.cfg global log 127.0.0.1 local0 # 日誌 daemon # 後臺運行 maxconn 256 # 最大鏈接數 defaults mode http # 默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK stats enable stats uri /haproxy-stats stats refresh 10s monitor-uri /haproxy-test balance roundrobin option httpclose # 每次請求完畢後主動關閉http通道 option forwardfor # 若是後端服務器須要得到客戶端真實ip須要配置的參數,能夠從Http Header中得到客戶端ip timeout connect 5000ms # 鏈接超時時間 timeout client 50000ms # 客戶端鏈接超時時間 timeout server 50000ms # 服務器端鏈接超時時間 #################################################################### # listen http_front # bind 0.0.0.0:1080 #監聽端口 #stats refresh 30s #統計頁面自動刷新時間 #stats uri /haproxy?stats #統計頁面url #listen private_monitoring :8100 #stats realm Haproxy Manager #統計頁面密碼框上提示文本 #stats auth guest:guest #統計頁面用戶名和密碼設置 listen private_monitoring # haproxy 監控 bind 0.0.0.0:8100 #監聽端口 mode http option httplog # 採用http日誌格式 stats enable stats uri /stats #設置haproxy監控地址爲http://localhost:8100/stats stats refresh 30s #統計頁面自動刷新時間 stats auth guest:guest #添加用戶名密碼認證 #####################我把RabbitMQ的管理界面也放在HAProxy後面了############################### listen rabbitmq_admin bind 0.0.0.0:8004 server rabbit_node_one 127.0.0.1:15672 server rabbit_node_two 127.0.0.1:15673 server rabbit_node_three 127.0.0.1:15674 #################################################################### listen rabbitmq_cluster bind 0.0.0.0:6672 option tcplog mode tcp timeout client 3h timeout server 3h option clitcpka balance roundrobin #負載均衡算法(#banlance roundrobin 輪詢,balance source 保存session值,支持static-rr,leastconn,first,uri等參數) #balance url_param userid #balance url_param session_id check_post 64 #balance hdr(User-Agent) #balance hdr(host) #balance hdr(Host) use_domain_only #balance rdp-cookie #balance leastconn #balance source //ip server rabbit_node_one 127.0.0.1:5672 check inter 5s rise 2 fall 3 #check inter 2000 是檢測心跳頻率,rise 2是2次正確認爲服務器可用,fall 3是3次失敗認爲服務器不可用 server rabbit_node_two 127.0.0.1:5673 check inter 5s rise 2 fall 3 server rabbit_node_three 127.0.0.1:5674 check inter 5s rise 2 fall 3 zhouguangfengdeMacBook-Pro:haproxy zhouguangfeng$ pwd /usr/local/etc/haproxy zhouguangfengdeMacBook-Pro:haproxy zhouguangfeng$ ls haproxy.cfg zhouguangfengdeMacBook-Pro:haproxy zhouguangfeng$ cd /usr/local/Cellar/haproxy/1.8.12/ .brew/ INSTALL_RECEIPT.json README homebrew.mxcl.haproxy.plist CHANGELOG LICENSE bin/ share/ zhouguangfengdeMacBook-Pro:haproxy zhouguangfeng$ cd /usr/local/Cellar/haproxy/1.8.12/bin/ zhouguangfengdeMacBook-Pro:bin zhouguangfeng$ ls haproxy run.sh zhouguangfengdeMacBook-Pro:bin zhouguangfeng$ cat run.sh #!/bin/sh ./haproxy -f /usr/local/etc/haproxy/haproxy.cfg -d zhouguangfengdeMacBook-Pro:bin zhouguangfeng$ ./run.sh [WARNING] 187/111607 (50864) : config : log format ignored for proxy 'private_monitoring' since it has no log address. [WARNING] 187/111607 (50864) : config : monitor-uri will be ignored for proxy 'rabbitmq_cluster' (needs 'mode http'). [WARNING] 187/111607 (50864) : config : log format ignored for proxy 'rabbitmq_cluster' since it has no log address. [WARNING] 187/111607 (50864) : config : 'stats' statement ignored for proxy 'rabbitmq_cluster' as it requires HTTP mode. [WARNING] 187/111607 (50864) : config : 'option forwardfor' ignored for proxy 'rabbitmq_cluster' as it requires HTTP mode. Available polling systems : kqueue : pref=300, test result OK poll : pref=200, test result OK select : pref=150, test result OK Total: 3 (3 usable), will use kqueue. Available filters : [SPOE] spoe [COMP] compression [TRACE] trace Using kqueue() as the polling mechanism.
訪問:統計頁面 http://localhost:8100/stats 用戶和密碼是上述配置的guest
訪問RabbitMQ統一管理頁面:http://localhost:8004/#/ 用戶和密碼是初始化密碼 guest和guest
Clustering Guide:http://www.rabbitmq.com/clustering.html
RabbitMQ單機集羣搭建:https://blog.csdn.net/Java_HuiLong/article/details/73718714
RabbitMQ分佈式集羣架:http://www.javashuo.com/article/p-giwbpbcc-gd.html
RabbitMQ3.6.3集羣搭建+HAProxy1.6作負載均衡:https://www.cnblogs.com/lion.net/p/5725474.html
RabbitMQ集羣及負載均衡搭建:https://blog.csdn.net/zpwmail/article/details/78066862
HAProxy Configuration Manual :http://cbonte.github.io/haproxy-dconv/1.8/configuration.html