RabbitMQ集群
RabbitMQ是什么?
MQ(Message Queue,消息队列)消息中间件,一般以集群方式部署,主要提供消息的接受和发送,实现各微服务之间的消息同步。
原理介绍
rabbitmq是依据erlang的分布式特性(RabbitMQ底层是通过Erlang架构来实现的,所以rabbitmqctl会启动Erlang节点,并基于Erlang节点来使用Erlang系统连接RabbitMQ节点,在连接过程中需要正确的Erlang Cookie和节点名称,Erlang节点通过交换Erlang Cookie以获得认证)来实现的,所以部署rabbitmq分布式集群时要先安装erlang,并把其中一个服务的cookie复制到另外的节点
rabbitmq集群中,各个rabbitmq为对等节点,即每个节点均提供给客户端连接,进行消息的接收和发送。节点分为内存节点和磁盘节点,一般的,均应建立为磁盘节点,为了防止机器重启后的消息消失;
RabbitMQ的Cluster集群模式一般分为两种,普通模式和镜像模式。消息队列通过rabbitmq HA镜像队列进行消息队列实体复制
1.普通模式下,以两个节点(rabbit01、rabbit02)为例来进行说明。对于Queue来说,消息实体只存在于其中一个节点rabbit01(或者rabbit02),rabbit01和rabbit02两个节点仅有相同的元数据,即队列的结构。当消息进入rabbit01节点的Queue后,consumer从rabbit02节点消费时,RabbitMQ会临时在rabbit01、rabbit02间进行消息传输,把A中的消息实体取出并经过B发送给consumer。所以consumer应尽量连接每一个节点,从中取消息。即对于同一个逻辑队列,要在多个节点建立物理Queue。否则无论consumer连rabbit01或rabbit02,出口总在rabbit01,会产生瓶颈。
2.镜像模式下,将需要消费的队列变为镜像队列,存在于多个节点,这样就可以实现RabbitMQ的HA高可用性。作用就是消息实体会主动在镜像节点之间实现同步,而不是像普通模式那样,在consumer消费数据时临时读取。缺点就是,集群内部的同步通讯会占用大量的网络带宽。
RabbitMQ的结构图
环境部署
IP地址 | 主机名 | 操作系统 | 操作系统 |
192.168.0.200 | server-1 | CentOS Linux release 7.7.1908 (Core) | 磁盘节点 |
192.168.0.202 | server-2 | CentOS Linux release 7.7.1908 (Core) | 内存节点 |
192.168.0.203 | server-3 | CentOS Linux release 7.7.1908 (Core) | 内存节点 |
开始部署
1.关闭防火墙和selinux(3台服务器都要关)
systemctl stop firewalld.service setenforce 0
[root@server-1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.0.200 server-1 192.168.0.202 server-2 192.168.0.203 server-3
2.三个节点配置epel源,安装rabbitmq软件包
[root@server-1 ~]# yum install epel-release -y && yum install rabbitmq-server -y
3.建立软连接
ln -s /usr/lib/rabbitmq/bin/* /usr/bin
4.查看插件的信息
[root@server-1 ~]# rabbitmq-plugins list [e] amqp_client 3.3.5 [ ] cowboy 0.5.0-rmq3.3.5-git4b93c2d [ ] eldap 3.3.5-gite309de4 [e] mochiweb 2.7.0-rmq3.3.5-git680dba8 [ ] rabbitmq_amqp1_0 3.3.5 [ ] rabbitmq_auth_backend_ldap 3.3.5 [ ] rabbitmq_auth_mechanism_ssl 3.3.5 [ ] rabbitmq_consistent_hash_exchange 3.3.5 [ ] rabbitmq_federation 3.3.5 [ ] rabbitmq_federation_management 3.3.5 [E] rabbitmq_management 3.3.5 [e] rabbitmq_management_agent 3.3.5 [ ] rabbitmq_management_visualiser 3.3.5 [ ] rabbitmq_mqtt 3.3.5 [ ] rabbitmq_shovel 3.3.5 [ ] rabbitmq_shovel_management 3.3.5 [ ] rabbitmq_stomp 3.3.5 [ ] rabbitmq_test 3.3.5 [ ] rabbitmq_tracing 3.3.5 [e] rabbitmq_web_dispatch 3.3.5 [ ] rabbitmq_web_stomp 3.3.5 [ ] rabbitmq_web_stomp_examples 3.3.5 [ ] sockjs 0.3.4-rmq3.3.5-git3132eb9 [e] webmachine 1.10.3-rmq3.3.5-gite9359c7 [root@server-1 ~]#
5.启用rabbitmq_management服务
[root@server-1 ~]# rabbitmq-plugins enable rabbitmq_management
6.启动rabbitmq服务
[root@server-1 ~]# systemctl start rabbitmq-server.service
7.使用以下命令检查三台的集群状态,目前相互独立,没有形成集群
[root@server-1 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-1' ... [{nodes,[{disc,['rabbit@server-1']}]}, {running_nodes,['rabbit@server-1']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done.
8.使用以下命令查看端口开放说明正常。(其中15672和55672都是rabbitmq的管理端口,5672则是和生产者、消费者通信的端口。)
[root@server-1 ~]# netstat -ntap | grep 5672 tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 1766/beam tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 1766/beam tcp 0 0 192.168.0.200:25672 192.168.0.203:45155 ESTABLISHED 1766/beam tcp 0 0 192.168.0.200:25672 192.168.0.202:45426 ESTABLISHED 1766/beam tcp 0 0 192.168.0.200:15672 192.168.0.105:55843 ESTABLISHED 1766/beam tcp6 0 0 :::5672 :::* LISTEN 1766/beam [root@server-1 ~]#
9.停止三台服务器
systemctl stop rabbitmq-server.service
10.把server-1的cookie值复制到server-2和server-3服务器
[root@server-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@192.168.0.202:/var/lib/rabbitmq/.erlang.cookie root@192.168.0.202's password: .erlang.cookie 100% 20 30.2KB/s 00:00 [root@server-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@192.168.0.203:/var/lib/rabbitmq/.erlang.cookie root@192.168.0.203's password: .erlang.cookie
11.开启3台rabbitmq服务
systemctl start rabbitmq-server.service
12.以下操作只在server-2服务器和server-3服务器上操作
[root@server-2 ~]# rabbitmqctl stop_app Stopping node 'rabbit@server-2' ... ...done. [root@server-3 ~]# rabbitmqctl stop_app Stopping node 'rabbit@server-3' ... ...done.
13.分别在mq02服务器、mq03服务器上把mq02、mq03作为内存节点与mq01磁盘节点连接起来。
[root@server-2 ~]# rabbitmqctl join_cluster --ram rabbit@server-1 Clustering node 'rabbit@server-2' with 'rabbit@server-1' ... ...done. [root@server-3 ~]# rabbitmqctl join_cluster --ram rabbit@server-1 Clustering node 'rabbit@server-3' with 'rabbit@server-1' ... ...done.
14.启动rabbitmq应用
[root@server-2 ~]# rabbitmqctl start_app Starting node 'rabbit@server-2' ... ...done. [root@server-3 ~]# rabbitmqctl start_app Starting node 'rabbit@server-3' ... ...done.
分别查看3台rabbitmq服务器的状态
[root@server-1 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-1' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-3','rabbit@server-2']}]}, {running_nodes,['rabbit@server-3','rabbit@server-2','rabbit@server-1']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done. [root@server-2 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-2' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-3','rabbit@server-2']}]}, {running_nodes,['rabbit@server-3','rabbit@server-1','rabbit@server-2']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done. [root@server-3 ~]# rabbitmqctl cluster_status Cluster status of node 'rabbit@server-3' ... [{nodes,[{disc,['rabbit@server-1']}, {ram,['rabbit@server-3','rabbit@server-2']}]}, {running_nodes,['rabbit@server-2','rabbit@server-1','rabbit@server-3']}, {cluster_name,<<"rabbit@server-1">>}, {partitions,[]}] ...done.
通过浏览器输入http://192.168.0.200:15672访问RabbitMQ(默认用户名:guest 密码:guest)