RabbitMQ高可用

RabbitMQ highly avilable

基础环境

系统: ubuntu19.10
节点:node1,node2,node3

  1. 修改服务hostname文件和hosts文件
sudo vim /etc/hostname
//将主机名分别改为node1,node2,node3

sudo vim /etc/hosts
//对两主机名和ip进行映射

安装rabbitmq,启动rabbitmq,开启web管理权限,添加用户

https://blog.csdn.net/weixin_43562234/article/details/102792580

搭建rabbitmq–>普通集群模式

  1. 使集群中三台服务器的.erlang.cookie文件内容保持一致
sudo chmod 777 ./.erlang.cookie
sudo cat ./.erlang.cookie

将node1的.erlang.cookie文件内容复制到node2和node3
.erlang.cookie文件权限改为只读

sudo chmod 400 ./.erlang.cookie  
  1. 停掉所有节点上的rabbitmq服务,然后使用-detached独立运行各个节点
sudo rabbitmqctl stop

sudo rabbitmq-server -detached

//查看集群状态
sudo rabbitmqctl cluster_status  

//此时都以单个节点的形式运行,{cluster_name,<<"rabbit@node1">>},这是集群名字,其他节点可以 join 到这个集群中。所有节点都是平等的,可以加入到任意一个集群中,最终这些节点都会在同一个集群中.
  1. 将node2加入到集群
sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster --ram rabbit@node1
//如果想使用disk节点,命令为sudo rabbitmqctl join_cluster rabbit@node1
sudo rabbitmqctl start_app
  1. 将node3加入到集群
sudo rabbitmqctl stop_app
sudo rabbitmqctl join_cluster --ram rabbit@node1
sudo rabbitmqctl start_app
  1. 打开web管理页面
    在这里插入图片描述
    图中节点名与文章中的节点名有出入,请以文章为主要参考。

配置集群高可用

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台机器)。

搭建过程中出现的错误

  1. 将node2加入集群失败,报错信息如下:
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集群中成功
  1. 在搭建集群过程中,若节点异常退出且无法再次加入集群
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.

参考文章:

  1. https://www.cnblogs.com/knowledgesea/p/6535766.html
  2. https://blog.csdn.net/ronaldo1994/article/details/85927168
  3. https://blog.csdn.net/jinyidong/article/details/80003362
©️2020 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值