RabbitMQ集群的学习
Posted 人间忽晚,山河以秋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ集群的学习相关的知识,希望对你有一定的参考价值。
搭建RabbitMQ集群
节点规划:
可以参照下图来规划自己的节点
一、修改主机名,添加映射,关闭防火墙
每一个节点根据自己的主机名规划配置
我的节点与主机名
10.30.59.188 | rabbitmq1 |
---|---|
10.30.59.219 | rabbitmq2 |
– | – |
10.30.59.248 | rabbitmq3 |
rabbitmq1节点:
[root@localhost ~]# hostnamectl set-hostname rabbitmq1
[root@localhost ~]# bash
[root@rabbitmq1 ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.30.59.188 rabbitmq1
10.30.59.219 rabbitmq2
10.30.59.248 rabbitmq3
~
~
~
"/etc/hosts" 5L, 227C written
[root@rabbitmq1 ~]#
rabbitmq2节点:
[root@master1 opt]# hostnamectl set-hostname rabbitmq2
[root@master1 opt]# bash
[root@rabbitmq2 opt]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.30.59.188 rabbitmq1
10.30.59.219 rabbitmq2
10.30.59.248 rabbitmq3
~
~
~
"/etc/hosts" 5L, 227C written
rabbitmq3节点:
[root@localhost ~]# hostnamectl set-hostname rabbitmq3
[root@localhost ~]# bash
[root@rabbitmq3 ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.30.59.188 rabbitmq1
10.30.59.219 rabbitmq2
10.30.59.248 rabbitmq3
~
~
~
"/etc/hosts" 5L, 227C written
三个节点都执行的:
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
二、配置yum源
三个节点均使用提供的rabbitmq-repo.tar.gz的压缩包,上传至虚拟机的/root目录下,解压并放在/opt目录下,进入/etc/yum.repos.d目录下,将原来的repo文件移除,新建local.repo文件并编辑内容。可以使用ftp
rabbitmq1节点:
1、先配好yum源
[root@localhost ~]# ls
anaconda-ks.cfg rabbitmq-repo.tar.gz
[root@localhost ~]# tar -zxvf rabbitmq-repo.tar.gz -C /opt/
#此处省略
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
centos rabbitmq-repo
[root@localhost opt]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[rabbit]
name=rabbit
baseurl=file:///opt/rabbitmq-repo
gpgcheck=0
enabled=1
~
~
"/etc/yum.repos.d/local.repo" 17L, 224C written
[root@localhost opt]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: centos mariadb rabbit
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
[root@localhost opt]# yum repolist
Loaded plugins: fastestmirror
Determining fastest mirrors
centos | 3.6 kB 00:00:00
mariadb | 2.9 kB 00:00:00
rabbit | 2.9 kB 00:00:00
(1/4): centos/group_gz | 155 kB 00:00:00
(2/4): centos/primary_db | 2.8 MB 00:00:00
(3/4): rabbit/primary_db | 35 kB 00:00:00
(4/4): mariadb/primary_db | 66 kB 00:00:00
repo id repo name status
centos centos 3,723
mariadb mariadb 91
rabbit rabbit 26
repolist: 3,840
[root@localhost opt]#
[root@localhost opt]#
[root@localhost opt]# cd
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id repo name status
centos centos 3,723
rabbit rabbit 26
repolist: 3,749
[root@localhost ~]#
然后ftp安装将yum源共享给另两个节点
[root@localhost ~]# yum install vsftpd -y
[root@localhost ~]# echo anon_root=/opt >> /etc/vsftpd/vsftpd.conf
[root@localhost ~]# systemctl start vsftpd
[root@localhost ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
rabbitmq2节点和rabbitm3节点:
[root@rabbitmq2 ~]# mv /etc/yum.repos.d/C* /media/
[root@rabbitmq2 ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=ftp://10.30.59.188/centos
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=ftp://10.30.59.188/mariadb-repo
gpgcheck=0
enabled=1
[rabbit]
name=rabbit
baseurl=ftp://10.30.59.188/rabbitmq-repo
gpgcheck=0
enabled=1
[root@rabbitmq2 ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: centos mariadb rabbit
Cleaning up list of fastest mirrors
Other repos take up 102 M of disk space (use --verbose for details)
[root@rabbitmq2 ~]# yum repolist
Loaded plugins: fastestmirror
Determining fastest mirrors
centos | 3.6 kB 00:00:00
mariadb | 2.9 kB 00:00:00
rabbit | 2.9 kB 00:00:00
(1/4): centos/group_gz | 155 kB 00:00:00
(2/4): rabbit/primary_db | 35 kB 00:00:00
(3/4): centos/primary_db | 2.8 MB 00:00:00
(4/4): mariadb/primary_db | 66 kB 00:00:00
repo id repo name status
centos centos 3,723
rabbit rabbit 26
repolist: 3,749
三、安装RabbitMQ服务并启动
三个节点都要安装:
# yum install -y rabbitmq-server
# systemctl start rabbitmq-server
#systemctl status rabbitmq-server
● rabbitmq-server.service - RabbitMQ broker
Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2020-10-20 05:48:17 UTC; 1h 7min ago
Process: 13641 ExecStop=/usr/lib/rabbitmq/bin/rabbitmqctl stop (code=exited, status=0/SUCCESS)
Main PID: 13685 (beam)
CGroup: /system.slice/rabbitmq-server.service
├─13685 /usr/lib64/erlang/erts-5.10.4/bin/beam -W w -K true -A30 -P 1048576 -- -root /usr/lib64/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /usr/lib/rabbitmq/lib/ra...
├─13757 inet_gethost 4
└─13758 inet_gethost 4
Oct 20 05:48:14 rabbitmq1 systemd[1]: Starting RabbitMQ broker...
四、配置界面访问
RabbitMQ提供了一个非常友好的图形化监控页面插件(rabbitmq_management),让我们可以一目了然看见Rabbit的状态或集群状态。启用图形化页面插件的具体命令如下:
[root@rabbitmq1 ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.
启用图形化界面后,需要重启RabbitMQ服务,命令如下:
[root@rabbitmq1 ~]# service rabbitmq-server restart
Redirecting to /bin/systemctl restart rabbitmq-server.service
开启了图形化监控页面后,能通过网页访问,访问端口为15672,输入rabbitmq1节点的ip加端口15672(http://172.30.11.12:15672)访问RabbitMQ监控界面,使用用户名:密码guest:guset登录。
例如:
这是加入了其他主机后的,也就是配置集群后的
五、配置RabbitMQ集群服务
配置节点间的通信的原理:
Rabbitmq的集群是依附于erlang集群来工作的,所以必须先构建起一个erlang集群。erlang集群中各节点是由magic cookie来实现的,每个节点上要保持相同的.erlang.cookie文件,这个cookie存放在/var/lib/rabbitmq/.erlang.cookie中,文件是400的权限。必须保证各节点cookie一致,不然节点之间就无法通信。
1、 在这之前要配置好rabbitmq2节点和rabbitmq3节点的RAM界面访问,即启用监控管理器
在rabbitmq2和rabbitmq3节点上启用rabbitmq_management,命令如下:
rabbitmq2节点:
[root@rabbitmq2 rabbitmq]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.
[root@rabbitmq2 rabbitmq]# service rabbitmq-server restart
Redirecting to /bin/systemctl restart rabbitmq-server.service
rabbitmq3节点:
[root@rabbitmq3 rabbitmq]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.
[root@rabbitmq3 rabbitmq]# service rabbitmq-server restart
Redirecting to /bin/systemctl restart rabbitmq-server.service
2、配置cookie一致
[root@rabbitmq1 ~]# cat /var/lib/rabbitmq/.erlang.cookie
EZYGPUJOTSESXPAUFMWO
[root@rabbitmq1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@rabbitmq2:/var/lib/rabbitmq/
[root@rabbitmq1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@rabbitmq3:/var/lib/rabbitmq/
将.erlang.cookie文件传至rabbitmq2和rabbitmq3节点后,需要修改该文件的用户与用户组,命令如下:
[root@rabbitmq2 rabbitmq]# chown rabbitmq:rabbitmq .erlang.cookie
[root@rabbitmq3 rabbitmq]# chown rabbitmq:rabbitmq .erlang.cookie
3、配置节点加入集群
在rabbitmq2、rabbitmq3节点执行如下命令,将这两个节点作为RAM节点加入到RabbitMQ集群中,具体命令如下:
rabbitmq2节点:
[root@rabbitmq2 rabbitmq]# rabbitmqctl stop_app
Stopping node rabbit@rabbitmq2 ...
...done.
[root@rabbitmq2 rabbitmq]# rabbitmqctl join_cluster --ram rabbit@rabbitmq1
Clustering node rabbit@rabbitmq2 with rabbit@rabbitmq1 ...
...done.
[root@rabbitmq2 rabbitmq]# rabbitmqctl start_app
Starting node rabbit@rabbitmq2 ...
...done.
rabbitmq3节点:
[root@rabbitmq3 rabbitmq]# rabbitmqctl stop_app
Stopping node rabbit@rabbitmq3 ...
...done.
[root@rabbitmq3 rabbitmq]# rabbitmqctl join_cluster --ram rabbit@rabbitmq1
Clustering node rabbit@rabbitmq3 with rabbit@rabbitmq1 ...
...done.
[root@rabbitmq3 rabbitmq]# rabbitmqctl start_app
Starting node rabbit@rabbitmq3 ...
...done.
默认rabbitmq启动后是磁盘节点,在这个cluster命令下,rabbitmq2和rabbitmq3是内存节点,rabbitmq1是磁盘节点。
如果要使rabbitmq2、rabbitmq3都是磁盘节点,去掉–ram参数即可。
如果想要更改节点类型,可以使用命令rabbitmqctl change_cluster_node_type disc(ram),前提是必须停掉rabbit应用。
启用rabbitmq2节点和rabbitmq3节点的监控界面后,登录http://172.30.11.12:15672,查看监控界面,如下图所示:
可以看到rabbitmq1节点为数据节点,rabbitmq2和rabbitmq3节点为RAM内存节点。
六、rabbitMQ集群的常用命令
1、查看插件打开情况
[root@rabbitmq1 ~]# 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
2、启用监控管理器的命令
[root@rabbitmq2 ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.
3、关闭监控管理器命令:
# rabbitmq-plugins disable rabbitmq_management
4、查看所有的队列
[root@rabbitmq2 ~]# rabbitmqctl list_queues
Listing queues ...
...done.
[root@rabbitmq2 ~]#
5、清除所有的队列
# rabbitmqctl reset
6、查看状态
查看集群状态,在RabbitMQ集群的任一节点上,可以查看RabbitMQ集群的状态,命令如下
[root@rabbitmq2 ~]# rabbitmqctl list_queues
Listing queues ...
...done.
[root@rabbitmq2 ~]# rabbitmqctl status
Status of node rabbit@rabbitmq2 ...
[{pid,19134},
{running_applications,
[{rabbitmq_management,"RabbitMQ Management Console","3.3.5"},
{rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.3.5"},
{webmachine,"webmachine","1.10.3-rmq3.3.5-gite9359c7"},
{mochiweb,"MochiMedia Web Server","2.7.0-rmq3.3.5-git680dba8"},
{rabbitmq_management_agent,"RabbitMQ Management Agent","3.3.5"},
{rabbit,"RabbitMQ","3.3.5"},
{os_mon,"CPO CXC 138 46","2.2.14"},
{mnesia,"MNESIA CXC 138 12","4.11"},
{amqp_client,"RabbitMQ AMQP Client","3.3.5"},
{inets,"INETS CXC 138 49","5.9.8"},
{xmerl,"XML parser","1.3.6"},
{sasl,"SASL CXC 138 11","2.3.4"},
{stdlib,"ERTS CXC 138 10","1.19.4"},
{kernel,"ERTS CXC 138 10","2.16.4"}]},
{os,{unix,linux}},
{erlang_version,
"Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:2:2] [async-threads:30] [hipe] [kernel-poll:true]\\n"},
{memory,
[{total,39613672},
{connection_procs,5600},
{queue_procs,5600},
{plugins,127960},
{other_proc,13460392},
{mnesia,63664},
{mgmt_db,12224},
{msg_index,34528},
{other_ets,1078952},
{binary,20648},
{code,19754683},
{atom,703377},
{other_system,4346044}]},
{alarms,[]},
{listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]},
{vm_memory_high_watermark,0.4},
{vm_memory_limit,1589456076},
{disk_free_limit,50000000},
{disk_free,19906449408},
{file_descriptors,
[{total_limit,924},{total_used,3},{sockets_limit,829},{sockets_used,1}]},
{processes,[{limit,1048576},{used,178}]},
{run_queue,0},
{uptime,9227}]
...done.
[root@rabbitmq2 ~]#
可以查看到rabbitmq1节点为disc磁盘节点,rabbitmq2节点和rabbitmq3节点为RAM内存节点。
7、新增用户admin,并设置密码为admin
[root@rabbitmq2 ~]# rabbitmqctl add_user admin admin
Creating user "admin" ...
...done.
修改用户密码为admin23
[root@rabbitmq2 ~]# rabbitmqctl change_password admin admin123
Changing password for user "admin" ...
...done.
删除用户
[root@rabbitmq2 ~]# rabbitmqctl delete_user admin
Deleting user "admin" ...
...done.
8、设置角色命令
[root@rabbitmq2 ~]# rabbitmqctl set_user_tags admin adminstrator monitoring policymaker management
Setting tags for user "admin" to [adminstrator,monitoring,policymaker,
management] ...
...done.
9、设置用户权限命令
指定的vhost必须有,所以我们需要创建一个
[root@rabbitmq2 ~]# rabbitmqctl add_vhost VHostPath
Creating vhost "VHostPath" ...
...done.
[root@rabbitmq2 ~]# rabbitmqctl set_permissions -p VHostPath admin ConfP WriteP ReadP
Setting permissions for user "admin" in vhost "VHostPath" ...
...done.
10、查询所有权限命令:
# rabbitmqctl list_permissions [-p VHostPath]
[root@rabbitmq2 ~]# rabbitmqctl list_permissions -p VHostPath
Listing permissions in vhost "VHostPath" ...
admin ConfP WriteP ReadP
...done.
11、指定用户权限命令:
# rabbitmqctl list_user_permissions admin
[root@rabbitmq2 ~]# rabbitmqctl list_user_permissions admin
Listing permissions for user "admin" ...
VHostPath ConfP WriteP ReadP
...done.
12、清除用户权限命令:
# rabbitmqctl clear_permissions [-p VHostPath] admin
[root@rabbitmq2 ~]# rabbitmqctl clear_permissions -p VHostPath admin
Clearing permissions for user "admin" in vhost "VHostPath" ...
...done.
以上是关于RabbitMQ集群的学习的主要内容,如果未能解决你的问题,请参考以下文章