RabbitMQ群集搭建(Epel源)

  1. 实验环境

192.168.3.110   rabbitmq-node1  
192.168.3.120   rabbitmq-node2

2、配置前同步时间

[root@rabbitmq-test2 ~]# ntpdate pool.ntp.org
22 Jan 20:47:47 ntpdate[2358]: adjust time server 202.112.29.82 offset 0.000434 sec
[root@rabbitmq-test1 ~]# ntpdate pool.ntp.org
22 Jan 20:47:47 ntpdate[2358]: adjust time server 202.112.29.82 offset 0.000434 sec

3、注意一定要修改hosts

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

4.安装Epel源

[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.Xax2w8: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]

5.更新yum库

[root@rabbitmq-test1 ~]# yum repolist
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirrors.hustunique.com
 * epel: mirrors.zju.edu.cn
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
epel/primary_db                                                                                                | 6.4 MB     00:49     
extras                                                                                                         | 3.4 kB     00:00     
updates                                                                                                        | 3.4 kB     00:00     
updates/primary_db                                                                                             | 1.5 MB     00:00     
repo id                                     repo name                                                                           status
base                                        CentOS-6 - Base                                                                      6,518
epel                                        Extra Packages for Enterprise Linux 6 - x86_64                                      11,189
extras                                      CentOS-6 - Extras                                                                       36
updates                                     CentOS-6 - Updates                                                                     587
repolist: 18,330

6.下载并安装rabbitmq,并查看状态

方案一:

安装erlang

[root@rabbitmq-test1 ~]# yum -y install erlang
[root@rabbitmq-test1 ~]# wget http://www.rabbitmq.com/releases/rabbitmq-server/v2.8.6/rabbitmq-server-2.8.6-1.noarch.rpm
[root@rabbitmq-test1 ~]# rpm -ivh rabbitmq-server-2.8.6-1.noarch.rpm 
warning: rabbitmq-server-2.8.6-1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID 056e8e56: NOKEY
Preparing...                ########################################### [100%]
   1:rabbitmq-server        ########################################### [100%

方案二:

[root@rabbitmq-test1 ~]# yum -y install rabbitmq-server
[root@rabbitmq-test1 ~]# /etc/init.d/rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.
[root@rabbitmq-test1 ~]# ps -ef | grep rabbitmq
rabbitmq  2606     1  0 21:33 ?        00:00:00 /usr/lib64/erlang/erts-5.8.5/bin/epmd -daemon
root      2614     1  0 21:34 pts/0    00:00:00 runuser rabbitmq --session-command /usr/sbin/rabbitmq-server
rabbitmq  2637  2614  0 21:34 pts/0    00:00:00 /bin/sh /usr/sbin/rabbitmq-server
rabbitmq  2643  2637  1 21:34 pts/0    00:00:00 /usr/lib64/erlang/erts-5.8.5/bin/beam -W w -K true -A30 -P 1048576 -- -root /usr/lib64/erlang -progname erl -- -home /var/lib/rabbitmq -- -noshell -noinput -sname rabbit@kaibin -boot /var/lib/rabbitmq/mnesia/rabbit@kaibin-plugins-expand/rabbit -kernel inet_default_connect_options [{nodelay,true}] -sasl errlog_type error -sasl sasl_error_logger false -rabbit error_logger {file,"/var/log/rabbitmq/[email protected]"} -rabbit sasl_error_logger {file,"/var/log/rabbitmq/[email protected]"} -os_mon start_cpu_sup false -os_mon start_disksup false -os_mon start_memsup false -mnesia dir "/var/lib/rabbitmq/mnesia/rabbit@kaibin"
rabbitmq  2710  2643  0 21:34 ?        00:00:00 inet_gethost 4
rabbitmq  2711  2710  0 21:34 ?        00:00:00 inet_gethost 4
root      2724  2346  0 21:34 pts/0    00:00:00 grep rabbitmq
[root@rabbitmq-test1 ~]# rabbitmqctl status

7.安装插件管理界面

[root@rabbitmq-test1 ~]# cd /usr/lib/rabbitmq/bin/
[root@rabbitmq-test1 bin]# ./rabbitmq-plugins list
[ ] amqp_client                       3.1.5
[ ] cowboy                            0.5.0-rmq3.1.5-git4b93c2d
[ ] eldap                             3.1.5-gite309de4
[ ] mochiweb                          2.7.0-rmq3.1.5-git680dba8
[ ] rabbitmq_amqp1_0                  3.1.5
[ ] rabbitmq_auth_backend_ldap        3.1.5
[ ] rabbitmq_auth_mechanism_ssl       3.1.5
[ ] rabbitmq_consistent_hash_exchange 3.1.5
[ ] rabbitmq_federation               3.1.5
[ ] rabbitmq_federation_management    3.1.5
[ ] rabbitmq_jsonrpc                  3.1.5
[ ] rabbitmq_jsonrpc_channel          3.1.5
[ ] rabbitmq_jsonrpc_channel_examples 3.1.5
[ ] rabbitmq_management               3.1.5
[ ] rabbitmq_management_agent         3.1.5
[ ] rabbitmq_management_visualiser    3.1.5
[ ] rabbitmq_mqtt                     3.1.5
[ ] rabbitmq_shovel                   3.1.5
[ ] rabbitmq_shovel_management        3.1.5
......省略
[root@rabbitmq-test1 bin]# ./rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
  mochiweb
  webmachine
  rabbitmq_web_dispatch
  amqp_client
  rabbitmq_management_agent
  rabbitmq_management
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.
[root@rabbitmq-test1 bin]# /etc/init.d/rabbitmq-server restart
Restarting rabbitmq-server: RabbitMQ is not running
SUCCESS
rabbitmq-server.

查看管理端口是否启动:

[root@rabbitmq-test1 ~] lsof -i:55672
COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
beam    5574 rabbitmq   17r  IPv4  30162      0t0  TCP *:55672 (LISTEN)
浏览器打开http://IP:55672 账号密码都是guest
注意:rabbitmq从3.3.0开始禁止使用guest/guest权限通过除localhost外的访问。
如果想使用guest/guest通过远程机器访问,需要在rabbitmq配置文件中(/etc/rabbitmq/rabbitmq.config)中设置loopback_users为[]。
/etc/rabbitmq/rabbitmq.config文件完整内容如下(注意后面的半角句号):
[{rabbit, [{loopback_users, []}]}].

8.rabbitmq集群配置

(1)设置每个节点Cookie

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

[root@rabbitmq-test1 bin]# cd /var/lib/rabbitmq/
[root@rabbitmq-test1 rabbitmq]# scp -P 8022 .erlang.cookie [email protected]:/var/lib/rabbitmq/
[root@rabbitmq-test1 ~]# scp -P 8022 .erlang.cookie [email protected]:/root/

注:拷过去rabbitmq-test2节点重启后出现问题

[root@rabbitmq-test2 ~]# /etc/init.d/rabbitmq-server status
Status of node rabbit@rabbitmq-test2 ...  
Error: unable to connect to node rabbit@rabbitmq-test2: nodedown  
  
DIAGNOSTICS  
===========  
  
nodes in question: [rabbitmq-test2]  
  
hosts, their running nodes and ports:  
- unable to connect to epmd on rabbitmq-test2: badarg (unknown POSIX error)  
  
current node details:  
- node name: rabbitmqctl523@rabbitmq-test2 
- home dir: /var/lib/rabbitmq  
- cookie hash: pl2sZ2mw8yI/WrVgPZE+pw==  
#原因:因为修改了主机的hostname
解决方法:
1.重启系统
2.执行命令:
# /sbin/service rabbitmq-server stop
# /sbin/service rabbitmq-server start
# rabbitmqctl status 测试正确

(3)关于节点类型(ram |disk)及加入集群

ram节点的状态保存在内存中,disk节点保存在磁盘中被加入的节点为disk,如本例中rabbit@rabbitmq-test1为ram节点,rabbit@rabbitmq-test2为disk节点

[root@rabbitmq-test1 rabbitmq]# rabbitmqctl join_cluster rabbit@rabbitmq-test2
Clustering node 'rabbit@rabbitmq-test1' with 'rabbit@rabbitmq-test2' ...
Error: mnesia_unexpectedly_running
#提示需要本机节点正在运行,无法加入集群
[root@rabbitmq-test1 rabbitmq]# rabbitmqctl stop_app
Stopping node 'rabbit@rabbitmq-test1' ...
...done.
[root@rabbitmq-test1 rabbitmq]# rabbitmqctl join_cluster rabbit@rabbitmq-test2
Clustering node 'rabbit@rabbitmq-test1' with 'rabbit@rabbitmq-test2' ...
...done.        #已加入集群
[root@rabbitmq-test1 rabbitmq]# rabbitmqctl start_app
Starting node 'rabbit@rabbitmq-test1' ...
...done.
[root@rabbitmq-test1 rabbitmq]# rabbitmqctl cluster_status    #查看集群状态
Cluster status of node 'rabbit@rabbitmq-test1' ...
[{nodes,[{disc,['rabbit@rabbitmq-test1','rabbit@rabbitmq-test2']}]},    #两个disk节点
 {running_nodes,['rabbit@rabbitmq-test2','rabbit@rabbitmq-test1']},     #运行着两个节点
 {partitions,[]}]
...done.

改变test-node1的节点属性(改为ram几点)

[root@rabbitmq-test1 rabbitmq]# rabbitmqctl stop_app
Stopping node 'rabbit@rabbitmq-test1' ...
...done.
[root@rabbitmq-test1 rabbitmq]# rabbitmqctl change_cluster_node_type  ram
Turning 'rabbit@rabbitmq-test1' into a ram node ...
...done.
[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']},
 {partitions,[]}]
...done.

9.登陆web页面查看集群状态

http://192.168.3.110:15672

wKiom1TLNdGigSY2AAFqWtRUKDw772.jpg

10.关于rabbitmq的小知识:

#rabbitmq的web管理界面默认guest:guest,这样的话,不是很安全,需要更改下

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

#查看所有队列信息

[root@rabbitmq-test1 ~]# rabbitmqctl list_queues
Listing queues ...
...done.

#关闭应用

 rabbitmqctl stop_app

#启动应用,和上述关闭命令配合使用,达到清空队列的目的

rabbitmqctl start_app

#清除所有队列

rabbitmqctl reset

#关于端口

rabbimq  55672 15672 为监控插件端口

rabbitmq  5672为数据端口

[root@rabbitmq-test1 ~]# lsof -i:15672
COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
beam    1875 rabbitmq   20u  IPv4  24559      0t0  TCP *:15672 (LISTEN)
[root@rabbitmq-test1 ~]# lsof -i:55672
COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
beam    1875 rabbitmq   18u  IPv4  24555      0t0  TCP *:55672 (LISTEN)
[root@rabbitmq-test1 ~]# lsof -i:5672
COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
beam    1875 rabbitmq   15u  IPv6  24550      0t0  TCP *:amqp (LISTEN)


你可能感兴趣的:(rabbitmq)