Windows下RabbitMQ 集群及应用测试

 

转自:https://blog.csdn.net/elie_yang/article/details/86657851

1:安装erlang环境;

 

2:安装RabbitMQ, 以上2步都是下载安装,下一步结束;参考官网:http://www.rabbitmq.com/install-windows.html

 

步骤2完成后,可至windows服务列表中找到RabbitMQ服务;

 

3:删除默认guest 用户,添加管理员用户;

 

开启RabbitMQ命令行工具:rabbitmqctl delete_user guest;  注意是rabbitmqctl 中的结尾是ctl 不是ctrl

 

rabbitmqctl add_user janeway changeit

 

rabbitmqctl set_user_tags janeway administrator

 

rabbitmqctl set_permissions -p /vhost janeway “^janeway-.*” “.*” “.*”

 

注意上面是双引号,单引号会导致访问拒绝ACCESS_REFUSED

 

4: 如果使用阿里云服务器,开通tcp 5672端口;

 

5:重复上面步骤在另外一台服务器上安装rabbitMq实例;

 

6:分别在上述服务器中找到:C:\Windows\System32\drivers\etc 下的hosts文件,配置好两台服务器之间的互相访问:

 

例如:172.18.3.243 nodeServerA 保存后在cmd 中ping nodeServerA看是否生效;

 

7:复制.erlang.cookie文件到另外一台服务器,位置:C:\Users\Administrator,保持服务器之间的cookie一致;不一致 错误见下图

 

 

 

 

替换C:\Windows\system32\config\systemprofile 下的erlang.cookie文件,保持一致,重启服务;

 

8:在将要集群的主机中继续执行 rabbitmqctl stop_app;

 

9:rabbitmqctl join_cluster rabbit@HostNamexxx...(填写服务器名)

 

 

 

 

10: rabbitmqctl start_app;开启应用,非节点服务

 

11:rabbitmqctl cluster_status;查看集群状态,disc:为磁盘节点,ram:为内存节点;

 

 

 

 转自:https://blog.csdn.net/elie_yang/article/details/86657851

集群后,用户、队列什么的都同步过来了,验证下:rabbtimqctl list_users; rabbbitmqctl list_queues;

 

12:实验1:关闭disc类型的节点,用客户端直接连接ram节点:

 

当客户端程序QueueDeclare时报错:home node 'rabbit@HgwebHost' of durable queue 'xxx' in vhost '/' is down or inaccessible;

 

实验2:退出集群后在各自的RabbitMQ节点中创建同一个名称的非持久化队列会是什么情形呢?

 

首先退出集群:

 

在rabbitmq2上执行

 

#rabbitmqctl stop_app

 

#rabbitmqctl reset

 

#rabbitmqctl start_app

 

在集群主节点上执行

 

# rabbitmqctl forget_cluster_node rabbit@rabbitmq2

 

在这里碰见的一个异常为:rabbitmq thinks it's clustered with node rabbit@xx-nodeName, but rabbit@xx_node2 disagrees.

 

解决办法是搜索mnesia文件夹:找到例如:C:\Users\Administrator\AppData\Roaming\RabbitMQ\db\rabbit@iZmt2xpetxp62fZ-mnesia 然后删除cluster_nodes.config文件;

 

创建一个同名队列,重复步骤8,9  实验结果:join_cluster 后,刚才建立的队列不可见;

 

13:改变节点的集群属性:验证一个集群中至少有一个磁盘节点:

 

#rabbitmqctl stop_app

 

#rabbitmqctl change_cluster_node_type disc/ram –更改节点为磁盘或内存节点

 

#rabbitmqctl start_app

 

14:以上一个普通的集群就完成了,RabbitClient.Net 代码中:

 

var amqpList = new List<AmqpTcpEndpoint>

{

    new AmqpTcpEndpoint(new Uri("amqp://192.168.0.105:5672")),

    new AmqpTcpEndpoint(new Uri("amqp://192.168.0.107:5672"))

};

 

var factory = new ConnectionFactory()

                {          

                UserName = userName,

                Password = password,

                RequestedHeartbeat = heartbeat, //心跳超时时间

                TopologyRecoveryEnabled = true,

                AutomaticRecoveryEnabled = true //自动重连

            };

 

factory.CreateConnection(amqpList);

 

可以实现简单的客户端负载均衡;

 

官网中集群对客户端的意义中写道:

 

Assuming all cluster members are available, a client can connect to any node and perform any operation. Nodes will route operations to the queue master node transparently to clients.

 

With all supported messaging protocols a client is only connected to one node at a time.

 

In case of a node failure, clients should be able to reconnect to a different node, recover their topology and continue operation. For this reason, most client libraries accept a list of endpoints (hostnames or IP addresses) as a connection option. The list of hosts will be used during initial connection as well as connection recovery, if the client supports it. See documentation guides for individual clients to learn more.

 

There are scenarios where it may not be possible for a client to transparently continue operations after connecting to a different node. They usually involve non-mirrored queues hosted on a failed node.

 

15:HAProxy 安装,负载均衡使用;

 

haproxy.exe -f haproxy.cfg -D 后台运行;

 

taskkill /IM haproxy.exe /F 关闭haproxy;

 

 

 

 

16: 客户端检测AlreadyClosedException异常重连接入MQ节点;

 

17:自己写的一个消息队列应用测试:

 

 

 

 

 总结:主要解决了MQ服务器宕机或服务重启不影响客户端数据采集,可扩展单台MQ服务器连接瓶颈。

 

posted @ 2023-11-23 09:35  金虹巴巴  阅读(103)  评论(0编辑  收藏  举报