RabbitMQ: 批量创建节点并组成集群

    技术2022-05-19  21

    作者:Hily 原始链接:http://hily.me/blog/2010/05/rabbitmq-multi-node-cluster/ 版权声明:可以转载,转载时务必以超链接形式标明文章原始出处 和作者信息 及版权声明

     

    多核 CPU 上创建集群的好处很明显,可以充分地利用 CPU 运算能力。

    之前讲的都是在单机上用命令方式逐个创建节点,然后由这些节点组成集群。

    其实如果是在同一台计算机上创建多个节点,RabbitMQ 提供了一个便捷的命令来批量操作。

    这个命令是:rabbitmq-multi。

    命令格式: # rabbitmq-multi start_all count 其中 count 为要创建的节点的数量。

    运行该命令,将会创建 count 个节点,这些节点监听的端口从 5672 顺序开始。第一个节点名称为环境变量 RABBITMQ_NODENAME 定义的名称,之后的节点名称则以环境变量 RABBITMQ_NODENAME 定义的节点名和节点序号组成,并以下划线 _ 连接。

    如:定义的节点名称为 test,创建3个节点,则3个节点的名称分别为:test、test_1、test_2。

    为了让节点创建时自动装载集群配置,先创建集群配置文件 /etc/rabbitmq/rabbitmq_cluster.config。 因为是批量创建,所以配置内容与前文所述有所差别,配置内容: ['test@gentoo'].

    创建3个节点: # ./rabbitmq-multi start_all 3

    查看创建的节点状态: # ./rabbitmq-multi status Status of all running nodes... Node 'test_2@gentoo' with Pid 30120: running Node 'test_1@gentoo' with Pid 30073: running Node 'test@gentoo' with Pid 30026: running done.

    查看集群中节点状态: # ./rabbitmqctl -n test@gentoo status Status of node test@gentoo ... [..., {nodes,[test_2@gentoo,test_1@gentoo,test@gentoo]}, {running_nodes,[test_2@gentoo,test_1@gentoo,test@gentoo]}] ...done. # ./rabbitmqctl -n test_1@gentoo status Status of node test_1@gentoo ... [..., {nodes,[test_2@gentoo,test_1@gentoo,test@gentoo]}, {running_nodes,[test_2@gentoo,test@gentoo,test_1@gentoo]}] ...done. # ./rabbitmqctl -n test_2@gentoo status Status of node test_2@gentoo ... [..., {nodes,[test_2@gentoo,test_1@gentoo,test@gentoo]}, {running_nodes,[test@gentoo,test_1@gentoo,test_2@gentoo]}] ...done.

    停止集群中的所有节点: # ./rabbitmq-multi stop_all Stopping all nodes... Stopping node test_2@gentoo Erlang has closed ....OK Stopping node test_1@gentoo Erlang has closed ....OK Stopping node test@gentoo Erlang has closed ....OK done.

    -- EOF --


    最新回复(0)