RabbitMQ安装以及集群部署

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

  • RabbitMQ单机版本安装
  • RabbitMQ单机集群安装方法(适合开发练习)
  • RabbitMQ配置haproxy负载均衡
  • RabbitMQ集群测试(待续)
  • 参考文档
  • 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 [email protected] ...
[{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 [email protected] ...
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 [email protected]-Pro ...
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_two reset
Resetting node [email protected]-Pro ...
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_two join_cluster rabbit_node_one
Clustering node [email protected]-Pro with rabbit_node_one
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_two start_app
Starting node [email protected]-Pro ...
 completed with 6 plugins.
Resetting node [email protected]-Pro ...
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl cluster_status -n rabbit_node_one
Cluster status of node [email protected]houguangfengdeMacBook-Pro ...
[{nodes,[{disc,[‘[email protected]‘,
                ‘[email protected]‘]}]},
 {running_nodes,[‘[email protected]‘,
                 ‘[email protected]‘]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]},
 {alarms,[{‘[email protected]‘,[]},
          {‘[email protected]‘,[]}]}]
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 [email protected]-Pro with rabbit_node_one
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_three start_app
Starting node [email protected]-Pro ...
 completed with 6 plugins.
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl cluster_status -n rabbit_node_one
Cluster status of node [email protected]-Pro ...
[{nodes,[{disc,[‘[email protected]‘,
                ‘[email protected]‘]},
         {ram,[‘[email protected]‘]}]},
 {running_nodes,[‘[email protected]‘,
                 ‘[email protected]‘,
                 ‘[email protected]‘]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]},
 {alarms,[{‘[email protected]‘,[]},
          {‘[email protected]‘,[]},
          {‘[email protected]‘,[]}]}]
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 [email protected]-Pro ...
[{nodes,[{disc,[‘[email protected]‘,
                ‘[email protected]‘]},
         {ram,[‘[email protected]‘]}]},
 {running_nodes,[‘[email protected]‘,
                 ‘[email protected]‘,
                 ‘[email protected]‘]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]},
 {alarms,[{‘[email protected]‘,[]},
          {‘[email protected]‘,[]},
          {‘[email protected]‘,[]}]}]
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 [email protected]-Pro ...
[{nodes,[{disc,[‘[email protected]‘,
                ‘[email protected]‘]},
         {ram,[‘[email protected]‘]}]},
 {running_nodes,[‘[email protected]‘,
                 ‘[email protected]‘]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]},
 {alarms,[{‘[email protected]‘,[]},
          {‘[email protected]‘,[]}]}]
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl -n rabbit_node_two start_app
Starting node [email protected]-Pro ...
zhouguangfengdeMacBook-Pro:sbin zhouguangfeng$ ./rabbitmqctl cluster_status -n rabbit_node_one
Cluster status of node [email protected]-Pro ...
[{nodes,[{disc,[‘[email protected]‘,
                ‘[email protected]‘]},
         {ram,[‘[email protected]‘]}]},
 {running_nodes,[‘[email protected]‘,
                 ‘[email protected]‘,‘[email protected]‘]},
 {cluster_name,<<"[email protected]">>},
 {partitions,[]},
 {alarms,[{‘[email protected]‘,[]},
          {‘[email protected]‘,[]}]}]
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

原文地址:https://www.cnblogs.com/xiaoxing/p/9258345.html

时间: 2024-09-30 19:05:34

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

rabbitMQ 安装,集群搭建, 编码

RabbitMQ 一.背景 命令行工具: http://www.rabbitmq.com/man/rabbitmqctl.1.man.html 介绍入门文章: http://blog.csdn.net/anzhsoft/article/details/19563091 内容比较清晰: http://www.diggerplus.org/archives/3110 Exchange.Queue producer把消息发送到Exchange(带上route key),consumer声明queue(

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

三.完整集群部署 - kubernetes-with-ca 1. 理解认证授权 1.1 为什么要认证 想理解认证,我们得从认证解决什么问题.防止什么问题的发生入手.防止什么问题呢?是防止有人入侵你的集群,root你的机器后让我们集群依然安全吗?不是吧,root都到手了,那就为所欲为,防不胜防了.其实网络安全本身就是为了解决在某些假设成立的条件下如何防范的问题.比如一个非常重要的假设就是两个节点或者ip之间的通讯网络是不可信任的,可能会被第三方窃取,也可能会被第三方篡改.就像我们上学时候给心仪的女

redis CentOS6.5安装及集群部署

1.下载redis source包 链接:https://pan.baidu.com/s/122ZCjNvjl9Jx6M2YsLrncw 密码:92ze 2.解压 tar -xzf redis-3.2.9.tar.gz 3.安装 $cd rredis-3.2.9 $make $make PREFIX=/usr/local/redis install 4.创建pid,日志,数据dump目录 $mkdir /var/redis/run $mkdir /var/redis/log $mkdir /va

rabbitmq安装、集群搭建

rabbitmq的安装: CentOS上面部署: 首先修改hosts文件 修改hosts文件vi /etc/hosts1.1.1.1 hostname 2.2.2.2 hostname 3.3.3.3 hostname erlang的安装环境:rpm -i http://mirror.bjtu.edu.cn/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpmyum install erlang安装rabbitMQ(RPM链接可以从http://www

RabbitMQ学习之集群部署

我们先搭建一个普通集群模式,在这个模式基础上再配置镜像模式实现高可用,Rabbit集群前增加一个反向代理,生产者.消费者通过反向代理访问RabbitMQ集群. 架构图如下: 设计架构可以如下:在一个集群里,有4台机器,其中1台使用磁盘模式,另2台使用内存模式.2台内存模式的节点,无疑速度更快,因此客户端(consumer.producer) 连接访问它们.而磁盘模式的节点,由于磁盘IO相对较慢,因此仅作数据备份使用,另外一台作为反向代理. 一.4台机器如下(通过/etc/sysconfig/ne

千万PV网站中RabbitMQ安装、集群

RabbitMQ是什么 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术.排队指的是应用程序通过 队列来通信.队列的使用除去了接收和发送应用程序同时执行的要求. RabbitMQ概念 Broker:简单来说就是消息队列服务器实体. Exchange

Centos7上安装Kubernetes集群部署docker

一.安装前准备 1.操作系统详情 需要三台主机,都最小化安装 centos7.3,并update到最新 cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core)  角色主机名IP Master      master192.168.1.14 node1    slave-1192.168.1.15 node2slave-2192.168.1.16 2.在每台主机上关闭firewalld改用iptables 输入以下命令,关闭fire

RabbitMQ(6) 集群部署

单节点部署 rabbitmq单节点部署比较简单,可以使用apt-get等工具快速安装部署. wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add - echo 'deb http://www.rabbitmq.com/debian/ {distriubtion} main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list sudo

Rabbitmq集群部署手册

一.Rabbitmq集群部署手册 1.环境介绍 系统环境:Red HatEnterprise Linux Server release 6.2 (Santiago) 内核版本:Linux zxt-02.com2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64GNU/Linux 软件版本:otp_src_17.3:rabbitmq-server-3.2.4:Python 2.6.6:simple