首页 > 代码库 > RabbitMq 集群搭建

RabbitMq 集群搭建

??

实验环境:

操作系统为 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

    ??

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

    ??

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

    ??

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

    [root@node139 ~]# rpm --import https://www.rabbitmq.com/rabbitmq-signing-key-public.asc

    [root@node139 ~]# yum install rabbitmq-server-3.6.6-1.el7.noarch.rpm

    ??

    启动服务

    [root@node139 ~]# systemctl enable rabbitmq-server.service

    [root@node139 ~]# systemctl start rabbitmq-server.service

    ??


    启用RabbitMQ监控插件

    [root@node139 ~]# 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 rabbit@node139... started 6 plugins.

    [root@node139 ~]# netstat -apn | grep 15672

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

    ??


    RabbitMQ用户管理

    ??

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

    [root@node139 ~]# rabbitmqctl add_user admin admin

    ??

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

    [root@node139 ~]# rabbitmqctl set_user_tags admin administrator

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

    ??

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

    [root@node139 ~]# rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"

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

    ??

    删除用户(删除guest用户)

    [root@node139 ~]# rabbitmqctl delete_user guest

    ??

    ??

    RabbitMQ集群配置

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

    以下以node140为例,node132需要做同样的操作

    • 添加/etc/hosts条目

    node132node139node140里的/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保持一致,否则节点之间就无法通信

    ??

    [root@node140 /]# echo -n "AZVOCZYZZBVFLBPTBXU" > /var/lib/rabbitmq/.erlang.cookie

    [root@node140 ~]# chown rabbitmq:root /var/lib/rabbitmq/.erlang.cookie

    [root@node140 /]# chmod 600 /var/lib/rabbitmq/.erlang.cookie

    [root@node140 /]# cat /var/lib/rabbitmq/.erlang.cookie

    AZVOCZYZZBVFLBPTBXU

    ??

    启动服务

    [root@node140 ~]# systemctl start rabbitmq-server.service

    [root@node140 ~]# rabbitmq-server -detached

    ??

    开通防火墙

    [root@node140 ~]# firewall-cmd --permanent --add-port={4369/tcp,25672/tcp}

    success

    [root@node140 ~]# firewall-cmd --reload

    success

    ??

  • 加入集群

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

    ??

    [root@node140 ~]# rabbitmqctl stop_app

    Stopping node rabbit@node140 ...

    [root@node140 ~]# rabbitmqctl join_cluster rabbit@node139

    Clustering node rabbit@node140 with rabbit@node139 ...

    [root@node140 ~]# rabbitmqctl start_app

    Starting node rabbit@node140 ...

  • 设置镜像策略

    ??

    [root@node140 ~]# 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管理插件还未启用。

    [root@node140 ~]# rabbitmq-plugins enable rabbitmq_management

    ??

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

    [root@node140 ~]# rabbitmqctl cluster_status

    ??

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

    ??

    ??

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

    ??

    技术分享

    ??

    技术分享

    ??

    ??

    ??

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

    安装中遇到的问题:

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

    ??

    ??

    ??

    ??

    ??

    ??

    ??

    ??

    ??

    ??

    ??

    ??

    ??

    ??

RabbitMq 集群搭建