rabbitmq 基础讲解

来源:互联网 发布:淘宝睡衣好评语50字 编辑:程序博客网 时间:2024/06/06 04:32

一.rabbitmq介绍

rabbitmq模式:
1. 单一模式:非集群模式
2. 默认的集群模式

    对于Queue来说,消息实体只存在于其中一个节点,A、B两个节点仅有相同的元数据,即队列结构。

当消息进入A节点的Queue中后,consumer从B节点拉取时,RabbitMQ会临时在A、B间进行消息传输,把A中的消息实体取出并经过B发送给consumer。

所以consumer应尽量连接每一个节点,从中取消息。即对于同一个逻辑队列,要在多个节点建立物理Queue。否则无论consumer连A或B,出口总在A,会产生瓶颈。

该模式存在一个问题就是当A节点故障后,B节点无法取到A节点中还未消费的消息实体。

如果做了消息持久化,那么得等A节点恢复,然后才可被消费;如果没有持久化的话,然后就没有然后了


使用rabbitmq作为消息队列,经测试显示redis豌豆荚开源redis集群codis方案测试不成功,这里选择了rabbitmq集群,支持100t/s 

 3. 镜像模式:把需要的队列做成镜像队列,存在于多个节点上,属于RABBITMA集群

该模式解决了上述问题,其实质和普通模式不同之处在于,消息实体会主动在镜像节点间同步,而不是在consumer取数据时临时拉取。

该模式带来的副作用也很明显,除了降低系统性能外,如果镜像队列数量过多,加之大量的消息进入,集群内部的网络带宽将会被这种同步通讯大大消耗掉。

所以在对可靠性要求较高的场合中适用

设计架构:集群一般使用三个节点,一个磁盘节点,两个内存节点。这样设计速度更快。由于实验环境有限,仅仅做两台vm部署。

集群搭建:

[root@kvm ~]#yum -y install erlang

注:rabbitmq是用erlang语言开发的,所以erlang作为rabbitmq的依赖包

[root@kvm ~]# yum install -y rabbitmq-server –y

[root@kvm~]# systemctl start rabbitmq-server

[root@kvm~]# systemctl enable rabbitmq-server

启动web管理界面的插件,静态html页面

[root@kvm~]# rabbitmq-plugins enable rabbitmq_management

两台机器做同样的操作

 

二.配置rabbitmq集群

kvm

more /var/lib/rabbitmq/.erlang.cookie

把.erlang.cookie里面的文家拷贝num2,两个文家保持一样。

解释:

Rabbitmq的集群是依赖于erlang的集群来工作的,所以必须先构建起erlang的集群环境。Erlang的集群中各节点是通过一个magic cookie来实现的,这个cookie存放 /var/lib/rabbitmq/.erlang.cookie 中,文件是400的权限。所以必须保证各节点cookie保持一致,否则节点之间就无法通信。

[root@kvm ~]# ll -a /var/lib/rabbitmq/.erlang.cookie 
-r-------- 1 rabbitmq rabbitmq 20 12月 16 00:00 /var/lib/rabbitmq/.erlang.cookie

将其中一台节点上的.erlang.cookie值复制下来保存到其他节点上。或者使用scp的方法也可,但是要注意文件的权限和属主属组,复制好后别忘记还原.erlang.cookie的权限,否则可能会遇到错误

#chmod 400 /var/lib/rabbitmq/.erlang.cookie

接着:重启rabbitmq-server

[root@kvm ~]# systemctl restart rabbitmq-server

[root@num2~]# systemctl restart rabbitmq-server

 

然后使用命令:

rabbitmqctl status -n rabbit@kvm  #验证hosts解析ip是否正确,如果报错,请查看.erlang.cookie文件一致性on both servers

把num2加入集群:

[root@num2 ~]# rabbitmqctl stop_app

[root@num2 rabbitmq]# rabbitmqctl join_cluster --ram rabbit@kvm
Clustering node rabbit@num2 with rabbit@kvm ...
...done.

[root@num2 rabbitmq]# rabbitmqctl cluster_status
Cluster status of node rabbit@num2 ...
[{nodes,[{disc,[rabbit@kvm]},{ram,[rabbit@num2]}]},
 {running_nodes,[rabbit@kvm,rabbit@num2]},
 {partitions,[]}]
...done.

kvm节点上查看集群:

[root@kvm ~]# rabbitmqctl  cluster_status
Cluster status of node rabbit@kvm ...
[{nodes,[{disc,[rabbit@kvm]},{ram,[rabbit@num2]}]},
 {running_nodes,[rabbit@num2,rabbit@kvm]},
 {partitions,[]}]
...done.


集群搭建完毕!

这个时候发现一个节点是内存节点,一个节点是磁盘节点。

往任意一台集群节点里写入消息队列,会复制到另一个节点上,我们看到两个节点的消息队列数一致。

这种模式更适合非持久化队列,只有该队列是非持久的,客户端才能重新连接到集群里的其他节点,并重新创建队列。假如该队列是持久化的,那么唯一办法是将故障节点恢复起来。  
      为什么RabbitMQ不将队列复制到集群里每个节点呢?这与它的集群的设计本意相冲突,集群的设计目的就是增加更多节点时,能线性的增加性能(CPU、内存)和容量(内存、磁盘)。理由如下:

1. storage space: If every cluster node had a full copy of every queue, adding nodes wouldn’t give you more storage capacity. For example, if one node could store 1GB of messages, adding two more nodes would simply give you two more copies of the same 1GB of messages.

2. performance: Publishing messages would require replicating those messages to every cluster node. For durable messages that would require triggering disk activity on all nodes for every message. Your network and disk load would increase every time you added a node, keeping the performance of the cluster the same (or possibly worse).

当然RabbitMQ新版本集群也支持队列复制(有个选项可以配置)。比如在有五个节点的集群里,可以指定某个队列的内容在2个节点上进行存储,从而在性能与高可用性之间取得一个平衡。

      镜像模式配置

上面配置RabbitMQ默认集群模式,但并不保证队列的高可用性,尽管交换机、绑定这些可以复制到集群里的任何一个节点,但是队列内容不会复制,虽然该模式解决一部分节点压力,但队列节点宕机直接导致该队列无法使用,只能等待重启,所以要想在队列节点宕机或故障也能正常使用,就要复制队列内容到集群里的每个节点,需要创建镜像队列。
我们看看如何镜像模式来解决复制的问题,从而提高可用性 
增加负载均衡器
 

关于负载均衡器,商业的比如F5的BIG-IP,Radware的AppDirector,是硬件架构的产品,可以实现很高的处理能力。但这些产品昂贵的价格会让人止步,所以我们还有软件负载均衡方案。互联网公司常用的软件LB一般有LVS、HAProxy、Nginx等。LVS是一个内核层的产品,主要在第四层负责数据包转发,使用较复杂。HAProxy和Nginx是应用层的产品,但Nginx主要用于处理HTTP,所以这里选择HAProxy作为RabbitMQ前端的LB。

HAProxy的安装使用非常简单,在Centos下直接yum install haproxy,然后更改/etc/haproxy/haproxy.cfg 文件即可,文件内容大概如下:

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000


listen rabbitmq_cluster  0.0.0.0:5672
mode tcp
balance roundrobin
server rqslave 192.168.140.122:5672 check inter 2000 rise 2 fall 3
server rqmaster 192.168.140.111:5672 check inter 2000 rise 2 fall 3

一般使用内存节点做负载均衡,磁盘节点只做备份不提供给生产者、消费者使用,当然如果我们服务器资源充足情况也可以配置多个磁盘节点,这样磁盘节点除了故障也不会影响,除非同时出故障。

配置策略
 
使用Rabbit镜像功能,需要基于rabbitmq策略来实现,政策是用来控制和修改群集范围的某个vhost队列行为和Exchange行为
 
在cluster中任意节点启用策略,策略会自动同步到集群节点
 
# rabbitmqctl set_policy -p hrsystem ha-allqueue"^" '{"ha-mode":"all"}'
 
这行命令在vhost名称为hrsystem创建了一个策略,策略名称为ha-allqueue,策略模式为 all 即复制到所有节点,包含新增节点,
策略正则表达式为 “^” 表示所有匹配所有队列名称。
例如rabbitmqctl set_policy -p hrsystem ha-allqueue "^message" '{"ha-mode":"all"}'
注意:"^message" 这个规则要根据自己修改,这个是指同步"message"开头的队列名称,我们配置时使用的应用于所有队列,所以表达式为"^"
官方set_policy说明参见
set_policy [-p vhostpath] {name} {pattern} {definition} [priority]
参考策略设置

ha-mode:
 
ha-modeha-paramsResultall(absent)Queue is mirrored across all nodes in the cluster. When a new node is added to the cluster, the queue will be mirrored to that node.exactlycountQueue is mirrored to count nodes in the cluster. If there are less than count nodes in the cluster, the queue is mirrored to all nodes. If there are more than countnodes in the cluster, and a node containing a mirror goes down, then a new mirror will not be created on another node. (This is to prevent queues migrating across a cluster as it is brought down.)nodesnode namesQueue is mirrored to the nodes listed in node names. If any of those node names are not a part of the cluster, this does not constitute an error. If none of the nodes in the list are online at the time when the queue is declared then the queue will be
创建队列时需要指定ha 参数,如果不指定x-ha-prolicy 的话将无法复制。

客户端使用负载均衡器,队列将被复制到所有节点,这样故障或者重启就不会影响什么了。

RabbitMQ简介

AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计。消息中间件主要用于组件之间的解耦,消息的发送者无需知道消息使用者的存在,反之亦然。
AMQP的主要特征是面向消息、队列、路由(包括点对点和发布/订阅)、可靠性、安全。
RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等,支持AJAX。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。
下面将重点介绍RabbitMQ中的一些基础概念,了解了这些概念,是使用好RabbitMQ的基础。

ConnectionFactory、Connection、Channel

ConnectionFactory、Connection、Channel都是RabbitMQ对外提供的API中最基本的对象。Connection是RabbitMQ的socket链接,它封装了socket协议相关部分逻辑。ConnectionFactory为Connection的制造工厂。
Channel是我们与RabbitMQ打交道的最重要的一个接口,我们大部分的业务操作是在Channel这个接口中完成的,包括定义Queue、定义Exchange、绑定Queue与Exchange、发布消息等。


参考文献:http://www.diggerplus.org/archives/3110

0 0
原创粉丝点击