RabbitMQ基础使用之集群构建

时间:2024-01-22 20:28:53

简介

RabbitMQ是基于Erlang开发的一种消息队列服务,本篇文章主要部署三台机器用来实现集群的普通模式与镜像模式!欢迎大家吐槽交流学习!

特点

集群节点包括内存节点和磁盘节点,有了磁盘节点就支持消息的持久化。

1.安装

1.1安装前的准备工作

1.首先查看系统版本,我这里三台阿里云CentOS!
[root@Ali-Zookeeper-MQ01 rabbit-rpm]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
2.规划修改所有节点的主机名。
[root@Ali-Zookeeper-MQ01 rabbit-rpm]# cat /etc/hostname
Ali-Zookeeper-MQ01
[root@Ali-Zookeeper-MQ01 rabbit-rpm]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.5.0.203 Ali-Zookeeper-MQ01
10.5.0.228 Ali-Zookeeper-MQ02
10.5.0.229 Ali-Zookeeper-MQ03
另外俩台改成对应的配置!
3.关闭防火墙
# 停止firewall
systemctl stop firewalld.service
# 禁止firewall开机启动
systemctl disable firewalld.service
# 查看防火墙状态
[root@Ali-Zookeeper-MQ01 ~]# firewall-cmd --state
not running
4.安装Erlang
# yum安装epel-release
yum -y install epel-release
# 安装Erlang环境
yum -y install erlang  

1.2使用rpm正式安装mq

# 创建目录用来存放rabbitmq的rpm
mkdir /usr/local/src/rabbit-rpm
# 切换到/usr/local/rabbit-rpm目录                
cd /usr/local/src/rabbit-rpm
# 下载RabbitMQ-3.6.9安装包rpm
wget  http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.9/rabbitmq-server-3.6.9-1.el7.noarch.rpm 
# 开始安装,当看到Complete!提示时,安装完成!    
yum -y install rabbitmq-server-3.6.9-1.el7.noarch.rpm

1.3配置mq集群-普通模式

首先要保证三台机器安装的软件版本一致,主机名配置文件一致!
集群-普通模式-配置操作

1.设置Erlang Cookie 
  Erlang Cookie 文件:/var/lib/rabbitmq/.erlang.cookie。三台主机分别创建,给予400权限修改属主属组。
vim /var/lib/rabbitmq/.erlang.cookie
chmod 777 /var/lib/rabbitmq/.erlang.cookie
scp -r /var/lib/rabbitmq/.erlang.cookie root@Ali-Zookeeper-MQ02:/var/lib/rabbitmq/.erlang.cookie
scp -r /var/lib/rabbitmq/.erlang.cookie root@Ali-Zookeeper-MQ03:/var/lib/rabbitmq/.erlang.cookie
chmod 400 /var/lib/rabbitmq/.erlang.cookie
chown rabbitmq /var/lib/rabbitmq/.erlang.cookie
chgrp rabbitmq /var/lib/rabbitmq/.erlang.cookie
2.启动各节点 
service rabbitmq-server start
组成集群,在2,3节点上执行命令将2,3加入1.
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@Ali-Zookeeper-MQ01 --ram
rabbitmqctl start_app
3.如果需要退出集群
rabbitmqctl stop_app 
rabbitmqctl reset 
rabbitmqctl start_app
4.查看集群状态
[root@Ali-Zookeeper-MQ01 ~]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@Ali-Zookeeper-MQ01' ...
[{nodes,[{disc,['rabbit@Ali-Zookeeper-MQ01']},
         {ram,['rabbit@Ali-Zookeeper-MQ03','rabbit@Ali-Zookeeper-MQ02']}]},
 {running_nodes,['rabbit@Ali-Zookeeper-MQ03','rabbit@Ali-Zookeeper-MQ02',
                 'rabbit@Ali-Zookeeper-MQ01']},
 {cluster_name,<<"rabbit@Ali-Zookeeper-MQ01">>},
 {partitions,[]},
 {alarms,[{'rabbit@Ali-Zookeeper-MQ03',[]},
          {'rabbit@Ali-Zookeeper-MQ02',[]},
          {'rabbit@Ali-Zookeeper-MQ01',[]}]}]
5.三台上都开启web管理功能
rabbitmq-plugins enable rabbitmq_management

1.4补充说明

启动 RabbitMQ 服务
systemctl start rabbitmq-server
查看 RabbitMQ 状态
systemctl status rabbitmq-server
停止 RabbitMQ 服务
systemctl stop rabbitmq-server
开启web管理
rabbitmq-plugins enable rabbitmq_management 
查看当前所有用户
rabbitmqctl list_users
查看默认guest用户的权限
rabbitmqctl list_user_permissions guest
由于RabbitMQ默认的账号用户名和密码都是guest。为了安全起见, 删掉默认用户guest
rabbitmqctl delete_user guest
添加新用户
rabbitmqctl add_user username password
设置用户tag
rabbitmqctl set_user_tags username administrator
赋予用户默认vhost的全部操作权限
rabbitmqctl set_permissions -p / username “.” “.” “.*”