RabbitMQ安装以及集群部署

本次记录安装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/rabbitmqctl.8.html#Policy_Management

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 <[email protected]>

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分布式集群架: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

猜你喜欢

转载自www.cnblogs.com/xiaoxing/p/9258345.html