1. 实验环境

192.168.3.110   rabbitmq-test1  
192.168.3.120   rabbitmq-test2

2.修改主机名

[root@rabbitmq-test1 ~]# cat /etc/hosts
192.168.3.110    rabbitmq-test1    rabbitmq-test1.com
192.168.3.120    rabbitmq-test2    rabbitmq-test2.com

3.安装rabbitmq所依赖环境

[root@rabbitmq-test1 ~]# ntpdate pool.ntp.org
30 Jan 16:54:07 ntpdate[1592]: step time server 202.112.29.82 offset 1894159.715134 sec
[root@rabbitmq-test1 ~]# rpm  -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
Retrieving http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.qlPeIE: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[root@rabbitmq-test1 ~]# yum -y install gcc gcc-c++ ncurses-devel

4.Rabbitmq基于erlang语言开发,需要安装erlang虚拟机

[root@rabbitmq-test1 ~]# wget http://www.erlang.org/download/otp_src_R16B01.tar.gz
[root@rabbitmq-test1 ~]# tar zxvf otp_src_R16B01.tar.gz 
[root@rabbitmq-test1 ~]# cd otp_src_R16B01
[root@rabbitmq-test1 otp_src_R16B01]#./configure && make && make install

5.安装rabbitmq源码包,该源码包已经编译好,可直接使用

[root@rabbitmq-test1 ~]# wget 
[root@rabbitmq-test1 src]# tar zxvf rabbitmq-server-generic-unix-3.4.3.tar.gz 
[root@rabbitmq-test1 src]# ln -svf /usr/local/src/rabbitmq_server-3.4.3 /usr/local/rabbitmq-node1
[root@rabbitmq-test1 ~]# vim ~/.bash_profile 
export PATH=$PATH:$HOME/bin:/usr/local/rabbitmq-node1/sbin/:$PATH
[root@rabbitmq-test1 ~]# source ~/.bash_profile
[root@rabbitmq-test1 ~]# rabbitmq-plugins enable rabbitmq_management    #开启插件
[root@rabbitmq-test1 ~]# rabbitmq-plugins list
 Configured: E = explicitly enabled; e = implicitly enabled
 | Status:   [failed to contact rabbit@rabbitmq-test1 - status not shown]
 |/
[e ] amqp_client                       3.4.3
[  ] cowboy                            0.5.0-rmq3.4.3-git4b93c2d
[e ] mochiweb                          2.7.0-rmq3.4.3-git680dba8
[  ] rabbitmq_amqp1_0                  3.4.3
[  ] rabbitmq_auth_backend_ldap        3.4.3
[  ] rabbitmq_auth_mechanism_ssl       3.4.3
[  ] rabbitmq_consistent_hash_exchange 3.4.3
[  ] rabbitmq_federation               3.4.3
......
重启rabbitmq使得插件生效
[root@rabbitmq-test1 ~]# rabbitmqctl stop
[root@rabbitmq-test1 ~]# rabbitmq-server -detached
6.配置集群
[root@rabbitmq-test1 ~]# scp -P 8022 .erlang.cookie [email protected]:/root/
[root@rabbitmq-test2 ~]# rabbitmqctl status    #当把.erlang文件传过去后,可能会报错,所以需要重启
Status of node 'rabbit@rabbitmq-test2' ...
Error: unable to connect to node 'rabbit@rabbitmq-test2': nodedown

DIAGNOSTICS
===========

attempted to contact: ['rabbit@rabbitmq-test2']

rabbit@rabbitmq-test2:
  * connected to epmd (port 4369) on rabbitmq-test2
  * epmd reports node 'rabbit' running on port 25672
  * TCP connection succeeded but Erlang distribution failed
  * suggestion: hostname mismatch?
  * suggestion: is the cookie set correctly?

current node details:
- node name: 'rabbitmqctl-54368@rabbitmq-test2'
- home dir: /root
- cookie hash: adFjoR3CB/7hub0ns+opnw==
[root@rabbitmq-test2 ~]# ps -ef | grep rabbitmq | grep -v "grep"
[root@rabbitmq-test2 ~]# kill -9 54239 
[root@rabbitmq-test1 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-test2
Clustering node 'rabbit@rabbitmq-test1' with 'rabbitmq-test2' ...
Error: mnesia_unexpectedly_running
[root@rabbitmq-test1 ~]# rabbitmqctl stop_app
Stopping node 'rabbit@rabbitmq-test1' ...
[root@rabbitmq-test1 ~]# rabbitmqctl join_cluster rabbit@rabbitmq-test2
Clustering node 'rabbit@rabbitmq-test1' with 'rabbit@rabbitmq-test2' ...
[root@rabbitmq-test1 ~]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@rabbitmq-test1' ...
[{nodes,[{disc,['rabbit@rabbitmq-test1','rabbit@rabbitmq-test2']}]}]
#将node1节点转换成ram节点
[root@rabbitmq-test1 ~]# rabbitmqctl stop_app
Stopping node 'rabbit@rabbitmq-test1' ...
[root@rabbitmq-test1 ~]# rabbitmqctl change_cluster_node_type ram
Turning 'rabbit@rabbitmq-test1' into a ram node ...
[root@rabbitmq-test1 ~]# rabbitmqctl start_app
Starting node 'rabbit@rabbitmq-test1' ...
[root@rabbitmq-test1 ~]# rabbitmqctl cluster_status
Cluster status of node 'rabbit@rabbitmq-test1' ...
[{nodes,[{disc,['rabbit@rabbitmq-test2']},{ram,['rabbit@rabbitmq-test1']}]},
 {running_nodes,['rabbit@rabbitmq-test2','rabbit@rabbitmq-test1']},
 {cluster_name,<<"rabbit@rabbitmq-test1">>},
 {partitions,[]}]

7.修改默认密码

root@rabbitmq-test1 ~]# rabbitmqctl change_password guest kaibin
Changing password for user "guest" ...

8.登陆网页管理界面

http://localhost:15672

RabbitMQ群集搭建(源码)_第1张图片

9.处于安全的考虑,guest这个默认的用户只能通过localhost来登录,其他的IP无法直接使用这个账号

为了解决这个问题,需要在rabbitmq的配置文件中将loopback_users配置设置为空,如编写配置文件:/etc/rabbitmq/rabbitmq.config,并在其中添加以下内容:

[root@rabbitmq-test1 ~]# mkdir /etc/rabbitmq
[root@rabbitmq-test1 ~]# vim /etc/rabbitmq/rabbitmq.conf 
[{rabbit, [{loopback_users, []}]}].

方案二:

[root@rabbitmq-node2 ~]# cat /usr/local/rabbitmq-node2/ebin/rabbit.app | grep guest --color
          {default_user,<<"guest">>},
          {default_pass,<<"guest">>},
          {loopback_users,[<<"guest">>]},
将loopback_users本地用户删掉
[root@rabbitmq-node2 ~]# cat /usr/local/rabbitmq-node2/ebin/rabbit.app | grep loopback --color
          {loopback_users,[<<"">>]},

RabbitMQ群集搭建(源码)_第2张图片