本次记录安装RabbitMQ的过程,只针对MAC下单机版安装、单机集群安装方法以及配置haproxy负载均衡。

  • RabbitMQ单机版本安装
消息发送原理:
应用程序和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(绑定键)。
  • ConnectionFactory(连接管理器):应用程序与Rabbit之间建立连接的管理器,程序代码中使用;
  • Channel(信道):消息推送使用的通道;
  • Exchange(交换器):用于接受、分配消息;
  • Queue(队列):用于存储生产者的消息;
  • RoutingKey(路由键):用于把生成者的数据分配到交换器上;
  • BindingKey(绑定键):用于把交换器的消息绑定到队列上;

MAC下配置 brew  命令行: brew install rabbitmq 会帮助安装依赖的各个环境, 如Erlang等, 方便安装和卸载。

基础命令:

启动 : ./rabbitmq-server -detached (后台启动)

查看状态:./rabbitmqctl status

查看web管理界面:http://localhost:15672/#/

停止:./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-ports

http: RabbitMQ默认web管理界面端口

amqp:RabbitMQ默认客户端通信端口

  • RabbitMQ单机集群安装(适合开发练习)

由于在一个机器启动多个服务,会存在端口冲突问题,启动的时候需要设置不同的端口和节点名称。这里启动三个节点(rabbit_node_one、rabbit_node_two、rabbit_node_three)组合一个集群。

1、分别启动三个节点:

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

2、集群配置: 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配置haproxy负载均衡

1、配置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

2、安装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: #监听端口
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分布式集群架:https://blog.csdn.net/WoogeYu/article/details/51119101

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

RabbitMQ安装以及集群部署的更多相关文章

  1. Centos 6.5 Rabbitmq 安装和集群,镜像部署

    centos 6.5 rabbitmq 安装和集群,镜像部署 安装erlang: yum install gcc glibc-devel make ncurses-devel openssl-deve ...

  2. RabbitMQ安装、集群搭建、概念解析

    RabbitMQ安装.集群搭建.概念解析 基本概念 为什么会产生MQ 1.解耦:采用异步方式实现业务需求达到解耦的目的. 2.缓冲流量,削峰填谷: 问:为什么会有流量冲击? 答:采用"直接调 ...

  3. 初识zookeeper以及安装和集群部署

    初识zookeeper以及安装和集群部署     一.Zookeeper单体版安装     在安装zookeeper之前要先安装jdk环境,具体在linux环境安装jdk1.8请参照linux笔记. ...

  4. rabbitMQ 安装,集群搭建, 编码

    RabbitMQ 一.背景 命令行工具: http://www.rabbitmq.com/man/rabbitmqctl.1.man.html 介绍入门文章: http://blog.csdn.net ...

  5. RabbitMQ (简单集群部署操作)

    RabbitMQ 集群部署 前期准备 第一步:三台linux系统(centos7.3) 主机名(hostname) 网卡ip node1 192.168.137.138 node2 192.168.1 ...

  6. redis CentOS6.5安装及集群部署

    .下载redis source包 链接:https://pan.baidu.com/s/122ZCjNvjl9Jx6M2YsLrncw 密码:92ze 2.解压 tar -xzf redis-3.2. ...

  7. k8s1.9.0安装--完整集群部署

    三.完整集群部署 - kubernetes-with-ca 1. 理解认证授权 1.1 为什么要认证 想理解认证,我们得从认证解决什么问题.防止什么问题的发生入手.防止什么问题呢?是防止有人入侵你的集 ...

  8. kafka单机版的安装、集群部署 及使用

    1.安装kafka(单机版) 1.1上传 kafka_2.11-2.0.0.tgz 到 /root/Downloads 1.2解压 tar 包 tar -zxvf kafka_2.11-2.0.0.t ...

  9. k8s1.9.0安装--基础集群部署

    二.基础集群部署 - kubernetes-simple 1. 部署ETCD(主节点) 1.1 简介 kubernetes需要存储很多东西,像它本身的节点信息,组件信息,还有通过kubernetes运 ...

随机推荐

  1. asp.net 基于ajaxfileupload.js 实现文件异步上传

    前台代码: /*修改头像*/       //上传       function _sc() {           $(".ckfile").html("") ...

  2. CTSC2018&APIO2018游记

    CTSC2018&APIO2018游记 Day 0 傍晚出发,从长沙通往帝都的软卧哟. 然而长沙某中学坐高铁比我们晚出发还早到 Day 1 为了正经地写游记我决定忍住不在博客里吐槽酒店. 午饭 ...

  3. RPC笔记之初探RPC:DIY简单RPC框架

    一.什么是RPC RPC(Remote Procedure Call)即远程过程调用,简单的说就是在A机器上去调用B机器上的某个方法,在分布式系统中极其常用. rpc原理其实很简单,比较容易理解,在r ...

  4. 在pycharm和tensorflow环境下运行nmt

    目的是在pycharm中调试nmt代码,主要做了如下工作: 配置pycharm编译环境 在File->Settings->Project->Project Interpreter 设 ...

  5. docker之安装和管理mongodb

    前言 折腾一些使用docker来配置和管理mongodb和mongodb集群. 安装mongodb 从docker网站拉取mongodb镜像 docker search mongo # 选择一个版本 ...

  6. Linux 串口、usb转串口驱动分析(2-1) 【转】

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4186851 Linux 串口.usb转 ...

  7. poj1273

    赤裸裸的最大流 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstr ...

  8. Android 5.0 API

    Android 5.0 (LOLLIPOP) 为用户和应用开发者提供了新功能.本文旨在介绍其中最值得关注的新 API. 如果您有已发布的应用,请务必看一看 Android 5.0 行为变更,了解您的应 ...

  9. html可用于跨域的三个标签

    img 应用场景: <img>用于打点统计,统计网站可能是其他域 script 应用场景: 可以用于cdn,还可以用于JSONP link 可用于cdn

  10. java 轨迹栈

    printStackTrace()方法所提供的信息可以通过getStackTrace()方法直接访问. getStackTrace()方法返回一个由根轨迹中的元素所构成的数组,每一个元素都表示栈中的一 ...