##引言
消息系统允许软件、应用相互连接和扩展.这些应用可以相互链接起来组成一个更大的应用,或者将用户设备和数据进行连接.消息系统通过将消息的发送和接收分离来实现应用程序的异步和解偶.
或许你正在考虑进行数据投递,非阻塞操作或推送通知。或许你想要实现发布/订阅,异步处理,或者工作队列。所有这些都可以通过消息系统实现。
RabbitMQ是一个消息代理 – 一个消息系统的媒介。它可以为你的应用提供一个通用的消息发送和接收平台,并且保证消息在传输过程中的安全。
##安装 安装环境说明:
IP地址 主机名 OS版本
192.168.1.141 node1 CentOS7.2
1、安装前准备
(1)配置好epel源
[root@Bj-1-141 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@node1 ~]# hostnamectl set-hostname node1
[root@node1 ~]# hostname
[root@node1 ~]# exit
退出后重新连接,配置hosts
[root@node1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.141 node1
(2)配置主机名并能通过hosts解析
[root@node1 ~]# yum -y install rabbitmq-server
3、rabbitmq插件管理
RabbitMQ安装完成后使用rabbitmq-plugins管理相关插件。
语法格式:
Usage:
rabbitmq-plugins <command> [<command options>]
Commands:
list [-v] [-m] [-E] [-e] [<pattern>] #列出插件,显示大写E的为显式启用的,小写e为依赖启用
enable <plugin> ... #启动插件
disable <plugin> ... #禁用插件
4、启用rabbitmq_management插件
rabbitmq_management提供了一个http接口的管理控制台,监听在15672端口上,默认用户:guest 密码:guest
[root@node1 ~]# rabbitmq-plugins enable rabbitmq_management
5、启动rabbitmq-server
[root@node1 ~]# systemctl start rabbitmq-server.service
You have new mail in /var/spool/mail/root
[root@node1 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:25672 *:*
LISTEN 0 128 *:4369 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:15672 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::5672 :::*
LISTEN 0 128 :::4369 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
6、访问rabbitmq_management提供的管理接口


在控制台修改guest用户的密码

##RabbitMQ集群配置
环境说明:
IP地址 主机名 OS版本 角色
192.168.1.141 node1 CentOS7.2 Master
192.168.1.142 node2 CentOS7.2 Slave
1、配置主机名及hosts文件
两台节点都要配置,这里以node2节点为例。
[root@node2 ~]# hostnamectl set-hostname node2
[root@node1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.141 node1
192.168.1.142 node2
2、各节点安装rabbitmq-server
两台节点都要配置,这里以node2节点为例。
[root@node2 ~]# yum -y install rabbitmq-server
3、各节点启用rabbitmq_management插件
两台节点都要配置,这里以node2节点为例。
[root@node2 ~]# rabbitmq-plugins enable rabbitmq_management
4、启动rabbitmq-server
两台节点都要配置,这里以node2节点为例。
[root@node2 ~]# systemctl start rabbitmq-server.service
[root@node2 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:25672 *:*
LISTEN 0 128 *:4369 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:15672 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::5672 :::*
LISTEN 0 128 :::4369 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
5、查看各节点上的cluster name
(1)查看node1节点
[root@node1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@node1 ...
[{nodes,[{disc,[rabbit@node1]}]},
{running_nodes,[rabbit@node1]},
{cluster_name,<<"rabbit@node1">>},#集群名称
{partitions,[]}]
...done.
(2)查看node2节点
[root@node2 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@node2 ...
[{nodes,[{disc,[rabbit@node2]}]},
{running_nodes,[rabbit@node2]},
{cluster_name,<<"rabbit@node2">>},#集群名称
{partitions,[]}]
...done.
6、复制node1节点的erlang.cookie到slave节点的/var/lib/rabbitmq/目录
(1)停止node2节点的rabbitmq-server
[root@node2 ~]# systemctl stop rabbitmq-server.service
(2)复制node1节点erlang.cookie
[root@node1 ~]# scp /var/lib/rabbitmq/.erlang.cookie node2:/var/lib/rabbitmq/
(3)启动node2节点rabbitmq-server
[root@node2 ~]# systemctl start rabbitmq-server.service
7、在node2节点停止应用
[root@node2 ~]# rabbitmqctl stop_app
Stopping node rabbit@node2 ...
...done.
8、配置node2加入node1节点
[root@node2 ~]# rabbitmqctl join_cluster rabbit@node1
Clustering node rabbit@node2 with rabbit@node1 ...
...done.
[root@node2 ~]# rabbitmqctl start_app
Starting node rabbit@node2 ...
...done.
9、查看集群状态
(1)查看node1节点集群状态
[root@node1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@node1 ...
[{nodes,[{disc,[rabbit@node1,rabbit@node2]}]},#集群内的节点
{running_nodes,[rabbit@node2,rabbit@node1]},#正在运行的节点
{cluster_name,<<"rabbit@node1">>},
{partitions,[]}]
...done.
(2)查看node2节点集群状态
[root@node2 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@node2 ...
[{nodes,[{disc,[rabbit@node1,rabbit@node2]}]},
{running_nodes,[rabbit@node1,rabbit@node2]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]}]
...done.
(3)在rabbitmq-management接口查看集群状态

在集群模式中可以通过haproxy的tcp模式来实现对RabbitMQ集群实现负载均衡
本文详细介绍了如何在CentOS7.2环境下安装和配置RabbitMQ消息系统,包括单节点安装、插件管理、服务启动,以及如何构建RabbitMQ集群,实现负载均衡。
1130

被折叠的 条评论
为什么被折叠?



