RabbitMQ集群搭建

2020-11-24 10:32:43 浏览数 (1)

  • RabbitMQ本身是基于Erlang编写的,Erlang天生支持分布式(通过同步Erlang集群各节点的cookie来实现),因此不需要像Kafka那样通过ZooKeeper来实现分布式集群。
  • 元数据,RabbitMQ内部有各

种基础构件,包括队列、交换器、绑定、虚拟主机等,他们组成了AMQP协议消息通信的基础,而这些构件以元数据的形式存在

  • 内存节点与磁盘节点,在集群中的每个节点,要么是内存节点,要么是磁盘节点,如果是内存节点,会将所有的元数据信息仅存储到内存中,而磁盘节点则不仅会将所有元数据存储到内存上, 还会将其持久化到磁盘。所以在搭建集群的时候,为了保证数据的安全性和性能,最好是两种节点都要有

规划

主机名

IP

节点类型

linux01

192.168.17.128

磁盘节点

linux02

192.168.17.130

内存节点

linux03

192.168.17.132

内存节点

部署集群

1.配置hosts文件和主机名

三台主机分别配置hosts文件,以及设置主机名。

代码语言:javascript复制
## 设置hosts文件
[root@linux-01 ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.17.128  linux-01
192.168.17.130  linux-02
192.168.17.132  linux-03

## 分别设置主机名,命令如下
[root@linux-01 ~]# hostnamectl set-hostname linux-01

2. 关闭selinux和firewalld

三台虚拟主机分别做如下设置

代码语言:javascript复制
## 关闭selinux,使用setenforce 0可以关闭selinux
[root@linux-01 ~]# setenforce 0
setenforce: SELinux is disabled
[root@linux-01 ~]# getenforce
Disabled

## 关闭firewalld
[root@linux-01 ~]# systemctl stop firewalld 

3. 安装rabbitmq

三台虚拟主机分别安装rabbitmq

代码语言:javascript复制
[root@linux-01 ~]# curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash

[root@linux-01 ~]# yum install -y erlang

[root@linux-01 ~]# rpm --import https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey

[root@linux-01 ~]# rpm --import https://packagecloud.io/gpg.key

[root@linux-01 ~]# rpm --import https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc

[root@linux-01 ~]# vi /etc/yum.repos.d/rabbitmq.repo
[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


[root@linux-01 ~]# yum install -y rabbitmq-server

4.启动rabbitmq-server

三台虚拟机都需要启动rabbitmq-server以及rabbitmq_management

代码语言:javascript复制
[root@linux-01 ~]# systemctl start rabbitmq-server

[root@linux-01 ~]# rabbitmq-plugins enable rabbitmq_management

5. 编辑cookie文件

保证三台电脑的cookie文件信息一致,从linux-01上copy文件到linux-02和linux-03上,文件位置是:/var/lib/rabbitmq/.erlang.cookie

代码语言:javascript复制
[root@linux-01 ~]# scp /var/lib/rabbitmq/.erlang.cookie  linux-02:/var/lib/rabbitmq/.erlang.cookie
root@linux-02's password:
.erlang.cookie                                                                                                  100%   20    17.5KB/s   00:00
[root@linux-01 ~]# scp /var/lib/rabbitmq/.erlang.cookie  linux-03:/var/lib/rabbitmq/.erlang.cookie
Warning: the ECDSA host key for 'linux-03' differs from the key for the IP address '192.168.17.132'
Offending key for IP in /root/.ssh/known_hosts:2
Matching host key in /root/.ssh/known_hosts:4
Are you sure you want to continue connecting (yes/no)? yes
root@linux-03's password:
.erlang.cookie

6.重启rabbitmq服务

由于linux-02和linux-03上的cookies信息修改,所以需要重启linux-02和linux-03的rebbitmq服务。

代码语言:javascript复制
## 重启rabbitmq服务,linux-03也需要这样操作。
[root@linux-02 ~]# systemctl start rabbitmq-server

7.添加节点

linux-01为硬盘节点,linux-02和linux-03是内存节点,所以分别在linux-02和linux-03上设置如下内容。

代码语言:javascript复制
## 以下操作是在linux-02上操作,linux-03同理
[root@linux-02 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@linux-02 ...

## rabbitmqctl join_cluster --ram rabbit@linux-01 记得在复制的时候查看自己的主机名
[root@linux-02 ~]# rabbitmqctl join_cluster --ram rabbit@linux-01
Clustering node rabbit@linux-02 with rabbit@linux-01

[root@linux-02 ~]# rabbitmqctl start_app
Starting node rabbit@linux-02 ...

8.查看集群状态

可以通过linux-01的web节目查看到集群上添加的节点。

代码语言:javascript复制
[root@linux-01 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@linux-01 ...
Basics

Cluster name: rabbit@linux-01

Disk Nodes

rabbit@linux-01

RAM Nodes

rabbit@linux-02
rabbit@linux-03

Running Nodes

rabbit@linux-01
rabbit@linux-02
rabbit@linux-03

Versions

rabbit@linux-01: RabbitMQ 3.8.9 on Erlang 23.1.2
rabbit@linux-02: RabbitMQ 3.8.9 on Erlang 23.1.2
rabbit@linux-03: RabbitMQ 3.8.9 on Erlang 23.1.2

Maintenance status

Node: rabbit@linux-01, status: not under maintenance
Node: rabbit@linux-02, status: not under maintenance
Node: rabbit@linux-03, status: not under maintenance

Alarms

(none)

Network Partitions

(none)

Listeners

Node: rabbit@linux-01, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@linux-01, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@linux-01, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@linux-02, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@linux-02, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@linux-02, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@linux-03, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@linux-03, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@linux-03, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0

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
[root@linux-01 ~]#

0 人点赞