一、单节点配置
1、配置主机名
修改hosts文件,将集群所有机器 ip hostname 分别添加到每一台机器中
192.168.198.128 mq128
192.168.198.129 mq129
2、下载和安装
wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
wget http://packages.erlang-solutions.com/site/esl/esl-erlang/FLAVOUR_1_general/esl-erlang_19.1~centos~6_amd64.rpm
[root@mq128 ~]#yum localinstall esl-erlang_19.1~centos~6_amd64.rpm
[root@mq128 ~]# yum install socat
[root@mq128 ~]# rpm -ivh --nodeps rabbitmq-server-3.6.6-1.el6.noarch.rpm
warning: rabbitmq-server-3.6.6-1.el6.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY
Preparing... ########################################### [100%]
1:rabbitmq-server ########################################### [100%]
[root@mq128 ~]# service rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.
注意:rabbitmq安装后默认没有配置文件,启动时会使用默认参数
3、创建配置文件
cp /usr/share/doc/rabbitmq-server-3.6.6/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config #复制配置模板
[root@mq128 ~]# vim /etc/rabbitmq/rabbitmq.config #修改配置文件
8: {rabbit,
17: {tcp_listeners, [5672]},
64: {loopback_users, []} #注意这里的逗号要去掉,否则启动会报错。
317: ]},
324: {kernel,
330: ]},
338: {rabbitmq_management,
358: {listener,[{port, 12345}] } #web管理端口
372: ]},
380: {rabbitmq_shovel,
381: [{shovels,
440: ]},
448: {rabbitmq_stomp,
487: ]},
496: {rabbitmq_mqtt,
545: ]},
554: {rabbitmq_amqp1_0,
566: ]},
575: {rabbitmq_auth_backend_ldap,
[root@mq128 ~]# service rabbitmq-server restart
Starting rabbitmq-server: SUCCESS
rabbitmq-server.
[root@mq128 ~]# service rabbitmq-server status
Status of node rabbit@mq128 ...
[{pid,5155},
{running_applications,[{rabbit,"RabbitMQ","3.6.6"},
{mnesia,"MNESIA CXC 138 12","4.14.1"},
{rabbit_common,[],"3.6.6"},
{xmerl,"XML parser","1.3.12"},
{os_mon,"CPO CXC 138 46","2.4.1"},
{ranch,"Socket acceptor pool for TCP protocols.",
"1.2.1"},
{sasl,"SASL CXC 138 11","3.0.1"},
{stdlib,"ERTS CXC 138 10","3.1"},
{kernel,"ERTS CXC 138 10","5.1"}]},
{os,{unix,linux}},
{erlang_version,"Erlang/OTP 19 [erts-8.1] [source] [64-bit] [async-threads:64] [hipe] [kernel-poll:true]\n"},
{memory,[{total,43335456},
{connection_readers,0},
{connection_writers,0},
{connection_channels,0},
{connection_other,0},
{queue_procs,2688},
{queue_slave_procs,0},
{plugins,0},
{other_proc,17741736},
{mnesia,58072},
{mgmt_db,0},
{msg_index,38536},
{other_ets,1003752},
{binary,20744},
{code,17822316},
{atom,752561},
{other_system,5895051}]},
{alarms,[]},
{listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]},
{vm_memory_high_watermark,0.4},
{vm_memory_limit,414546329},
{disk_free_limit,50000000},
{disk_free,10644054016},
{file_descriptors,[{total_limit,924},
{total_used,2},
{sockets_limit,829},
{sockets_used,0}]},
{processes,[{limit,1048576},{used,137}]},
{run_queue,0},
{uptime,54},
{kernel,{net_ticktime,60}}]
4、开启web管理界面
[root@mq128 ~]# 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@mq128... started 6 plugins.
[root@mq128 ~]# netstat -tnlp|grep beam
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 5155/beam
tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN 5155/beam
tcp 0 0 :::5672 :::* LISTEN 5155/beam
开启之后可以用浏览器访问管理界面:http://ip:12345
默认用户名密码 guest:guest
如果提示不能登录就需要新建账户,见下文添加用户操作。
5、添加账户(只需要在一台机器上设置,加入集群后会同步到所有机器)
rabbitmqctl add_user username password
添加账户标签(administrator)
rabbitmqctl set_user_tags username administrator
标签:
management
用户可以访问管理插件。
policymaker
用户可以访问管理插件,可以管理可访问的vhost的策略和参数。
monitoring
用户可以访问管理插件,可以查看所有connections、channels、node-related信息。
administrator
用户拥有monitoring用户的所有权限,可以管理用户,vhost和权限,关闭他用户的连接,并管理所有vhost策略和参数。
(除此之外可以设置任何标签,以上几个只是特殊标签)
赋予权限 set_permissions [-p vhost] {user} {conf} {write} {read}
[root@mq128 ~]# rabbitmqctl add_user admin admin123
Creating user "admin" ...
[root@mq128 ~]# rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...
[root@mq128 ~]# rabbitmqctl list_users
Listing users ...
admin [administrator]
guest [administrator]
6、创建虚拟机并授权
[root@mq128 ~]# rabbitmqctl add_vhost share #创建虚拟机
Creating vhost "share" ...
[root@mq128 ~]# rabbitmqctl set_permissions -p /share admin ".*" ".*" ".*" #这里授权失败是因为/share这个虚拟机没有被创建
Setting permissions for user "admin" in vhost "/share" ...
Error: no_such_vhost: /share
[root@mq128 ~]# rabbitmqctl delete_vhost share #删除虚拟机
Deleting vhost "share" ...
[root@mq128 ~]# rabbitmqctl add_vhost /share
Creating vhost "/share" ...
[root@mq128 ~]# rabbitmqctl set_permissions -p /share admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/share" ...
[root@mq128 ~]# rabbitmqctl list_vhosts
Listing vhosts ...
/
/share
7、安装mq129的配置
这里只是安装软件包和修改配置文件,用户和授权等步骤忽略,后面集群会同步
[root@mq129 ~]#yum localinstall esl-erlang_19.1~centos~6_amd64.rpm
[root@mq129 ~]# yum install socat
[root@mq129 ~]# rpm -ivh --nodeps rabbitmq-server-3.6.6-1.el6.noarch.rpm
warning: rabbitmq-server-3.6.6-1.el6.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY
Preparing... ########################################### [100%]
1:rabbitmq-server ########################################### [100%]
[root@mq129 ~]# scp -r mq128:/etc/rabbitmq/rabbitmq.config /etc/rabbitmq/
[root@mq129 ~]# cat /etc/rabbitmq/rabbitmq.config |grep -n -v %|grep ,
8: {rabbit,
17: {tcp_listeners, [5672]},
64: {loopback_users, []}
317: ]},
324: {kernel,
330: ]},
338: {rabbitmq_management,
358: {listener,[{port, 12345}] }
372: ]},
380: {rabbitmq_shovel,
381: [{shovels,
440: ]},
448: {rabbitmq_stomp,
487: ]},
496: {rabbitmq_mqtt,
545: ]},
554: {rabbitmq_amqp1_0,
566: ]},
575: {rabbitmq_auth_backend_ldap,
[root@mq129 ~]# service rabbitmq-server restart
Restarting rabbitmq-server: RabbitMQ is not running
SUCCESS
rabbitmq-server.
[root@mq129 ~]# service rabbitmq-server restart
Restarting rabbitmq-server: SUCCESS
rabbitmq-server.
[root@mq129 ~]# netstat -tnlp|grep beam
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 6435/beam
tcp 0 0 :::5672 :::* LISTEN 6435/beam
[root@mq129 ~]# 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@mq129... started 6 plugins.
[root@mq129 ~]# netstat -tnlp|grep beam
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 6435/beam
tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN 6435/beam
tcp 0 0 :::5672 :::* LISTEN 6435/beam
二、一般集群配置
RabbitMQ集群中所有节点都是平等的,没有绝对的主次之分,每一个节点的配置信息都会在集群中进行同步。
只有节点类型区别,节点类型有disk型和RAM型。分别对应磁盘和内存,disk节点可以持久化消息安全性较高、
RAM节点存取速度快,但是不能持久化。一个集群中至少要有一个disk类型的节点,以便在所有节点宕机重启
时将持久化队列中的消息进行持久化。
1、同步 Erlang Cookie
Erlang Cookie 文件:/var/lib/rabbitmq/.erlang.cookie
将任意一台的cookie文件或者内容替换到集群其他机器。最后确保文件权限为400。
查看某台机器的cookie
[root@mq128 ~]# cat /var/lib/rabbitmq/.erlang.cookie
VTJSYEJPOOMPHNKCTAGF
关闭rabbitmq,将cookie复制到其他机器…
[root@mq129 ~]# rabbitmqctl stop
Stopping and halting node rabbit@mq129 ...
[root@mq129 ~]# echo VTJSYEJPOOMPHNKCTAGF > /var/lib/rabbitmq/.erlang.cookie
[root@mq129 ~]# cat /var/lib/rabbitmq/.erlang.cookie
VTJSYEJPOOMPHNKCTAGF
[root@mq128 ~]# rabbitmq-server -detached
Warning: PID file not written; -detached was passed. #该命令方式启动可能会报错,kill掉进程,然后通过service方式启动
ERROR: node with name "rabbit" already running on "mq128"
[root@mq128 ~]# rabbitmqctl stop
Stopping and halting node rabbit@mq128 ...
[root@mq128 ~]# ps aux | grep rabbitmq
rabbitmq 2096 0.0 0.0 10832 448 ? S 09:13 0:00 /usr/lib64/erlang/erts-5.8.5/bin/epmd -daemon
root 16062 0.0 0.0 103332 860 pts/0 S+ 10:59 0:00 grep rabbitmq
[root@mq128 ~]# kill 2096
[root@mq128 ~]# ps aux | grep rabbitmq
root 16084 0.0 0.0 103328 856 pts/0 S+ 10:59 0:00 grep rabbitmq
2、将mq129加入到集群
[root@mq129 ~]# service rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.
[root@mq129 ~]# rabbitmqctl stop_app
Stopping node rabbit@mq129 ...
[root@mq129 ~]# rabbitmqctl reset
Resetting node rabbit@mq129 ...
(rabbitmq集群节点有disc 和ram两种类型,一个集群中至少要有一个disc类型的节点,不指定默认加入为disc)
vm_memory_high_watermark:表示RabbitMQ使用内存的上限为系统内存的40%。也可以通过absolute参数制定具体可用的内存数。
当RabbitMQ使用内存超过这个限制时,RabbitMQ 将对消息的发布者进行限流,直到内存占用回到正常值以内。
Vm_memory_high_watermark_paging_ratio:表示当RabbitMQ达到0.4*0.75=30%,系统将对queue中的内容启用paging机制,将message等内容换页到disk 中。
[root@mq129 ~]# rabbitmqctl join_cluster rabbit@mq128 --ram
Clustering node rabbit@mq129 with rabbit@mq128 ...
[root@mq129 ~]# rabbitmqctl start_app
Starting node rabbit@mq129 ...
[root@mq129 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@mq129 ...
[{nodes,[{disc,[rabbit@mq128]},{ram,[rabbit@mq129]}]},
{running_nodes,[rabbit@mq128,rabbit@mq129]},
{cluster_name,<<"rabbit@mq128">>},
{partitions,[]},
{alarms,[{rabbit@mq128,[]},{rabbit@mq129,[]}]}]
3、查看集群状态
[root@mq128 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@mq128 ...
[{nodes,[{disc,[rabbit@mq128]},{ram,[rabbit@mq129]}]},
{running_nodes,[rabbit@mq129,rabbit@mq128]},
{cluster_name,<<"rabbit@mq128">>},
{partitions,[]},
{alarms,[{rabbit@mq129,[]},{rabbit@mq128,[]}]}]
这里配置的是一般模式的mq集群,配置往后128的配置会同步到129的,此时129上面已经有128上面创建的用户和虚拟机,
当然,现在在129上面更改的配置也会同步到128上面。
4、测试
生产消息测试
# !/usr/bin/env python
#coding=utf-8
import pika
credentials = pika.PlainCredentials('admin','admin123')
connection = pika.BlockingConnection(pika.ConnectionParameters(
'192.168.198.128',5672,'/share',credentials))
channel = connection.channel()
# 声明queue
channel.queue_declare(queue='balance')
# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
routing_key='balance',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
[root@mq128 ~]# python mq.py
Traceback (most recent call last):
File "mq.py", line 3, in <module>
import pika
ImportError: No module named pika
执行报错时因为相关模块没有安装
wget https://pypi.python.org/packages/source/p/pika/pika-0.9.9.tar.gz#md5=5737fe84209a4df81fe4cff4052793da
tar -zxvf pika-0.9.9.tar.gz
[root@mq128 ~]# cd pika-0.9.9/
[root@mq128 pika-0.9.9]# python setup.py install
[root@mq128 ~]# python mq.py #生产信息成功
[x] Sent 'Hello World!'
[root@mq128 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
balance 1 #消息生产成功
消费消息脚本:
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika
credentials = pika.PlainCredentials('admin','admin123')
connection = pika.BlockingConnection(pika.ConnectionParameters(
'192.168.198.128',5672,'/share',credentials))
channel = connection.channel()
# You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
channel.queue_declare(queue='balance')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(callback,
queue='balance',
no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
[root@mq128 ~]# python cost.py
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[x] Received 'Hello World!'
[*] Waiting for messages. To exit press CTRL+C
[root@mq128 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
balance 0 #消息已被消费
故障测试:
[root@mq129 ~]# python mq.py #推送消息到mq128
[x] Sent 'Hello World!'
[root@mq129 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
balance 1 #一个队列
[root@mq129 ~]# service rabbitmq-server stop #停止mq129
[root@mq128 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
balance 1 #mq128的队列不受影响
[root@mq129 ~]# service rabbitmq-server start
Starting rabbitmq-server: SUCCESS #恢复mq129
rabbitmq-server.
[root@mq129 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
balance 1
[root@mq128 ~]# service rabbitmq-server stop
Stopping rabbitmq-server: rabbitmq-server. #停止mq128
[root@mq129 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
#停止mq128后mq129也没有消息队列了
小结:一般模式的集群,消息的可靠性依赖于原来的消息节点,如果原节点失效,则消息丢失。
消息持久化测试:
[root@mq128 ~]# vim chijiu.py
# !/usr/bin/env python
#coding=utf-8
import pika
credentials = pika.PlainCredentials('admin','admin123')
connection = pika.BlockingConnection(pika.ConnectionParameters(
'192.168.198.128',5672,'/share',credentials))
channel = connection.channel()
# 声明queue
channel.queue_declare(queue='durable',durable=True)
# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
routing_key='durable',
body='Hello cheng!',
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
)
)
print(" [x] Sent 'Hello cheng!'")
[root@mq128 ~]# python chijiu.py
[x] Sent 'Hello cheng!'
[root@mq128 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
durable 1
[root@mq128 ~]# rabbitmqctl stop
Stopping and halting node rabbit@mq128 ...
[root@mq129 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
durable #队列名还在,消息没有了
[root@mq128 ~]# service rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.
[root@mq128 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
durable 1 #消息又恢复了
注意:如果持久化标志设置为true,则代表是一个持久的队列,那么在服务重启后,也会存在。
因为服务会把持久化的queue存放在硬盘上,当服务重启的时候,会重新申明之前被持久
化的queue。队列是可以被持久化,但是里面的消息是否为持久化那还要看消息的持久化
设置。也就是说,如果重启之前那个queue里面还有没有发出去的消息的话,重启之后那
队列里面是不是还存在原来的消息,这个就要取决于发送者在发送消息时对消息的设置了。
三、镜像同步集群
1、镜像同步概述
镜像队列是通过RB的配置策略(policy)来实现的,
镜像队列提供了三种模式:
Ø all:全部的节点队列都做镜像;
Ø exactly:指定镜像队列的节点最高镜像数量;
Ø nodes:只为指定具体节点配置镜像队列;
策略参数:
Ha mode 同步方式
Ha params 选择其它同步方式的参数 比如节点名
Ha async mode 同步方式,这个需要详细说明的,默认的情况下。
当节点断线后那么这个节点就已经是落后的版本,当我们再去启动
节点的时候数据我们需要去手动的同步,这自然是不好的,做到自
动化是最完美的,所以设置成automatic是最佳选择
2、配置策略
[root@mq129 ~]# rabbitmqctl set_policy -p /share ha-all "^" '{"ha-mode":"all"}'
-p:指定vhost
ha-all是自己命名的策略名
Setting policy "ha-all" for pattern "^" to "{\"ha-mode\":\"all\"}" with priority "0" ...
[root@mq129 ~]# rabbitmqctl list_policies -p /share
Listing policies ...
/share ha-all all ^ {"ha-mode":"all"} 0
注意:在cluster集群中任意节点开启策略,策略会自动同步到其它节点。
[root@mq128 rabbitmq]# rabbitmqctl list_policies -p /share
Listing policies ...
/share ha-all all ^ {"ha-mode":"all"} 0 #策略已经同步
[root@mq129 ~]# service rabbitmq-server restart #默认节点重启后,如果同步前已经有数据,需要手动同步数据
Restarting rabbitmq-server: SUCCESS
rabbitmq-server.
3、测试
[root@mq129 ~]# python mq.py #产生消息
[x] Sent 'Hello World!'
[root@mq129 ~]# python mq.py
[x] Sent 'Hello World!'
[root@mq129 ~]# python mq.py
[x] Sent 'Hello World!'
[root@mq129 ~]# python chijiu.py
[x] Sent 'Hello cheng!'
[root@mq129 ~]# python chijiu.py
[x] Sent 'Hello cheng!'
[root@mq129 ~]# vim chijiu.py
[root@mq129 ~]# python chijiu.py
[x] Sent 'Hello cheng!'
[root@mq129 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
balance 3
durable 3
[root@mq129 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
balance 3
durable 3
[root@mq129 ~]# rabbitmqctl list_queues -p /share
Listing queues ...
balance 3
durable 0
[root@mq128 ~]# service rabbitmq-server stop #停止mq128
Stopping rabbitmq-server: rabbitmq-server.
[root@mq128 ~]# python cost2.py #消费mq129的消息
[x] Received 'Hello cheng!'
[x] Received 'Hello cheng!'
[x] Received 'Hello cheng!'
[*] Waiting for messages. To exit press CTRL+C
注意:节点down机前还有没有消费的消息,重启后不会自动同步,需要手动同步才行。
[root@mq128 ~]# rabbitmqctl sync_queue -p /share balance #手动同步队列
Synchronising queue 'balance' in vhost '/share' ...
[root@mq128 ~]# rabbitmqctl list_policies -p /share
Listing policies ...
/share ha-all all ^ {"ha-mode":"all"} 0
[root@mq128 ~]# rabbitmqctl clear_policy -p /share ha-all
Clearing policy "ha-all" ...
[root@mq128 ~]# rabbitmqctl list_policies -p /share
Listing policies ...
4、自动同步策略
[root@mq128 ~]# rabbitmqctl set_policy -p /share 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" ...
[root@mq128 ~]# rabbitmqctl list_policies -p /share
Listing policies ...
/share ha-all all ^ {"ha-mode":"all","ha-sync-mode":"automatic"} 0
自动同步策略,节点down机后会自动同步数据,但同步完成前可能无法写入数据。
四、添加新的rabbitmq节点
1、修改hosts文件
192.168.198.128 mq128
192.168.198.129 mq129
192.168.198.130 mq130
注意:要把新增的hosts记录添加到原来的节点服务器中
2、安装rabbitmq
[root@localhost ~]# cd rabbitmq/
[root@localhost rabbitmq]# ls
esl-erlang_19.1~centos~6_amd64.rpm rabbitmq-server-3.6.6-1.el6.noarch.rpm
[root@localhost rabbitmq]# yum localinstall esl-erlang_19.1~centos~6_amd64.rpm
[root@localhost rabbitmq]# yum install socat -y
[root@localhost rabbitmq]# rpm -ivh --nodeps rabbitmq-server-3.6.6-1.el6.noarch.rpm
warning: rabbitmq-server-3.6.6-1.el6.noarch.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY
Preparing... ########################################### [100%]
1:rabbitmq-server ########################################### [100%]
[root@localhost rabbitmq]# scp -r mq128:/etc/rabbitmq/rabbitmq.config /etc/rabbitmq/ #复制源节点的配置文件
[root@localhost rabbitmq]# service rabbitmq-server start
[root@localhost rabbitmq]# netstat -tnlp|grep beam
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 2606/beam
tcp 0 0 :::5672 :::* LISTEN 2606/beam
[root@localhost rabbitmq]# rabbitmq-plugins enable rabbitmq_management #开启web管理界面
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@localhost... started 6 plugins.
[root@localhost rabbitmq]# netstat -tnlp|grep beam
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 2606/beam
tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN 2606/beam
tcp 0 0 :::5672 :::* LISTEN 2606/beam
3、添加到集群
注意:加入集群时要保证主机名已经生效,而且主机名不能随意更改,否则集群节点会失效。
[root@mq130 ~]# hostname
mq130
[root@mq130 ~]# rabbitmqctl stop
Stopping and halting node rabbit@localhost ...
[root@mq130 ~]# echo VTJSYEJPOOMPHNKCTAGF > /var/lib/rabbitmq/.erlang.cookie
[root@mq130 ~]# cat /var/lib/rabbitmq/.erlang.cookie
VTJSYEJPOOMPHNKCTAGF
[root@mq130 ~]# service rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.
[root@mq130 ~]# rabbitmqctl stop_app #关闭应用
Stopping node rabbit@mq130 ...
[root@mq130 ~]# rabbitmqctl reset #还原
Resetting node rabbit@mq130 ...
[root@mq130 ~]# rabbitmqctl join_cluster rabbit@mq128 #加入到rabbit@mq128集群中
Clustering node rabbit@mq130 with rabbit@mq128 ...
[root@mq130 ~]# rabbitmqctl start_app
Starting node rabbit@mq130 ...
[root@mq130 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@mq130 ...
[{nodes,[{disc,[rabbit@localhost,rabbit@mq128,rabbit@mq130]},
{ram,[rabbit@mq129]}]},
{running_nodes,[rabbit@mq128,rabbit@mq129,rabbit@mq130]},
{cluster_name,<<"rabbit@mq128">>},
{partitions,[]},
{alarms,[{rabbit@mq128,[]},{rabbit@mq129,[]},{rabbit@mq130,[]}]}]
#查看认证信息和队列信息是否同步过来了
[root@mq130 ~]# rabbitmqctl list_vhosts #查看虚拟主机
Listing vhosts ...
/
/share
[root@mq130 ~]# rabbitmqctl list_queues -p /share #查看队列
Listing queues ...
balance 1
durable 2
[root@mq128 ~]# tail -f /var/log/rabbitmq/ #查看主节点的日志
=INFO REPORT==== 15-Jul-2017::19:51:13 ===
node rabbit@mq130 up #新增节点
=INFO REPORT==== 15-Jul-2017::19:51:37 ===
rabbit on node rabbit@mq130 up
=INFO REPORT==== 15-Jul-2017::19:51:37 ===
Mirrored queue 'balance' in vhost '/share': Synchronising: 1 messages to synchronise #同步数据
=INFO REPORT==== 15-Jul-2017::19:51:37 ===
Mirrored queue 'balance' in vhost '/share': Synchronising: batch size: 4096
=INFO REPORT==== 15-Jul-2017::19:51:37 ===
Mirrored queue 'balance' in vhost '/share': Synchronising: mirrors [rabbit@mq130] to sync
=INFO REPORT==== 15-Jul-2017::19:51:37 ===
Mirrored queue 'balance' in vhost '/share': Synchronising: complete
=INFO REPORT==== 15-Jul-2017::19:51:37 ===
Mirrored queue 'durable' in vhost '/share': Synchronising: 2 messages to synchronise
=INFO REPORT==== 15-Jul-2017::19:51:37 ===
Mirrored queue 'durable' in vhost '/share': Synchronising: batch size: 4096
=INFO REPORT==== 15-Jul-2017::19:51:37 ===
Mirrored queue 'durable' in vhost '/share': Synchronising: mirrors [rabbit@mq130] to sync
=INFO REPORT==== 15-Jul-2017::19:51:37 ===
Mirrored queue 'durable' in vhost '/share': Synchronising: complete
五、直接删掉集群中的某个节点
[root@mq128 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@mq128 ...
[{nodes,[{disc,[rabbit@localhost,rabbit@mq128,rabbit@mq130]},
{ram,[rabbit@mq129]}]},
{running_nodes,[rabbit@mq129,rabbit@mq130,rabbit@mq128]},
{cluster_name,<<"rabbit@mq128">>},
{partitions,[]},
{alarms,[{rabbit@mq129,[]},{rabbit@mq130,[]},{rabbit@mq128,[]}]}]
#rabbit@localhost节点失效,需要删除。
[root@mq128 ~]# rabbitmqctl forget_cluster_node rabbit@localhost #由于节点已经生效,只能强制删除。
Removing node rabbit@localhost from cluster ...
Cluster status of node rabbit@mq128 ...
[{nodes,[{disc,[rabbit@mq128,rabbit@mq130]},{ram,[rabbit@mq129]}]},
{running_nodes,[rabbit@mq129,rabbit@mq130,rabbit@mq128]},
{cluster_name,<<"rabbit@mq128">>},
{partitions,[]},
{alarms,[{rabbit@mq129,[]},{rabbit@mq130,[]},{rabbit@mq128,[]}]}]
|