CentOS 7.2 下 RabbitMQ 集群搭建

摘要

实验环境: 操作系统为 CentOS 7.2 IP hostName 192.168.190.132 node […]

实验环境:

操作系统为 CentOS 7.2

IP hostName

  • 192.168.190.132 node132
  • 192.168.190.139 node139
  • 192.168.190.140 node140

RabbitMQ相关端口

•4369 (epmd)
•5672, 5671 (AMQP 0-9-1 and 1.0 without and with TLS)
•25672. This port used by Erlang distribution for inter-node and CLI tools communication and is allocated from a dynamic range (limited to a single port by default, computed as AMQP port + 20000). Seenetworking guide for details.
•15672 (if management plugin is enabled)
•61613, 61614 (if STOMP is enabled)

•1883, 8883 (if MQTT is enabled)

相应端口开启防火墙设置

添加EPEL源

[[email protected] ~]# rpm -Uvh https://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Retrieving https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

warning: /var/tmp/rpm-tmp.2hDPSr: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY

Preparing... ################################# [100%]

Updating / installing...

 1:epel-release-7-8 ################################# [100%]

添加Erlang源

[[email protected] ~]# rpm -Uvh http://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm

Retrieving http://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm

Preparing... ################################# [100%]

Updating / installing...

 1:erlang-solutions-1.0-1 ################################# [100%]

--2016-11-29 06:34:38-- http://packages.erlang-solutions.com/rpm/centos/erlang_solutions.repo

Resolving packages.erlang-solutions.com (packages.erlang-solutions.com)... 31.172.186.53

Connecting to packages.erlang-solutions.com (packages.erlang-solutions.com)|31.172.186.53|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 245

Saving to: 'erlang_solutions.repo'

100%[==========================================================================>] 245 --.-K/s in 0s

2016-11-29 06:34:39 (27.5 MB/s) - 'erlang_solutions.repo' saved [245/245]

安装RabbitMQ

软件版本:http://www.rabbitmq.com/releases/rabbitmq-server

[[email protected] ~]# wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-3.6.6-1.el7.noarch.rpm

[[email protected] ~]# rpm --import https://www.rabbitmq.com/rabbitmq-signing-key-public.asc

[[email protected] ~]# yum install rabbitmq-server-3.6.6-1.el7.noarch.rpm

启动服务

[[email protected] ~]# systemctl enable rabbitmq-server.service

[[email protected] ~]# systemctl start rabbitmq-server.service

启用RabbitMQ监控插件

[[email protected] ~]# rabbitmq-plugins enable rabbitmq_management

The following plugins have been enabled:

 mochiweb

 webmachine

 rabbitmq_web_dispatch

 amqp_client

 rabbitmq_management_agent

 rabbitmq_management

Applying plugin configuration to [email protected].. started 6 plugins.

[[email protected] ~]# netstat -apn | grep 15672

tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 4119/beam.smp

RabbitMQ用户管理

添加用户(用户名root,密码admin)

[[email protected] ~]# rabbitmqctl add_user admin admin

设置用户角色(设置admin用户为管理员角色)

[[email protected] ~]# rabbitmqctl set_user_tags admin administrator

Setting tags for user "admin" to [administrator] ...

设置用户权限(设置admin用户配置、写、读的权限)

[[email protected] ~]# rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"

Setting permissions for user "admin" in vhost "/" ...

删除用户(删除guest用户)

[[email protected] ~]# rabbitmqctl delete_user guest

RabbitMQ集群配置

集群配置在单机配置完成的基础上进行

以下以node140为例,node132需要做同样的操作
•添加/etc/hosts条目

在node132、node139、node140里的/etc/hosts文件中分别添加:

192.168.190.132 node132
192.168.190.139 node139
192.168.190.140 node140

 
•设置每个节点Cookie

Rabbitmq的集群是依赖于erlang的集群来工作的,所以必须先构建起erlang的集群环境。Erlang的集群中各节点是通过一个magic cookie来实现的,这个cookie存放在 /var/lib/rabbitmq/.erlang.cookie 中,文件是400的权限。所以必须保证各节点cookie保持一致,否则节点之间就无法通信

[[email protected] /]# echo -n "AZVOCZYZZBVFLBPTBXU" > /var/lib/rabbitmq/.erlang.cookie

[[email protected] ~]# chown rabbitmq:root /var/lib/rabbitmq/.erlang.cookie

[[email protected] /]# chmod 600 /var/lib/rabbitmq/.erlang.cookie

[[email protected] /]# cat /var/lib/rabbitmq/.erlang.cookie

AZVOCZYZZBVFLBPTBXU

启动服务

[[email protected] ~]# systemctl start rabbitmq-server.service

[[email protected] ~]# rabbitmq-server -detached

开通防火墙

[[email protected] ~]# firewall-cmd --permanent --add-port={4369/tcp,25672/tcp}

success

[[email protected] ~]# firewall-cmd --reload

success

•加入集群

默认是磁盘节点,如果是内存节点的话,需要加--ram参数

[[email protected] ~]# rabbitmqctl stop_app

Stopping node [email protected] ...

[[email protected] ~]# rabbitmqctl join_cluster [email protected]

Clustering node [email protected] with [email protected] ...

[[email protected] ~]# rabbitmqctl start_app

Starting node [email protected] ...

•设置镜像策略

[[email protected] ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'

Setting policy "ha-all" for pattern "^" to "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"automatic\"}" with priority "0" …

启用RabbitMQ监控插件

cluster搭建起来后若在web管理工具中rabbitmq_management的Overview的Nodes部分看到"Node statistics not available"的信息,说明在该节点上web管理插件还未启用。

[[email protected] ~]# rabbitmq-plugins enable rabbitmq_management

集群配置好后,可以在 RabbitMQ 任意节点上执行下面的命令来查看是否集群配置成功。

[[email protected] ~]# rabbitmqctl cluster_status

其它节点需要做同样的操作

登录站点http://192.168.190.139:15672/

CentOS 7.2 下 RabbitMQ 集群搭建

参考网址:http://www.rabbitmq.com/clustering.html

安装中遇到的问题:

这个/var/lib/rabbitmq/.erlang.cookie文件需要修改文件所有者和文件的权限为rw,否则rabbitmq服务无法启动;

CentOS 5.6 安装RabbitMQ http://www.searu.org/2013-02/79508.htm

RabbitMQ客户端C++安装详细记录 http://www.searu.org/2012-02/53521.htm

用Python尝试RabbitMQ http://www.searu.org/2011-12/50653.htm

RabbitMQ集群环境生产实例部署 http://www.searu.org/2012-10/72720.htm

CentOS7环境安装使用专业的消息队列产品RabbitMQ http://www.searu.org/2016-11/13673.htm

在CentOS上安装RabbitMQ流程 http://www.searu.org/2011-12/49610.htm

RabbitMQ概念及环境搭建 http://www.searu.org/2014-12/110449.htm

RabbitMQ入门教程  http://www.searu.org/2015-02/113983.htm

RabbitMQ 的详细介绍请点这里
RabbitMQ 的下载地址请点这里

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: