当前位置:   article > 正文

RabbitMQ简单介绍_rabbitmq server是什么

rabbitmq server是什么
RabbitMQ

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

 官网:https://www.rabbitmq.com/

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:将生产的消息放在指定的节点上。
工作流程:

  • 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-node1ens33:192.168.43.176
rabbitmq-node2ens33:192.168.43.104
rabbitmq-node3ens33: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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
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',[]}]}]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
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',[]}]}]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
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',[]}]}]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
rabbitmq-node2:
[root@rabbitmq-node2 ~]# echo "ZXJQWSRSOTPJOWVOGUWG" > /var/lib/rabbitmq/.erlang.cookie
[root@rabbitmq-node2 ~]# reboot
  • 1
  • 2
rabbitmq-node3:
[root@rabbitmq-node3 ~]# echo "ZXJQWSRSOTPJOWVOGUWG" > /var/lib/rabbitmq/.erlang.cookie
[root@rabbitmq-node3 ~]# reboot
  • 1
  • 2
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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
rabbitmq-node1:
[root@rabbitmq-node1 ~]# firefox http://192.168.43.176:15672
  • 1


创建虚拟主机:
Admin–>Add a new virtual host–>Name:Vhost_1–>Add virtual host:


Vhost_1–>Set Permission:



查看是否创建成功:
Admin–>User:

创建Policies:
Admin–>Policies–>Add / update a policy–>Vitual host:VHost_1,Name:Policy_VHost_1–>Definition:ha-mode–>all–>Add policy:


创建Queue:
Queues–>Add a new queue–>Virtual host:VHost_1,Name:Polocy_VHost_1–>Add queue:


发送消息测试:
Queues–>Virtual host–>Publish message–>Delivery mode–>2-Persistent–>Headers–>Test–>Publish message:

这是一个线性图,可以看出消息消费的状态:

由于设置了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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/925982
推荐阅读
相关标签
  

闽ICP备14008679号