Rabbitmq 相关介绍之单机普通模式集群配置

1、说明:php

说到集群,你们应该都不陌生,为了提升性能须要配置集群,而在有的时候,咱们须要在测试环境先测试而后灰度上线,因此这里介绍在一台服务器上配置rabbitmq集群html

2、rabbitmq集群模式node

一、普通模式:rabbitmq默认的集群模式nginx

RabbitMQ集群中节点包括内存节点、磁盘节点。内存节点就是将全部数据放在内存,磁盘节点将数据放在磁盘上。若是在投递消息时,打开了消息的持久化,那么即便是内存节点,数据仍是安全的放在磁盘。那么内存节点的性能只能体如今资源管理上,好比增长或删除队列(queue),虚拟主机(vrtual hosts),交换机(exchange)等,发送和接受message速度同磁盘节点同样。一个集群至少要有一个磁盘节点。一个rabbitmq集群中能够共享user,vhost,exchange等,全部的数据和状态都是必须在全部节点上复制的,对于queue根据集群模式不一样,应该有不一样的表现。在集群模式下只要有任何一个节点可以工做,RabbitMQ集群对外就能提供服务。web

默认的集群模式,queue建立以后,若是没有其它policy,则queue就会按照普通模式集群。对于Queue来讲,消息实体只存在于其中一个节点,A、B两个节点仅有相同的元数据,即队列结构,但队列的元数据仅保存有一份,即建立该队列的rabbitmq节点(A节点),当A节点宕机,你能够去其B节点查看,./rabbitmqctl list_queues发现该队列已经丢失,但声明的exchange还存在。浏览器

当消息进入A节点的Queue中后,consumer从B节点拉取时,RabbitMQ会临时在A、B间进行消息传输,把A中的消息实体取出并通过B发送给consumer,因此consumer应平均链接每个节点,从中取消息。该模式存在一个问题就是当A节点故障后,B节点没法取到A节点中还未消费的消息实体。若是作了队列持久化或消息持久化,那么得等A节点恢复,而后才可被消费,而且在A节点恢复以前其它节点不能再建立A节点已经建立过的持久队列;若是没有持久化的话,消息就会失丢。这种模式更适合非持久化队列,只有该队列是非持久的,客户端才能从新链接到集群里的其余节点,并从新建立队列。假如该队列是持久化的,那么惟一办法是将故障节点恢复起来。安全

为何RabbitMQ不将队列复制到集群里每一个节点呢?这与它的集群的设计本意相冲突,集群的设计目的就是增长更多节点时,能线性的增长性能(CPU、内存)和容量(内存、磁盘)。固然RabbitMQ新版本集群也支持队列复制(有个选项能够配置)。好比在有五个节点的集群里,能够指定某个队列的内容在2个节点上进行存储,从而在性能与高可用性之间取得一个平衡(应该就是指镜像模式)。bash

二、镜像模式:把须要的队列作成镜像队列,存在于多个节点,属于RabbitMQ的HA方案服务器

该模式解决了上述问题,其实质和普通模式不一样之处在于,消息实体会主动在镜像节点间同步,而不是在consumer取数据时临时拉取。该模式带来的反作用也很明显,除了下降系统性能外,若是镜像队列数量过多,加之大量的消息进入,集群内部的网络带宽将会被这种同步通信大大消耗掉。因此在对可靠性要求较高的场合中适用,一个队列想作成镜像队列,须要先设置policy,而后客户端建立队列的时候,rabbitmq集群根据“队列名称”自动设置是普通集群模式或镜像队列。具体以下:cookie

队列经过策略来使能镜像。策略能在任什么时候刻改变,rabbitmq队列也近可能的将队列随着策略变化而变化;非镜像队列和镜像队列之间是有区别的,前者缺少额外的镜像基础设施,没有任何slave,所以会运行得更快。

为了使队列称为镜像队列,你将会建立一个策略来匹配队列,设置策略有两个键“ha-mode和 ha-params(可选)”。ha-params根据ha-mode设置不一样的值,下面表格说明这些key的选项,以下图:

wKiom1m6QULj22AsAADOEuCVFCQ937.jpg

3、普通集群模式安装配置

官方文档https://www.rabbitmq.com/clustering.html

一、环境
CentOS 6.7    IP 172.16.100.94   x86_64
二、软件包版本:
erlang-20.0.4-1.el6.x86_64.rpm
rabbitmq-server-3.6.12-1.el6.noarch.rpm
三、安装
#rpm -ivh  erlang-20.0.4-1.el6.x86_64.rpm
#yum install socat  -y   # rabbitmq-server依赖次软件包
#rpm -ivh rabbitmq-server-3.6.12-1.el6.noarch.rpm
四、启动3个进程(模拟3台不一样的node)
#RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit1 /etc/init.d/rabbitmq-server start 
#RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=rabbit2 /etc/init.d/rabbitmq-server start 
#RABBITMQ_NODE_PORT=5674 RABBITMQ_NODENAME=rabbit3 /etc/init.d/rabbitmq-server start 
查看启动状况
#netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name     
tcp        0      0 0.0.0.0:25672               0.0.0.0:*                   LISTEN      25527/beam.smp      
tcp        0      0 0.0.0.0:25673               0.0.0.0:*                   LISTEN      26425/beam.smp            
tcp        0      0 0.0.0.0:25674               0.0.0.0:*                   LISTEN      27310/beam.smp      
tcp        0      0 0.0.0.0:4369                0.0.0.0:*                   LISTEN      25191/epmd                 
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1778/sshd                          
tcp        0      0 :::5672                     :::*                        LISTEN      25527/beam.smp      
tcp        0      0 :::5673                     :::*                        LISTEN      26425/beam.smp      
tcp        0      0 :::5674                     :::*                        LISTEN      27310/beam.smp               
tcp        0      0 :::4369                     :::*                        LISTEN      25191/epmd          
tcp        0      0 :::22                       :::*                        LISTEN      1778/sshd  
五、结束进程命令
#rabbitmqctl -n rabbit1 stop
#rabbitmqctl -n rabbit2 stop
#rabbitmqctl -n rabbit3 stop
六、查看状态:
#rabbitmqctl -n rabbit1 cluster_status
Cluster status of node rabbit1@localhost
[{nodes,[{disc,[rabbit1@localhost]}]},
 {running_nodes,[rabbit1@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit1@localhost,[]}]}]
#rabbitmqctl -n rabbit2 cluster_status
Cluster status of node rabbit2@localhost
[{nodes,[{disc,[rabbit2@localhost]}]},
 {running_nodes,[rabbit2@localhost]},
 {cluster_name,<<"rabbit2@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit2@localhost,[]}]}]
#rabbitmqctl -n rabbit3 cluster_status
Cluster status of node rabbit3@localhost
[{nodes,[{disc,[rabbit3@localhost]}]},
 {running_nodes,[rabbit3@localhost]},
 {cluster_name,<<"rabbit3@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit3@localhost,[]}]}]
七、开始配置
停掉rabbit2节点的应用
#rabbitmqctl -n rabbit2 stop_app
Stopping rabbit application on node rabbit2@localhost
将rabbit2节点加入到rabbit1@localhost
#rabbitmqctl -n rabbit2 join_cluster rabbit1@localhost
Clustering node rabbit2@localhost with rabbit1@localhost
启动rabbit2应用节点
#rabbitmqctl -n rabbit2 start_app
Starting node rabbit2@localhost
查看集群状态
#rabbitmqctl -n rabbit1 cluster_status
Cluster status of node rabbit1@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost]}]},
 {running_nodes,[rabbit2@localhost,rabbit1@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit2@localhost,[]},{rabbit1@localhost,[]}]}]
 
#rabbitmqctl -n rabbit2 cluster_status
Cluster status of node rabbit2@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost]}]},
 {running_nodes,[rabbit1@localhost,rabbit2@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit1@localhost,[]},{rabbit2@localhost,[]}]}]
能够看到无论是以哪个节点的身份去查,集群中都有节点rabbit1和rabbit2
#############################################3
如今加入rabbit3
首先停掉rebbit3节点的应用
# rabbitmqctl -n rabbit3 stop_app
Stopping rabbit application on node rabbit3@localhost

# rabbitmqctl -n rabbit3 join_cluster rabbit2@localhost
Clustering node rabbit3@localhost with rabbit2@localhost
启动
# rabbitmqctl -n rabbit3 start_app
Starting node rabbit3@localhost
查看集群状态
#rabbitmqctl -n rabbit1 cluster_status
Cluster status of node rabbit1@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
 {running_nodes,[rabbit3@localhost,rabbit2@localhost,rabbit1@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit3@localhost,[]},
          {rabbit2@localhost,[]},
          {rabbit1@localhost,[]}]}]
注:已经加入群集的节点能够随时中止,而且宕机对集群来讲也是没事的,在这两种状况下,集群的其
余部分继续运行不受影响,而且节点在从新启动时会自动“遇上”其余集群节点。

4、对集群进行测试:

咱们假设关闭rabbit1和rabbit3,在每个步骤检查集群状态
一、关闭rabbit1节点
#rabbitmqctl -n rabbit1 stop             
Stopping and halting node rabbit1@localhost
二、查看集群状态
# rabbitmqctl -n rabbit2 cluster_status
Cluster status of node rabbit2@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
 {running_nodes,[rabbit3@localhost,rabbit2@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit3@localhost,[]},{rabbit2@localhost,[]}]}]    
能够看到rabbit1节点已经再也不running_noeds中了,可是依然在disc列表!
#rabbitmqctl -n rabbit3 cluster_status
Cluster status of node rabbit3@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
 {running_nodes,[rabbit2@localhost,rabbit3@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit2@localhost,[]},{rabbit3@localhost,[]}]}]
############################################################
#rabbitmqctl -n rabbit3 stop    #关闭rabbit3 节点
Stopping and halting node rabbit3@localhost
三、查看集群状态
#rabbitmqctl -n rabbit2 cluster_status
Cluster status of node rabbit2@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
 {running_nodes,[rabbit2@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit2@localhost,[]}]}]
四、如今开启关闭的节点rabbit1 和rabbit3 
#RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit1 /etc/init.d/rabbitmq-server start
#RABBITMQ_NODE_PORT=5674 RABBITMQ_NODENAME=rabbit3 /etc/init.d/rabbitmq-server start

五、再查看集群状态
#rabbitmqctl -n rabbit1 cluster_status
Cluster status of node rabbit1@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
 {running_nodes,[rabbit3@localhost,rabbit2@localhost,rabbit1@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit3@localhost,[]},
          {rabbit2@localhost,[]},
          {rabbit1@localhost,[]}]}]
能够看到3个节点都已是running状态了。

六、为rabbit1节点添加用户名
#rabbitmqctl -n rabbit1 add_user admin admin123
#rabbitmqctl -n rabbit1 set_user_tags admin administrator
#rabbitmqctl -n rabbit1 set_permissions -p / admin  ".*" ".*" ".*"
七、开启web管理界面
#rabbitmq-plugins -n rabbit1 enable rabbitmq_management   #会开启15672端口
在浏览器访问  输入第6步设置的用户名和密码,界面以下:

wKiom1m6TNjzFGlbAADdAuD1MoU197.jpg

注:rabbit2和rabbit3之因此是×××状态,红色小箭头也解释了。由于咱们是在一台服务器模拟的3个节点,rabbitmq_management界面启动以后,端口是15672,只启动了一个,再启动rabbit2的时候,提示端口被占用,目前不知道如何为管理界面指定不一样的端口,后面会继续研究……

5、退出集群和重启集群

一、退出集群
当节点再也不成为集群中的一部分时,须要从集群中明确删除节点,咱们以rabbit3节点为例子:
#rabbitmqctl -n rabbit3 stop_app
#rabbitmqctl -n rabbit3 reset
#rabbitmqctl -n rabbit3 start_app
查询状态
#rabbitmqctl -n rabbit1 cluster_status
Cluster status of node rabbit1@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost]}]},
 {running_nodes,[rabbit2@localhost,rabbit1@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit2@localhost,[]},{rabbit1@localhost,[]}]}]
####################################################
#rabbitmqctl -n rabbit2 cluster_status
Cluster status of node rabbit2@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost]}]},
 {running_nodes,[rabbit1@localhost,rabbit2@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit1@localhost,[]},{rabbit2@localhost,[]}]}]
#####################################################
#rabbitmqctl -n rabbit3 cluster_status
Cluster status of node rabbit3@localhost
[{nodes,[{disc,[rabbit3@localhost]}]},
 {running_nodes,[rabbit3@localhost]},
 {cluster_name,<<"rabbit3@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit3@localhost,[]}]}]
能够看到节点3已经被移除了集群!
#####################################################
咱们也能够远程删除节点,例如当必须处理一个无响应的节点的时候,能够从rabbit2@localhost中删除rabbit1@localhost         
#rabbitmqctl -n rabbit1 stop_app          #首先停掉rabbit1节点

#rabbitmqctl -n rabbit2 forget_cluster_node rabbit1@localhost #从集群中移除rabbit1
Removing node rabbit1@localhost from cluster

注意,rabbit1仍然认为它与rabbit2是属于一个集群,当尝试启动它的时候将致使错误,咱们将须要
重置它,以便可以再次启动它,以下:
#rabbitmqctl -n rabbit1 start_app
Starting node rabbit1@localhost
BOOT FAILED
===========
Error description:
   {error,{inconsistent_cluster,"Node rabbit1@localhost thinks it's clustered with node rabbit2@localhost, but rabbit2@localhost disagrees"}}
Log files (may contain more information):
   /var/log/rabbitmq/rabbit1.log
   /var/log/rabbitmq/rabbit1-sasl.log

Stack trace:
   [{rabbit_mnesia,check_cluster_consistency,0,
                   [{file,"src/rabbit_mnesia.erl"},{line,598}]},
    {rabbit,'-start/0-fun-0-',0,[{file,"src/rabbit.erl"},{line,273}]},
    {rabbit,start_it,1,[{file,"src/rabbit.erl"},{line,417}]},
    {rpc,'-handle_call_call/6-fun-0-',5,[{file,"rpc.erl"},{line,197}]}]

Error: {error,{inconsistent_cluster,"Node rabbit1@localhost thinks it's clustered with node rabbit2@localhost, but rabbit2@localhost disagrees"}}

#rabbitmqctl -n rabbit1 reset     #重置rabbit1节点
Resetting node rabbit1@localhost
#rabbitmqctl -n rabbit1 start_app #再开启就不会报错了
Starting node rabbit1@localhost
而后查看移除集群以后的状态
#rabbitmqctl -n rabbit1 cluster_status
Cluster status of node rabbit1@localhost
[{nodes,[{disc,[rabbit1@localhost]}]},
 {running_nodes,[rabbit1@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit1@localhost,[]}]}]
#rabbitmqctl -n rabbit2 cluster_status
Cluster status of node rabbit2@localhost
[{nodes,[{disc,[rabbit2@localhost]}]},
 {running_nodes,[rabbit2@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit2@localhost,[]}]}]
#rabbitmqctl -n rabbit3 cluster_status
Cluster status of node rabbit3@localhost
[{nodes,[{disc,[rabbit3@localhost]}]},
 {running_nodes,[rabbit3@localhost]},
 {cluster_name,<<"rabbit3@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit3@localhost,[]}]}]
注:cluster_status命令如今显示做为独立RabbitMQ brokers运行的全部三个节点.可是有一点须要注意
rabbit2节点保留了集群的残留状态,而rabbit1和rabbit3是新近初始化的brokers。若是咱们想要从新
初始化rabbit2节点,操做以下:
#rabbitmqctl -n rabbit2 stop_app 
#rabbitmqctl -n rabbit2 reset 
#rabbitmqctl -n rabbit2 start_app
以后再查看,rabbit2就是一个新初始化的节点了,以下:
#rabbitmqctl -n rabbit2 cluster_status
Cluster status of node rabbit2@localhost
[{nodes,[{disc,[rabbit2@localhost]}]},
 {running_nodes,[rabbit2@localhost]},
 {cluster_name,<<"rabbit2@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit2@localhost,[]}]}]
 
二、集群重启 
当集群重启时,最后一个挂掉的节点应该第一个重启,若是没有这样作,节点将等待30秒钟,以便最后
一个disc节点从新联机,并随后失败。
若是最后一个脱机的节点没法恢复,则可使用forget_cluster_node命令将其从集群中删除。若是集群
因特殊缘由(好比断电),而不知道哪一个节点最后一个挂掉。可用如下方法重启,使其再次可引导,先
在一个节点上执行,以下:
#rabbitmqctl force_boot
#service rabbitmq-server start
在其余节点上执行
#service rabbitmq-server start

查看cluster状态是否正常(要在全部节点上查询)。
#rabbitmqctl cluster_status
若是有节点没加入集群,能够先退出集群,而后再从新加入集群。

注意:上述方法不适合内存节点重启,内存节点重启的时候是会去磁盘节点同步数据,若是磁盘节点没
起来,内存节点一直失败。

6、解决单机集群管理界面出现×××的问题(第四步骤最后)

在官网上看到了如何在一台服务器配置三个节点,而且将其配置成集群,以下步骤:
一、启动三个节点
RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit1 /etc/init.d/rabbitmq-server start
RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}]" RABBITMQ_NODENAME=rabbit2 /etc/init.d/rabbitmq-server start
RABBITMQ_NODE_PORT=5674 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}]" RABBITMQ_NODENAME=rabbit3 /etc/init.d/rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.
二、查看端口
# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:25672               0.0.0.0:*                   LISTEN      17282/beam.smp      
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      125867/php-fpm      
tcp        0      0 0.0.0.0:25673               0.0.0.0:*                   LISTEN      17911/beam.smp      
tcp        0      0 0.0.0.0:25674               0.0.0.0:*                   LISTEN      20203/beam.smp      
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      125204/nginx        
tcp        0      0 0.0.0.0:4369                0.0.0.0:*                   LISTEN      17041/epmd          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      3172/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1324/master         
tcp        0      0 :::5672                     :::*                        LISTEN      17282/beam.smp      
tcp        0      0 :::5673                     :::*                        LISTEN      17911/beam.smp      
tcp        0      0 :::5674                     :::*                        LISTEN      20203/beam.smp      
tcp        0      0 :::4369                     :::*                        LISTEN      17041/epmd          
tcp        0      0 :::22                       :::*                        LISTEN      3172/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1324/master         
三、配置集群
#rabbitmqctl -n rabbit2 stop_app

#rabbitmqctl -n rabbit2 join_cluster rabbit1@localhost   #将rabbit2节点加入到rabbit1

#rabbitmqctl -n rabbit2 start_app

四、查看
#rabbitmqctl  -n rabbit1 cluster_status
Cluster status of node rabbit1@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost]}]},
 {running_nodes,[rabbit2@localhost,rabbit1@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit2@localhost,[]},{rabbit1@localhost,[]}]}]
 
五、继续添加集群节点
#rabbitmqctl -n rabbit3 stop_app

#rabbitmqctl -n rabbit3 join_cluster rabbit2@localhost   #将rabbit3节点加入rabbit2

#rabbitmqctl -n rabbit3 start_app

六、查看集群状态
#rabbitmqctl  -n rabbit1 cluster_status
Cluster status of node rabbit1@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
 {running_nodes,[rabbit3@localhost,rabbit2@localhost,rabbit1@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit3@localhost,[]},
          {rabbit2@localhost,[]},
          {rabbit1@localhost,[]}]}]
          
#rabbitmqctl  -n rabbit2 cluster_status
Cluster status of node rabbit2@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
 {running_nodes,[rabbit3@localhost,rabbit1@localhost,rabbit2@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit3@localhost,[]},
          {rabbit1@localhost,[]},
          {rabbit2@localhost,[]}]}]
          
#rabbitmqctl  -n rabbit3 cluster_status
Cluster status of node rabbit3@localhost
[{nodes,[{disc,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]}]},
 {running_nodes,[rabbit1@localhost,rabbit2@localhost,rabbit3@localhost]},
 {cluster_name,<<"rabbit1@localhost">>},
 {partitions,[]},
 {alarms,[{rabbit1@localhost,[]},
          {rabbit2@localhost,[]},
          {rabbit3@localhost,[]}]}]
七、启动三个节点的rabbitmq-management界面
#rabbitmq-plugins -n rabbit1 enable rabbitmq_management
The following plugins have been enabled:
  amqp_client
  cowlib
  cowboy
  rabbitmq_web_dispatch
  rabbitmq_management_agent
  rabbitmq_management
Applying plugin configuration to rabbit1@localhost... started 6 plugins.

#rabbitmq-plugins -n rabbit2 enable rabbitmq_management
Plugin configuration unchanged.
Applying plugin configuration to rabbit2@localhost... started 6 plugins.

#rabbitmq-plugins -n rabbit3 enable rabbitmq_management
Plugin configuration unchanged.
Applying plugin configuration to rabbit3@localhost... started 6 plugins.

而后能够看到1567二、1567三、15674端口都起来了。

八、在rabbit1节点上建立用户
rabbitmqctl -n rabbit1 add_user admin admin123
rabbitmqctl -n rabbit1 set_user_tags admin administrator
rabbitmqctl -n rabbit1 set_permissions -p / admin  ".*" ".*" ".*"
注意:节点1设置完以上这些以后,在集群内的机器都会同步此配置,可是/etc/rabbitmq/rabbitmq.config文件不会同步。
查看vhost(/)容许哪些用户访问
#rabbitmqctl -n rabbit1 list_permissions -p /    #节点rabbit2和rabbit3查询出的信息是同样的
Listing permissions in vhost "/"
guest   .*      .*      .*
admin   .*      .*      .*
配置容许远程访问的用户,rabbitmq的guest用户默认不容许远程主机访问
#cat rabbitmq.config
[
{rabbit, [{tcp_listeners, [5672]}, {loopback_users, ["admin"]}]}
].
查看集群建立了那些用户:
#rabbitmqctl -n rabbit1 list_users
Listing users
admin   [administrator]
guest   [administrator]
删除guest用户:
#rabbitmqctl -n rabbit1 delete_user guest
Deleting user "guest"

而后在浏览器访问,以下图:

wKioL1nAqsaQzpAOAAEqd7gNafQ983.jpg

注意:集群中的节点disc表示为磁盘模式,ram表示为内存模式。默认是disc磁盘模式,而一个集群中最少要有一个磁盘节点。

下面咱们将rabbit2和rabbit3节点改成ram模式,操做以下

#rabbitmqctl -n rabbit2 stop_app

修改rabbit2节点disc模式为ram模式
#rabbitmqctl -n rabbit2 change_cluster_node_type ram
Turning rabbit2@localhost into a ram node

启动rabbit2节点
#rabbitmqctl -n rabbit2 start_app
Starting node rabbit2@localhost

节点rabbit3操做和rabbit2同样!
########################################
注意:也能够在加入集群的时候设置节点模式
#rabbitmqctl -n rabbit1 stop_app
#rabbitmqctl -n rabbit2 join_cluster rabbit1@localhost --ram
#rabbitmqctl -n rabbit1 start_app

刷新管理页面,会看到模式改变了,rabbit1是disc模式,rabbit2和rabbit3是ram模式,以下图:

wKioL1nAr2rBNkQYAAEPkclRIrw036.jpg

注意事项:

一、cookie在全部节点上必须彻底同样,同步时必定要注意。

二、erlang是经过主机名来链接服务,必须保证各个主机名之间能够ping通。能够经过编辑/etc/hosts来手工添加主机名和IP对应关系。若是主机名ping不通,rabbitmq服务启动会失败。

三、若是queue是非持久化queue,则若是建立queue的那个节点失败,发送方和接收方能够建立一样的queue继续运做。但若是是持久化queue,则只能等建立queue的那个节点恢复后才能继续服务。

四、在集群元数据有变更的时候须要有disk node在线,可是在节点加入或退出的时候全部的disk node必须所有在线。若是没有正确退出disk node,集群会认为这个节点当掉了,在这个节点恢复以前不要加入其它节点。

参考地址:http://www.ywnds.com/?p=4741

7、rabbitmq遇到的问题

一、rabbitmq管理界面没法启动

#rabbitmq-plugins disable rabbitmq_management  
Error: The following plugins could not be found:
  rabbitmq_management
执行#rabbitmq-plugins set rabbitmq_management  而后再执行disable或者enable就ok了

二、rabbitmq集群配置好以后,主机的hostname不能轻易修改,若是修改了,就会报错提示节点找不到,由于rabbitmq集群是以hostname为节点建立的。


不足之处,请多多指出。