码迷,mamicode.com
首页 > 其他好文 > 详细

Rabbitmq 相关介绍之单机集群配置

时间:2017-09-14 23:49:35      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:rabbitmq   默认   集群模式   

一、说明:

说到集群,大家应该都不陌生,为了提高性能需要配置集群,而在有的时候,我们需要在测试环境先测试然后灰度上线,所以这里介绍在一台服务器上配置rabbitmq集群

二、rabbitmq集群模式

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

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

默认的集群模式,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个节点上进行存储,从而在性能与高可用性之间取得一个平衡(应该就是指镜像模式)。

2、镜像模式:把需要的队列做成镜像队列,存在于多个节点,属于RabbitMQ的HA方案

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

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

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

技术分享

以上概念参考地址:http://www.ywnds.com/?p=4741

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

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

1、环境
CentOS 6.7    IP 172.16.100.94   x86_64
2、软件包版本:
erlang-20.0.4-1.el6.x86_64.rpm
rabbitmq-server-3.6.12-1.el6.noarch.rpm
3、安装
#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
4、启动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  
5、结束进程命令
#rabbitmqctl -n rabbit1 stop
#rabbitmqctl -n rabbit2 stop
#rabbitmqctl -n rabbit3 stop
6、查看状态:
#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,[]}]}]
7、开始配置
停掉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,[]}]}]
注:已经加入群集的节点可以随时停止,并且宕机对集群来说也是没事的,在这两种情况下,集群的其
余部分继续运行不受影响,并且节点在重新启动时会自动“赶上”其他集群节点。

四、对集群进行测试:

我们假设关闭rabbit1和rabbit3,在每一个步骤检查集群状态
1、关闭rabbit1节点
#rabbitmqctl -n rabbit1 stop             
Stopping and halting node rabbit1@localhost
2、查看集群状态
# 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
3、查看集群状态
# 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,[]}]}]
4、现在开启关闭的节点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

5、再查看集群状态
#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状态了。
一些注意事项:
当整个群集被关闭时,最后一个节点必须是要联机的第一个节点。如果没有发生这种情况,节点将等待
30秒钟,最后一个disc节点重新联机,之后会失败。如果最后一个要脱机的节点无法恢复,则可以使用
forget_cluster_node命令将其从集群中删除。如果所有群集节点都以同时和不受控制的方式停止
(例如断电),你可以留下一种情况,所有的节点都认为一些其他节点在它们之后停止,在这种情况下
您可以在一个节点上使用force_boot命令,使其再次可引导。
6、为rabbit1节点添加用户名
#rabbitmqctl -n rabbit1 add_user admin admin123
#rabbitmqctl -n rabbit1 set_user_tags admin administrator
#rabbitmqctl -n rabbit1 set_permissions -p / admin  ".*" ".*" ".*"
7、开启web管理界面
#rabbitmq-plugins -n rabbit1 enable rabbitmq_management   #会开启15672端口
在浏览器访问  输入第6步设置的用户名和密码,界面如下:

技术分享

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

本文出自 “知识体系” 博客,请务必保留此出处http://linuxg.blog.51cto.com/4410110/1965369

Rabbitmq 相关介绍之单机集群配置

标签:rabbitmq   默认   集群模式   

原文地址:http://linuxg.blog.51cto.com/4410110/1965369

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!