RabbitMQ简单介绍

RabbitMQ:

 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue)的开源实现。

 官网:https://www.rabbitmq.com/
RabbitMQ简单介绍_第1张图片

AMQP:

 高级消息列队协议,应用于应用层,面向消息中间件设计的他可以接收客户端的请求的消息,传递给后端的主机。

消息列队的应用:
  • 流量消峰。
  • 架构的解耦。
消息列队的种类:
  • redis:入队和出队的策略(先进先出)。
  • memcacheQ:支持多条列队,并发性能好。
  • MSMQ:支持大数据,最大支持4MB,但是只有发送和接收的功能。
  • ZeroMQ:号称最快的消息列队,高吞吐,低延迟。
  • Kafka:支持快速的持久化,支持分布式。
  • AvtiveMQ:Java中间力量。
  • RabbitMQ:开源的,最早的,最稳定(重量级)的,扩展性能差,消息封装比较大。
组成:
  • RabbitMQ Server:消息列队服务,用于接收生成者产生的消息,并将消息推送给消费者。
  • Producer:生产者,用于生产消息,消息分为两个部分,数据和标签,标签用于匹配。
  • Consumer:消费者,用于消费消息列队中分配的信息,处理完了之后会给列队发送ack回应。
  • 消息ack机制:用来判断哪些消息被消费了,如果检测到的消息被消费,那么这个消息就会被删除,如果所有的consumer没有消费,这个消息就会回转,再次等待被消费。
  • Connection:生产者和消费者与消息列队建立的tcp连接。
  • Channel:虚拟通道,当建立tcp连接后回自动产生。
  • Exchange:交换机,会根据自身的匹配规则交给后端合适的Queue exchange的匹配规则。
  • Fanout:将生产者发送过来的消息交给所有的消息列队。
  • Direct:进行精准匹配。
  • Topic:进行模糊匹配。
  • Queue:消息的载体,列队。
  • Vhost:虚拟主机,一个Rabbit Server有单独的列队和交换机。
集群模式:
  • 普通模式:将生产者的消息放在单独的节点上。
  • 镜像模式:将生产者的消息存放在多个节点上。
    • all:将生产的消息放在所有节点上。
    • exactly:将生产的消息放在指定数量的节点上。
    • nodes:将生产的消息放在指定的节点上。
工作流程:
RabbitMQ简单介绍_第2张图片

  • Cleint(ClientA~B)生产消息,发送给服务端(RabbitMQ Server)的Exchange。
  • Exchange(Exchange1~2)收到消息,根据Routing key将消息转发到Queue(Queue1~2)。
  • Queue(Queue1~2)收到消息,将消息推送给消费者(Cleint1~3)。
  • 消费者(Cleint1~3)收到消息,并发送ACK给Queue(Queue1~2)确认收到消息。
  • Queue(Queue1~2)收到ACK,删除列队中缓存的此条消息。
注意:

 Binding key只用来把Exchange和Queue绑定在一起,并没有实际的路由关系,而Routing key则是将生产者发送的消息路由到exchange绑定到的Queue里面。

搭建RabbitMQ集群(基于Erlang):

Host 网络信息
rabbitmq-node1 ens33:192.168.43.176
rabbitmq-node2 ens33:192.168.43.104
rabbitmq-node3 ens33:192.168.43.23
rabbitmq-node1:
[root@localhost ~]# rpm -ivh erlang-18.1-1.el7.centos.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
    1:erlang-18.1-1.el7.centos         ################################# [100%]
[root@localhost ~]# yum -y install socat
[root@localhost ~]# rpm -ivh rabbitmq-server-3.6.15-1.el7.noarch.rpm 
warning: rabbitmq-server-3.6.15-1.el7.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
    1:rabbitmq-server-3.6.15-1.el7     ################################# [100%]
[root@localhost ~]# hostname rabbitmq-node1
[root@localhost ~]# vim /etc/hosts
192.168.43.176 rabbitmq-node1
192.168.43.104 rabbitmq-node2
192.168.43.23 rabbitmq-node3
[root@localhost ~]# bash
[root@rabbitmq-node1 ~]# reboot
rabbitmq-node2:
[root@localhost ~]# rpm -ivh erlang-18.1-1.el7.centos.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
    1:erlang-18.1-1.el7.centos         ################################# [100%]
[root@localhost ~]# yum -y install socat
[root@localhost ~]# rpm -ivh rabbitmq-server-3.6.15-1.el7.noarch.rpm 
warning: rabbitmq-server-3.6.15-1.el7.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
    1:rabbitmq-server-3.6.15-1.el7     ################################# [100%]
[root@localhost ~]# hostname rabbitmq-node2
[root@localhost ~]# vim /etc/hosts
192.168.43.176 rabbitmq-node1
192.168.43.104 rabbitmq-node2
192.168.43.23 rabbitmq-node3
[root@localhost ~]# bash
[root@rabbitmq-node1 ~]# reboot
rabbitmq-node3:
[root@localhost ~]# rpm -ivh erlang-18.1-1.el7.centos.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
    1:erlang-18.1-1.el7.centos         ################################# [100%]
[root@localhost ~]# yum -y install socat
[root@localhost ~]# rpm -ivh rabbitmq-server-3.6.15-1.el7.noarch.rpm 
warning: rabbitmq-server-3.6.15-1.el7.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
    1:rabbitmq-server-3.6.15-1.el7     ################################# [100%]
[root@localhost ~]# vim /etc/hosts
192.168.43.176 rabbitmq-node1
192.168.43.104 rabbitmq-node2
192.168.43.23 rabbitmq-node3
[root@localhost ~]# hostname rabbitmq-node3
[root@localhost ~]# bash
[root@rabbitmq-node1 ~]# reboot
rabbitmq-node1:
[root@rabbitmq-node1 ~]# systemctl start rabbitmq-server
[root@rabbitmq-node1 ~]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@rabbitmq-node1'
[{nodes,[{disc,['rabbit@rabbitmq-node1']}]},
{running_nodes,['rabbit@rabbitmq-node1']},
{cluster_name,<<"rabbit@rabbitmq-node1">>},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-node1',[]}]}]
rabbitmq-node2:
[root@rabbitmq-node2 ~]# systemctl start rabbitmq-server.service 
[root@rabbitmq-node2 ~]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@rabbitmq-node2'
[{nodes,[{disc,['rabbit@rabbitmq-node2']}]},
{running_nodes,['rabbit@rabbitmq-node2']},
{cluster_name,<<"rabbit@rabbitmq-node2">>},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-node2',[]}]}]
rabbitmq-node3:
[root@rabbitmq-node3 ~]# systemctl start rabbitmq-server.service 
[root@rabbitmq-node3 ~]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@rabbitmq-node3'
[{nodes,[{disc,['rabbit@rabbitmq-node3']}]},
{running_nodes,['rabbit@rabbitmq-node3']},
{cluster_name,<<"rabbit@rabbitmq-node3">>},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-node3',[]}]}]
rabbitmq-node1:
[root@rabbitmq-node1 ~]# cat /var/lib/rabbitmq/.erlang.cookie
ZXJQWSRSOTPJOWVOGUWG[root@rabbitmq-node1 ~]# 
[root@rabbitmq-node1 ~]# iptables -F
[root@rabbitmq-node1 ~]# systemctl stop firewalld
[root@rabbitmq-node1 ~]# setenforce 0
[root@rabbitmq-node1 ~]# iptables-save 
rabbitmq-node2:
[root@rabbitmq-node2 ~]# echo "ZXJQWSRSOTPJOWVOGUWG" > /var/lib/rabbitmq/.erlang.cookie
[root@rabbitmq-node2 ~]# reboot
rabbitmq-node3:
[root@rabbitmq-node3 ~]# echo "ZXJQWSRSOTPJOWVOGUWG" > /var/lib/rabbitmq/.erlang.cookie
[root@rabbitmq-node3 ~]# reboot
rabbitmq-node2:
[root@rabbitmq-node2 ~]# systemctl restart rabbitmq-server.service 
[root@rabbitmq-node2 ~]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@rabbitmq-node2'
[{nodes,[{disc,['rabbit@rabbitmq-node2']}]},
{running_nodes,['rabbit@rabbitmq-node2']},
{cluster_name,<<"rabbit@rabbitmq-node2">>},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-node2',[]}]}]
[root@rabbitmq-node2 ~]# iptables -F
[root@rabbitmq-node2 ~]# systemctl stop firewalld
[root@rabbitmq-node2 ~]# setenforce 0
[root@rabbitmq-node2 ~]# iptables-save 
rabbitmq-node3:
[root@rabbitmq-node3 ~]# systemctl restart rabbitmq-server.service 
[root@rabbitmq-node3 ~]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@rabbitmq-node3'
[{nodes,[{disc,['rabbit@rabbitmq-node3']}]},
{running_nodes,['rabbit@rabbitmq-node3']},
{cluster_name,<<"rabbit@rabbitmq-node3">>},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-node3',[]}]}]
[root@rabbitmq-node3 ~]# iptables -F
[root@rabbitmq-node3 ~]# systemctl stop firewalld
[root@rabbitmq-node3 ~]# setenforce 0
[root@rabbitmq-node3 ~]# iptables-save 
rabbitmq-node2:
[root@rabbitmq-node2 ~]# rabbitmqctl stop_app
Stopping rabbit application on node 'rabbit@rabbitmq-node2'
[root@rabbitmq-node2 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-node1
Clustering node 'rabbit@rabbitmq-node2' with 'rabbit@rabbitmq-node1'
[root@rabbitmq-node2 ~]# rabbitmqctl start_app
Starting node 'rabbit@rabbitmq-node2'
rabbitmq-node3:
[root@rabbitmq-node3 ~]# rabbitmqctl stop_app
Stopping rabbit application on node 'rabbit@rabbitmq-node3'
[root@rabbitmq-node3 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-node1
Clustering node 'rabbit@rabbitmq-node3' with 'rabbit@rabbitmq-node1'
[root@rabbitmq-node3 ~]# rabbitmqctl start_app
Starting node 'rabbit@rabbitmq-node3'
rabbitmq-node1:
[root@rabbitmq-node1 ~]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@rabbitmq-node1'
[{nodes,[{disc,['rabbit@rabbitmq-node1','rabbit@rabbitmq-node2',
                'rabbit@rabbitmq-node3']}]},
{running_nodes,['rabbit@rabbitmq-node3','rabbit@rabbitmq-node2',
                'rabbit@rabbitmq-node1']},
{cluster_name,<<"rabbit@rabbitmq-node1">>},
{partitions,[]},
{alarms,[{'rabbit@rabbitmq-node3',[]},
        {'rabbit@rabbitmq-node2',[]},
        {'rabbit@rabbitmq-node1',[]}]}]

[root@rabbitmq-node1 ~]# rabbitmq-plugins 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 rabbit@rabbitmq-node1... started 6 plugins.
[root@rabbitmq-node1 ~]# rabbitmqctl delete_user guest
Deleting user "guest"
[root@rabbitmq-node1 ~]# rabbitmqctl add_user rabbitmq 123.com
Creating user "rabbitmq"
[root@rabbitmq-node1 ~]# rabbitmqctl set_user_tags rabbitmq administrator
Setting tags for user "rabbitmq" to [administrator]
rabbitmq-node2:
[root@rabbitmq-node1 ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
    amqp_client
    cowlib
    cowboy
    rabbitmq_web_dispatch
    rabbitmq_management_agent
    rabbitmq_management
rabbitmq-node3:
[root@rabbitmq-node1 ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
    amqp_client
    cowlib
    cowboy
    rabbitmq_web_dispatch
    rabbitmq_management_agent
    rabbitmq_management
rabbitmq-node1:
[root@rabbitmq-node1 ~]# firefox http://192.168.43.176:15672
RabbitMQ简单介绍_第3张图片

RabbitMQ简单介绍_第4张图片

创建虚拟主机:
Admin–>Add a new virtual host–>Name:Vhost_1–>Add virtual host:
RabbitMQ简单介绍_第5张图片

RabbitMQ简单介绍_第6张图片

Vhost_1–>Set Permission:
RabbitMQ简单介绍_第7张图片

RabbitMQ简单介绍_第8张图片

RabbitMQ简单介绍_第9张图片

查看是否创建成功:
Admin–>User:
RabbitMQ简单介绍_第10张图片

创建Policies:
Admin–>Policies–>Add / update a policy–>Vitual host:VHost_1,Name:Policy_VHost_1–>Definition:ha-mode–>all–>Add policy:
RabbitMQ简单介绍_第11张图片

RabbitMQ简单介绍_第12张图片

创建Queue:
Queues–>Add a new queue–>Virtual host:VHost_1,Name:Polocy_VHost_1–>Add queue:
RabbitMQ简单介绍_第13张图片

RabbitMQ简单介绍_第14张图片

发送消息测试:
Queues–>Virtual host–>Publish message–>Delivery mode–>2-Persistent–>Headers–>Test–>Publish message:
RabbitMQ简单介绍_第15张图片

这是一个线性图,可以看出消息消费的状态:
RabbitMQ简单介绍_第16张图片

由于设置了all(镜像模式),所有在每台机器上都可以查看消费到的消息:
rabbitmq-node1:
[root@rabbitmq-node1 ~]# cd /var/lib/rabbitmq/mnesia/rabbit@rabbitmq-node1/queues/
[root@rabbitmq-node1 queues]# ls
558D7QT8LLHGY08V5IUGO6RSQ
[root@rabbitmq-node1 queues]# cd 558D7QT8LLHGY08V5IUGO6RSQ/
[root@rabbitmq-node1 558D7QT8LLHGY08V5IUGO6RSQ]# ls
0.idx  journal.jif
rabbitmq-node2:
[root@rabbitmq-node2 ~]# cd /var//lib/rabbitmq/mnesia/rabbit@rabbitmq-node2/queues/
[root@rabbitmq-node2 queues]# ls
558D7QT8LLHGY08V5IUGO6RSQ
[root@rabbitmq-node2 queues]# cd 558D7QT8LLHGY08V5IUGO6RSQ/
[root@rabbitmq-node2 558D7QT8LLHGY08V5IUGO6RSQ]# ls
0.idx  journal.jif
rabbitmq-node3:
[root@rabbitmq-node3 ~]# cd /var//lib/rabbitmq/mnesia/rabbit@rabbitmq-node3/queues/
[root@rabbitmq-node3 queues]# ls
558D7QT8LLHGY08V5IUGO6RSQ
[root@rabbitmq-node3 queues]# cd 558D7QT8LLHGY08V5IUGO6RSQ/
[root@rabbitmq-node3 558D7QT8LLHGY08V5IUGO6RSQ]# ls
0.idx  journal.jif

你可能感兴趣的:(CDN)