rabbitmq3.8集群部署
环境介绍
主机名 | IP |
---|---|
rabbitmq1 | 192.168.50.134 |
rabbitmq2 | 192.168.50.135 |
rabbitmq3 | 192.168.50.136 |
1、修改主机名
如果已经修改过主机名的话,就不再进行修改。但是切记rabbitmq搭建完成后期不要再进行修改,否则会出现问题。
hostnamectl set-hostname rabbitmq1
hostnamectl set-hostname rabbitmq2
hostnamectl set-hostname rabbitmq3
每个节点添加hosts
192.168.50.134 rabbitmq1
192.168.50.135 rabbitmq2
192.168.50.136 rabbitmq3
最好是重启一下
reboot
2、安装源
三台分别都进行操作
分别是erlang和rabbitmq的源。默认地源下载的版本比较低,这里安装的erlang版本是21.3.8.18
。rabbitmq的版本是3.8.9
erlang的源
[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq-erlang/rpm/erlang/21/el/7
gpgcheck=1
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1
rabbitmq-server的源
[bintray-rabbitmq-server]
name=bintray-rabbitmq-rpm
baseurl=https://dl.bintray.com/rabbitmq/rpm/rabbitmq-server/v3.8.x/el/7/
gpgcheck=0
repo_gpgcheck=0
enabled=1
接下来需要生成一下yum缓存
dnf clean all
dnf makecache
3、安装
三台分别都进行操作
3.1、先安装erlang和socat
dnf install socat erlang -y
3.2、安装rabbitmq-server
dnf install rabbitmq-server -y
分别启动rabbitmq-server服务
systemctl start rabbitmq-server
4、三台机器同步.erlang.cookie
文件
这里我们以rabbtmq1
节点作为集群管理节点,.erlang.cookie
文件在rabbitmq的安装目录/var/lib/rabbitmq
目录下,这是一个隐藏文件,需要加上-a
参数
[root@rabbitmq1 rabbitmq]# ls -al
total 8
drwxr-xr-x 3 rabbitmq rabbitmq 42 Oct 30 21:47 .
drwxr-xr-x. 27 root root 4096 Oct 30 21:20 ..
-r-------- 1 rabbitmq rabbitmq 20 Oct 30 00:00 .erlang.cookie
drwxr-x--- 4 rabbitmq rabbitmq 135 Oct 30 21:47 mnesia
这里我们把.erlang.cookie
文件同步到其他机器上面
[root@rabbitmq1 rabbitmq]# scp .erlang.cookie root@192.168.50.135:/var/lib/rabbitmq/
root@192.168.50.135's password:
.erlang.cookie 100% 20 27.0KB/s 00:00
[root@rabbitmq1 rabbitmq]# scp .erlang.cookie root@192.168.50.136:/var/lib/rabbitmq/
root@192.168.50.136's password:
.erlang.cookie 100% 20 36.7KB/s 00:00
同步完成之后我们再来查看几个节点的.erlang.cookie
文件,发现都是一致的,这是我们想要的结果。
[root@rabbitmq1 rabbitmq]# cat .erlang.cookie
SFWVLUCDUUVPIVRJDWTE[root@rabbitmq1 rabbitmq]#
这个文件是不带结尾换行符的,大家应该能看出来。
5、重启服务
三个节点分别重启rabbitmq-server服务
systemctl restart rabbitmq-server
6、三个节点都打开rabbitmq监控插件
如果不打开的话,那么集群之间无法查看对方的数据,查看数据就是依靠web插件来实现的
[root@rabbitmq1 ~]# rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@rabbitmq1:
rabbitmq_management
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@rabbitmq1...
The following plugins have been enabled:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
started 3 plugins.
其他另外两个节点都打开此功能(如有必要,需要重启服务,不过一般都不用重启rabbitmq)
7、在rabbitmq1上面添加用户
默认下,我们上面打开了监控插件功能,那么此时就能够看到15672端口了,我们可以使用IP:PORT的方式来进行访问,默认地账户密码都是guest,但是rabbitmq只允许本机通过localhost的方式进行通讯,因此我们再创建个其他的用户实现远程访问。
rabbitmq1添加用户实现web访问
[root@rabbitmq1 ebin]# rabbitmqctl add_user admin 111111
Adding user "admin" ...
[root@rabbitmq1 ebin]# rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/" ...
[root@rabbitmq1 ebin]# rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...
这个时候再次访问并登录即可。
8、将其他两个节点rabbitmq2、rabbitmq3加入集群中
这里需要强调的是,我们将rabbitmq1作为第一个集群节点,然后在rabbitm2和rabbitmq3节点分别操作申请加入集群的方式。
8.1、rabbitmq1节点查看当前集群。
虽然只有一个节点,但也是集群,也是可以查看本身的集群信息的。
[root@rabbitmq1 ebin]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq1 ...
Basics
Cluster name: rabbit@rabbitmq1
Disk Nodes
rabbit@rabbitmq1
Running Nodes
rabbit@rabbitmq1
Versions
rabbit@rabbitmq1: RabbitMQ 3.8.9 on Erlang 21.3.8.18
Maintenance status
Node: rabbit@rabbitmq1, status: not under maintenance
Alarms
(none)
Network Partitions
(none)
Listeners
Node: rabbit@rabbitmq1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@rabbitmq1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Feature flags
Flag: drop_unroutable_metric, state: disabled
Flag: empty_basic_get_metric, state: disabled
Flag: implicit_default_bindings, state: enabled
Flag: maintenance_mode_status, state: enabled
Flag: quorum_queue, state: enabled
Flag: virtual_host_metadata, state: enabled
我们需要注意的一行信息就是Cluster name: rabbit@rabbitmq1
。要记住这个名字,一会我们要使用。
8.2、现在我们操作rabbitmq2节点
需要强调一下,集群中至少有一个节点是磁盘节点用于数据持久化,然后剩下的节点都设置为内存节点以提高性能。
这里将 rabbitmq2 添加到集群当中,并成为内存节点,不加--ram 默认是磁盘节点
[root@rabbitmq2 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq2 ...
[root@rabbitmq2 ~]# rabbitmqctl reset
Resetting node rabbit@rabbitmq2 ...
[root@rabbitmq2 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq1
Clustering node rabbit@rabbitmq2 with rabbit@rabbitmq1
[root@rabbitmq2 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq2 ...
好了,现在rabbitmq2节点已经加入集群中了,怎么验证呢?我们来到rabbitmq1节点上面查看集群信息即可看到rabbitmq2节点。
[root@rabbitmq1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq1 ...
Basics
Cluster name: rabbit@rabbitmq1
Disk Nodes
rabbit@rabbitmq1
RAM Nodes
rabbit@rabbitmq2
Running Nodes
rabbit@rabbitmq1
rabbit@rabbitmq2
Versions
rabbit@rabbitmq1: RabbitMQ 3.8.9 on Erlang 21.3.8.18
rabbit@rabbitmq2: RabbitMQ 3.8.9 on Erlang 21.3.8.18
Maintenance status
Node: rabbit@rabbitmq1, status: not under maintenance
Node: rabbit@rabbitmq2, status: not under maintenance
Alarms
(none)
Network Partitions
(none)
Listeners
Node: rabbit@rabbitmq1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@rabbitmq1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq2, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq2, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq2, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
8.3、现在操作rabbitmq3节点加入集群
[root@rabbitmq3 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq3 ...
[root@rabbitmq3 ~]# rabbitmqctl reset
Resetting node rabbit@rabbitmq3 ...
[root@rabbitmq3 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq1
Clustering node rabbit@rabbitmq3 with rabbit@rabbitmq1
[root@rabbitmq3 ~]# rabbitmqctl start_app
Starting node rabbit@rabbitmq3 ...
现在也可以来到rabbitmq1节点查看到rabbitmq3的节点信息了。
当然也可以在web上面查看到三个节点的信息,如下所示:、
9、设置集群为镜像模式
镜像队列机制就是将队列在 N 个节点之间设置主从关系,消息会在 N 个节点之间进行自动同步,且如果其中一个节点不可用,并不会导致消息丢失或服务不可用的情况,提升 MQ 集群的整体高可用性。
这里在rabbitmq1节点操作(在任意节点操作都是可以的)
[root@rabbitmq1 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
10、验证当前集群
[root@rabbitmq1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq1 ...
Basics
Cluster name: rabbit@rabbitmq1
Disk Nodes
rabbit@rabbitmq1
RAM Nodes
rabbit@rabbitmq2
rabbit@rabbitmq3
Running Nodes
rabbit@rabbitmq1
rabbit@rabbitmq2
rabbit@rabbitmq3
Versions
rabbit@rabbitmq1: RabbitMQ 3.8.9 on Erlang 21.3.8.18
rabbit@rabbitmq2: RabbitMQ 3.8.9 on Erlang 21.3.8.18
rabbit@rabbitmq3: RabbitMQ 3.8.9 on Erlang 21.3.8.18
Maintenance status
Node: rabbit@rabbitmq1, status: not under maintenance
Node: rabbit@rabbitmq2, status: not under maintenance
Node: rabbit@rabbitmq3, status: not under maintenance
Alarms
(none)
Network Partitions
(none)
Listeners
Node: rabbit@rabbitmq1, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq1, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@rabbitmq1, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq2, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq2, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq2, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@rabbitmq3, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbitmq3, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbitmq3, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0