RabbitMQ消息中间件搭建详解
1.RabbitMQ简介
消息中间件也可以称消息队列,是指用高效可靠的消息传递机制进行与平台无关的数据交流,并基于数据通信来进行分布式系统的集成。通过提供消息传递和消息队列模型,可以在分布式环境下扩展进程的通信。
RabbitMQ是使用Erlang语言开发的开源消息队列系统,基于AMQP协议来实现。AMQP的主要特征是面向消息、队列、路由(包括点对点和发布/订阅)、可靠性、安全。AMQP协议更多用在企业系统内对数据一致性、稳定性和可靠性要求很高的场景,对性能和吞吐量的要求还在其次。
Erlang是一种通用的面向并发的编程语言
特点
- 可靠性
- 扩展性
- 高可用性
- 多种协议
- 多语言客户端
- 管理界面
- 插件机制
2.集群部署
1.环境部署及软件安装
准备三台虚拟机192.168.13.139 rabbitmq-1
192.168.13.142 rabbitmq-2
192.168.13.143 rabbitmq-3
配置hosts文件更改三台MQ节点的计算机名分别为rabbitmq-1、rabbitmq-2 和rabbitmq-3,然后修改hosts配置件,并关闭防火墙和selinux
192.168.13.139[root@rabbitmq-1 ~]# hostnamectl set-hostname rabbitmq-1
[root@rabbitmq-1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.13.139 rabbitmq-1
192.168.13.142 rabbitmq-2
192.168.13.143 rabbitmq-3
[root@rabbitmq-1 ~]# systemctl stop firewalld && setenforce 0
192.168.13.142[root@rabbitmq-2 ~]# hostnamectl set-hostname rabbitmq-2
[root@rabbitmq-2 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.13.139 rabbitmq-1
192.168.13.142 rabbitmq-2
192.168.13.143 rabbitmq-3
[root@rabbitmq-2 ~]# systemctl stop firewalld && setenforce 0
192.168.13.143[root@rabbitmq-3 ~]# hostnamectl set-hostname rabbitmq-3
[root@rabbitmq-3 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.13.139 rabbitmq-1
192.168.13.142 rabbitmq-2
192.168.13.143 rabbitmq-3
[root@rabbitmq-3 ~]# systemctl stop firewalld && setenforce 0
安装rabbitmq软件以及配置
192.168.13.139[root@rabbitmq-1 ~]# yum install -y *epel* gcc-c++ unixODBC unixODBC-devel openssl-devel ncurses-devel
1.yum安装erlang
[root@rabbitmq-1 ~]# wget --content-disposition https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-20.3-1.el7.centos.x86_64.rpm/download.rpm #Erlang环境包
[root@rabbitmq-1 ~]# yum install erlang-20.3-1.el7.centos.x86_64.rpm -y
测试;
[root@rabbitmq-1 ~]# erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1>
2.安装rabbitmq
[root@rabbitmq-1 ~]# wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.5/rabbitmq-server-3.7.5-1.el7.noarch.rpm
[root@rabbitmq-1 ~]# yum -y install rabbitmq-server-3.7.5-1.el7.noarch.rpm
3.启动
[root@rabbitmq-1 ~]# systemctl daemon-reload
[root@rabbitmq-1 ~]# systemctl start rabbitmq-server
[root@rabbitmq-1 ~]# systemctl enable rabbitmq-server
[root@rabbitmq-1 ~]# systemctl status rabbitmq-server
[root@rabbitmq-1 ~]# rabbitmq-plugins enable rabbitmq_management #管理界面
#如果报错,重启服务就好了
192.168.13.1394.创建用户
注意:在一台机器操作
添加用户和密码
[root@rabbitmq-1 ~]# rabbitmqctl add_user yjssjm yjssjm
Adding user "yjssjm" ...
设置为管理员
[root@rabbitmq-1 ~]# rabbitmqctl set_user_tags yjssjm administrator
Setting tags for user "yjssjm" to [administrator] ...
查看用户
[root@rabbitmq-1 ~]# rabbitmqctl list_users
Listing users ...
guest [administrator]
yjssjm [administrator]
...done.
[root@rabbitmq-1 ~]# rabbitmqctl set_permissions -p "/" yjssjm ".*" ".*" ".*"
Setting permissions for user "yjssjm" in vhost "/" ...
#此处设置权限时注意".*"之间需要有空格 三个".*"分别代表了conf权限,read权限与write权限 例如:当没有给
5.开启用户远程登录
[root@rabbitmq-1 ~]# cd /etc/rabbitmq/
[root@rabbitmq-1 rabbitmq]# cp /usr/share/doc/rabbitmq-server-3.7.5/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
[root@rabbitmq-1 rabbitmq]# ls
enabled_plugins rabbitmq.config
[root@rabbitmq-1 rabbitmq]# vim rabbitmq.config
修改如下: 在61行左右
{loopback_users, []}
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server #重启服务
192.168.13.142[root@rabbitmq-2 ~]# yum install -y *epel* gcc-c++ unixODBC unixODBC-devel openssl-devel ncurses-devel
1.yum安装erlang
[root@rabbitmq-2 ~]# wget --content-disposition https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-20.3-1.el7.centos.x86_64.rpm/download.rpm #Erlang环境包
[root@rabbitmq-2 ~]# yum install erlang-20.3-1.el7.centos.x86_64.rpm -y
[root@rabbitmq-2 ~]# erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1>
2.安装rabbitmq
[root@rabbitmq-2 ~]# wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.5/rabbitmq-server-3.7.5-1.el7.noarch.rpm
[root@rabbitmq-2 ~]# yum -y install rabbitmq-server-3.7.5-1.el7.noarch.rpm
3.启动
[root@rabbitmq-2 ~]# systemctl daemon-reload
[root@rabbitmq-2 ~]# systemctl start rabbitmq-server
[root@rabbitmq-2 ~]# systemctl enable rabbitmq-server
[root@rabbitmq-2 ~]# systemctl status rabbitmq-server
[root@rabbitmq-2 ~]# rabbitmq-plugins enable rabbitmq_management #管理界面
#如果报错,重启服务就好了
4.开启用户远程登录
[root@rabbitmq-2 ~]# cd /etc/rabbitmq/
[root@rabbitmq-2 rabbitmq]# cp /usr/share/doc/rabbitmq-server-3.7.5/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
[root@rabbitmq-2 rabbitmq]# ls
enabled_plugins rabbitmq.config
[root@rabbitmq-2 rabbitmq]# vim rabbitmq.config
修改如下: 在61行左右
{loopback_users, []}
[root@rabbitmq-2 ~]# systemctl restart rabbitmq-server
192.168.13.143[root@rabbitmq-3 ~]# yum install -y *epel* gcc-c++ unixODBC unixODBC-devel openssl-devel ncurses-devel
1.yum安装erlang
[root@rabbitmq-3 ~]# wget --content-disposition https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-20.3-1.el7.centos.x86_64.rpm/download.rpm #Erlang环境包
[root@rabbitmq-3 ~]# yum install erlang-20.3-1.el7.centos.x86_64.rpm -y
[root@rabbitmq-3 ~]# erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1>
2.安装rabbitmq
[root@rabbitmq-3 ~]# wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.5/rabbitmq-server-3.7.5-1.el7.noarch.rpm
[root@rabbitmq-3 ~]# yum -y install rabbitmq-server-3.7.5-1.el7.noarch.rpm
3.启动
[root@rabbitmq-3 ~]# systemctl daemon-reload
[root@rabbitmq-3 ~]# systemctl start rabbitmq-server
[root@rabbitmq-3 ~]# systemctl enable rabbitmq-server
[root@rabbitmq-3 ~]# systemctl status rabbitmq-server
[root@rabbitmq-3 ~]# rabbitmq-plugins enable rabbitmq_management #管理界面
#如果报错,重启服务就好了
4.开启用户远程登录
[root@rabbitmq-3 ~]# cd /etc/rabbitmq/
[root@rabbitmq-3 rabbitmq]# cp /usr/share/doc/rabbitmq-server-3.7.5/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
[root@rabbitmq-3 rabbitmq]# ls
enabled_plugins rabbitmq.config
[root@rabbitmq-3 rabbitmq]# vim rabbitmq.config
修改如下: 在61行左右
{loopback_users, []}
[root@rabbitmq-3 ~]# systemctl restart rabbitmq-server
2.访问网页
查看端口[root@rabbitmq-1 rabbitmq]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:4369 0.0.0.0:* LISTEN 1205/epmd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 985/sshd
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 988/beam.smp
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1285/master
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 988/beam.smp
tcp6 0 0 :::4369 :::* LISTEN 1205/epmd
tcp6 0 0 :::22 :::* LISTEN 985/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1285/master
tcp6 0 0 :::5672 :::* LISTEN 988/beam.smp
=========================================================================================
4369 -- erlang端口
5672 --程序连接端口
15672 -- 管理界面ui端口
25672 -- server间内部通信端口
访问页面:192.168.13.139:15672
rabbitmq默认管理员用户:guest 密码:guest
新添加的用户为:yjssjm 密码:yjssjm
3.部署集群
192.168.13.1391.首先创建好数据存放目录和日志存放目录:
[root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/data
[root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/logs
[root@rabbitmq-1 ~]# chmod 777 -R /data/rabbitmq
[root@rabbitmq-1 ~]# chown rabbitmq.rabbitmq /data/ -R
[root@rabbitmq-1 ~]# vim /etc/rabbitmq/rabbitmq-env.conf #创建配置文件
添加下面的内容:
RABBITMQ_MNESIA_BASE=/data/rabbitmq/data
RABBITMQ_LOG_BASE=/data/rabbitmq/logs
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server #重启服务
2.拷erlang.cookie
Rabbitmq的集群是依附于erlang的集群来工作的,所以必须先构建起erlang的集群。Erlang的集群中
各节点是经由各个cookie来实现的,这个cookie存放在/var/lib/rabbitmq/.erlang.cookie中,文件是400的权限。所以必须保证各节点cookie一致,不然节点之间就无法通信.
(官方在介绍集群的文档中提到过.erlang.cookie 一般会存在这两个地址:第一个是home/.erlang.cookie;第二个地方就是/var/lib/rabbitmq/.erlang.cookie。如果我们使用解压缩方式安装部署的rabbitmq,那么这个文件会在{home}目录下,也就是$home/.erlang.cookie。如果我们使用rpm等安装包方式进行安装的,那么这个文件会在/var/lib/rabbitmq目录下。)
=======================================================================================
[root@rabbitmq-1 ~]# cat /var/lib/rabbitmq/.erlang.cookie
HOUCUGJDZYTFZDSWXTHJ
scp的方式将rabbitmq-1节点的.erlang.cookie的值复制到其他两个节点中。
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@192.168.13.142:/var/lib/rabbitmq/
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@192.168.13.143:/var/lib/rabbitmq/
192.168.13.1421.首先创建好数据存放目录和日志存放目录:
[root@rabbitmq-2 ~]# mkdir -p /data/rabbitmq/data
[root@rabbitmq-2 ~]# mkdir -p /data/rabbitmq/logs
[root@rabbitmq-2 ~]# chmod 777 -R /data/rabbitmq
[root@rabbitmq-2 ~]# chown rabbitmq.rabbitmq /data/ -R
[root@rabbitmq-2 ~]# vim /etc/rabbitmq/rabbitmq-env.conf #创建配置文件
添加下面的内容:
RABBITMQ_MNESIA_BASE=/data/rabbitmq/data
RABBITMQ_LOG_BASE=/data/rabbitmq/logs
[root@rabbitmq-2 ~]# systemctl restart rabbitmq-server #重启服务
2.将mq-2作为内存节点加到mq-1节点集群中
[root@rabbitmq-2 ~]# rabbitmqctl stop_app #停止节点,切记不是停止服务
[root@rabbitmq-2 ~]# rabbitmqctl reset #如果有数据需要重置,没有则不用
如果执行# rabbitmqctl stop_app 这条命令报错
注意查看回显,如果不是以上图片中的就是错误;如果报错,重启rabbitmq服务[root@rabbitmq-2 ~]# systemctl restart rabbitmq-server
如果还是报错:将传过来的文件给与正确的权限
[root@rabbitmq-2 ~]# chmod 400 /var/lib/rabbitmq/.erlang.cookie
[root@rabbitmq-2 ~]# chown rabbitmq.rabbitmq /var/lib/rabbitmq/.erlang.cookie
将节点2加入集群,指定角色[root@rabbitmq-2 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1 #添加到内存节点
Clustering node "rabbit@rabbitmq-2" with "rabbit@rabbitmq-1" ...
[root@rabbitmq-2 ~]# rabbitmqctl start_app #启动节点
Starting node "rabbit@rabbitmq-2" ...
completed with 3 plugins.
192.168.13.1431.首先创建好数据存放目录和日志存放目录:
[root@rabbitmq-3 ~]# mkdir -p /data/rabbitmq/data
[root@rabbitmq-3 ~]# mkdir -p /data/rabbitmq/logs
[root@rabbitmq-3 ~]# chmod 777 -R /data/rabbitmq
[root@rabbitmq-3 ~]# chown rabbitmq.rabbitmq /data/ -R
[root@rabbitmq-3 ~]# vim /etc/rabbitmq/rabbitmq-env.conf
添加下面的内容:
RABBITMQ_MNESIA_BASE=/data/rabbitmq/data
RABBITMQ_LOG_BASE=/data/rabbitmq/logs
[root@rabbitmq-3 ~]# systemctl restart rabbitmq-server
2.将mq-2作为内存节点加到mq-1节点集群中
[root@rabbitmq-3 ~]# rabbitmqctl stop_app
[root@rabbitmq-3 ~]# rabbitmqctl reset
[root@rabbitmq-3 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1
Clustering node "rabbit@rabbitmq-3" with "rabbit@rabbitmq-1" ...
[root@rabbitmq-3 ~]# rabbitmqctl start_app
Starting node "rabbit@rabbitmq-3" ...
completed with 3 plugins.
(1)默认rabbitmq启动后是磁盘节点,在这个cluster命令下,mq-2和mq-3是内存节点,mq-1是磁盘节点。
(2)如果要使mq-2、mq-3都是磁盘节点,去掉--ram参数即可。
(3)如果想要更改节点类型,可以使用命令rabbitmqctl change_cluster_node_type
disc(ram),前提是必须停掉rabbitmq应用
查看集群状态
在 RabbitMQ 集群任意节点上执行 rabbitmqctl cluster_status来查看是否集群配置成功。在mq-1磁盘节点上面查看
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
每台机器显示出三台节点,表示已经添加成功!
登录查看
打开浏览器输入http://192.168.13.139:15672
你们的评论和点赞是我写文章的最大动力,蟹蟹。
以上是 RabbitMQ消息中间件搭建详解 的全部内容, 来源链接: utcz.com/z/515126.html