系统: ubuntu19.10
节点:node1,node2,node3
sudo vim /etc/hostname
//将主机名分别改为node1,node2,node3
sudo vim /etc/hosts
//对两主机名和ip进行映射
https://blog.csdn.net/weixin_43562234/article/details/102792580
.erlang.cookie
文件内容保持一致sudo chmod 777 ./.erlang.cookie
sudo cat ./.erlang.cookie
将node1的.erlang.cookie
文件内容复制到node2和node3
将.erlang.cookie
文件权限改为只读
sudo chmod 400 ./.erlang.cookie
sudo rabbitmqctl stop
sudo rabbitmq-server -detached
//查看集群状态
sudo rabbitmqctl cluster_status
//此时都以单个节点的形式运行,{cluster_name,<<"rabbit@node1">>},这是集群名字,其他节点可以 join 到这个集群中。所有节点都是平等的,可以加入到任意一个集群中,最终这些节点都会在同一个集群中.
sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster --ram rabbit@node1
//如果想使用disk节点,命令为sudo rabbitmqctl join_cluster rabbit@node1
sudo rabbitmqctl start_app
sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster --ram rabbit@node1
sudo rabbitmqctl start_app
sudo rabbitmqctl set_policy ha-all "^hello" '{"ha-mode":"all"}'
参数意思为:
ha-all:为策略名称。
:为匹配符,只有一个代表匹配所有,^hello为匹配名称为hello的exchanges或者queue。
ha-mode:为匹配类型,分为3种模式:all-所有(所有的queue),exctly-部分(需配置ha-params参数,此参数为int类型比如3,众多集群中的随机3台机器),nodes-指定(需配置ha-params参数,此参数为数组类型比如[“3rabbit@F”,“rabbit@G”]这样指定为F与G这2台机器)。
Clustering node rabbit@adder-ThinkPad-T430s with rabbit@mq1
Error: unable to perform an operation on node 'rabbit@mq1'. Please see diagnostics information and suggestions below.
Most common reasons for this are:
* Target node is unreachable (e.g. due to hostname resolution, TCP connection or firewall issues) //tcp连接正常,防火墙关闭
* CLI tool fails to authenticate with the server (e.g. due to CLI tool's Erlang cookie not matching that of the server) //erlang cookie匹配
* Target node is not running //节点运行?
In addition to the diagnostics info below:
* See the CLI, clustering and networking guides on https://rabbitmq.com/documentation.html to learn more
* Consult server logs on node rabbit@mq1
* If target node is configured to use long node names, don't forget to use --longnames with CLI tools
DIAGNOSTICS
===========
attempted to contact: [rabbit@mq1]
rabbit@mq1:
* connected to epmd (port 4369) on mq1
* epmd reports node 'rabbit' uses port 25672 for inter-node and CLI tool traffic
* TCP connection succeeded but Erlang distribution failed
* Node name (or hostname) mismatch: node "rabbit@adder-virtual-machine" believes its node name is not "rabbit@adder-virtual-machine" but something else.
All nodes and CLI tools must refer to node "rabbit@adder-virtual-machine" using the same name the node itself uses (see its logs to find out what it is)
Current node details:
* node name: 'rabbitmqcli-7292-rabbit@adder-ThinkPad-T430s'
* effective user's home directory: /var/lib/rabbitmq
* Erlang cookie hash: tAXkImMRIGHzycRYvcMf2w==
错误排除:
1. 查看防火墙状态-->rabbitmqctl start_app-->防火墙已关闭
2. 查看tcp连接-->TCP connection succeeded but Erlang distribution failed -->tcp连接正常
3. 查看目标节点是否运行-->rabbitmqctl cluster_status-->目标节点运行中
4. Node name (or hostname) mismatch: node "rabbit@adder-virtual-machine" believes its node name is not "rabbit@adder-virtual-machine" but something else. -->节点名和hosts文件中的命名不一致
解决办法:
将hostname改至和hosts文件中的名字保持一致
结果:
将节点加入rabbitmq集群中成功
sudo rm -rf /var/lib/rabbitmq/mnesia --->该文件夹保存着集群信息;
sudo rabbitmq-service stop --->若未执行第一步,会提示异常;
{"init terminating in do_boot",{error,{inconsistent_cluster,"Node rabbit@rabbit2 thinks it's clustered with node rabbit@rabbit3, but rabbit@rabbit3 disagrees"}}}
init terminating in do_boot ({error,{inconsistent_cluster,Node rabbit@rabbit2 thinks it's clustered with node rabbit@rabbit3, but rabbit@rabbit3 disagrees}})
sudo rabbitmqctl forget_cluster_node rabbit@node2 --->在节点(node1)中摘除该节点;
重启RabbitMQ,并加入集群
sudo rabbitmq-server -detached --->启动RabbitMQ节点,以分离模式运行
sudo rabbitmqctl start_app --->启动RabbitMQ应用,而不是节点
sudo rabbitmqctl stop_app --->停止RabbitMQ应用
sudo rabbitmqctl status --->查看节点状态
sudo rabbitmqctl add_user mq 123456
sudo rabbitmqctl set_user_tags mq administrator --->新增账户mq 密码123456 用户所在的组administrator
sudo rabbitmqctl set_permissions -p "/" mq ".*" ".*" ".*" --->使admin用户对虚拟主机“/” 具有所有权限
sudo rabbitmq-plugins enable rabbitmq_management --->启用RabbitMQ_Management
sudo rabbitmqctl cluster_status --->集群状态
sudo rabbitmqctl forget_cluster_node rabbit@node2 --->从核心节点摘除node2
sudo rabbitmqctl reset application --->重置
//rabbitmq服务
//启动
sudo service rabbitmq-server start
//停止
sudo service rabbitmq-server stop
//重启
sudo service rabbitmq-server restart
//修改节点类型
rabbitmqctl stop_app
rabbitmqctl change_cluster_node_type disc(由ram节点转为disk节点)
adder@node3:/var/lib/rabbitmq$ sudo rabbitmqctl stop_app
Stopping rabbit application on node rabbit@node3 ...
adder@node3:/var/lib/rabbitmq$ sudo rabbitmqctl reset
Resetting node rabbit@node3 ...
adder@node3:/var/lib/rabbitmq$ sudo rabbitmqctl start_app
Starting node rabbit@node3 ...
completed with 3 plugins.