rabbitmq单机多实例集群搭建

来源:互联网 发布:js根据逗号截取字符串 编辑:程序博客网 时间:2024/05/22 05:04

原文地址,转载请注明出处:http://blog.csdn.net/qq_34021712/article/details/72633948    ©王赛超

这里展示的是单机集群的部署,如果机器足够多,可以选择多机集群部署,详细可以参考《rabbitmq集群搭建(多机)》。 


1.安装单机版的 教程:《Linux下安装rabbitmq


2.要搭建集群,先将之前单机版中历史记录干掉,删除rabbitmq/var/lib/rabbitmq/mnesia下的所有内容。


3.启动3个实例

#因为我配置了web管理插件,所以还要指定其web插件占用的端口号,如果不指定,将不能启动多个节点,因为端口号被占用RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit rabbitmq-server -detachedRABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}]" RABBITMQ_NODENAME=rabbit2 rabbitmq-server -detachedRABBITMQ_NODE_PORT=5674 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}]" RABBITMQ_NODENAME=rabbit3 rabbitmq-server -detached


4.在浏览器访问每一个rabbitmq实例,是够可以显示登录页面,如果显示成功,继续往下进行


5.我们以rabbit为主节点,剩下两个为从节点(在从节点执行以下命令,主节点不用动,-n指定具体那个节点)
          [root@localhost mnesia]# rabbitmqctl -n rabbit2 stop_app
          Stopping rabbit application on node rabbit2@localhost ...
          [root@localhost mnesia]# rabbitmqctl -n rabbit2 reset
          Resetting node rabbit2@localhost ...
          [root@localhost mnesia]# rabbitmqctl -n rabbit2 join_cluster rabbit@`hostname -s`
          Clustering node rabbit2@localhost with rabbit@localhost ...
          [root@localhost mnesia]# rabbitmqctl -n rabbit2 start_app
          Starting node rabbit2@localhost ...
          [root@localhost mnesia]# rabbitmqctl -n rabbit3 stop_app
          Stopping rabbit application on node rabbit3@localhost ...
          [root@localhost mnesia]# rabbitmqctl -n rabbit3 reset
          Resetting node rabbit3@localhost ...
          [root@localhost mnesia]# rabbitmqctl -n rabbit3 join_cluster rabbit@`hostname -s`
          Clustering node rabbit3@localhost with rabbit@localhost ...
          [root@localhost mnesia]# rabbitmqctl -n rabbit3 start_app
          Starting node rabbit3@localhost ...
这是查看每一个rabbitmq的节点,可以看到以下场景,证明多实例启动成功



6.如果想加入一个新的节点到集群,只需要执行第五步相同的命令即可!

删除节点:rabbitmqctl forget_cluster_node rabbit3@hostname



7.使用rabbitmqctl -n rabbit cluster_status查看集群信息
          [root@localhost mnesia]# rabbitmqctl -n rabbit cluster_status
          Cluster status of node rabbit@localhost ...
          [{nodes,[{disc,[rabbit2@localhost,rabbit3@localhost,rabbit@localhost]}]},
           {running_nodes,[rabbit3@localhost,rabbit2@localhost,rabbit@localhost]},
           {cluster_name,<<"rabbit@localhost">>},
           {partitions,[]},
           {alarms,[{rabbit3@localhost,[]},
                    {rabbit2@localhost,[]},
                    {rabbit@localhost,[]}]}]
搭建过程中,可能会出现如下错误,我感觉只要是出现带mnesia关键字的,把rabbitmq/var/lib/rabbitmq/mnesia下所有内容删掉(rm -rf *)。
Clustering node hare@localhost with rabbit@localhost ...
Error: mnesia_not_running


8.测试,生产者使用5672生产消息,消费者使用5673获取消息,如果能获取到表示集群搭建成功
Producer类:
package com.rabbitmq.test.T_helloworld;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.test.util.ConnectionUtil;/** * helloworld * @author lenovo * */public class Producer {private final static String QUEUE_NAME = "test_queue";    public static void main(String[] argv) throws Exception {    //定义一个链接工厂ConnectionFactory factory=new ConnectionFactory();//设置服务地址,IP,端口,账号密码信息factory.setHost("192.168.1.103");factory.setPort(5672);factory.setUsername("admin");factory.setPassword("admin");//vhost:虚拟主机,一个broker里可以开设多个vhost,用作不同用户的权限分离。//factory.setVirtualHost("/testrabbit");// 获取到连接以及mq通道Connection connection = factory.newConnection();                //Connection connection = ConnectionUtil.getConnection();        // 从连接中创建通道        Channel channel = connection.createChannel();        /*         * 声明(创建)队列         * 参数1:队列名称         * 参数2:为true时server重启队列不会消失         * 参数3:队列是否是独占的,如果为true只能被一个connection使用,其他连接建立时会抛出异常         * 参数4:队列不再使用时是否自动删除(没有连接,并且没有未处理的消息)         * 参数5:建立队列时的其他参数         */        channel.queueDeclare(QUEUE_NAME, false, false, false, null);        // 消息内容        String message = "Hello World!";        /*         * 向server发布一条消息         * 参数1:exchange名字,若为空则使用默认的exchange         * 参数2:routing key         * 参数3:其他的属性         * 参数4:消息体         * RabbitMQ默认有一个exchange,叫default exchange,它用一个空字符串表示,它是direct exchange类型,         * 任何发往这个exchange的消息都会被路由到routing key的名字对应的队列上,如果没有对应的队列,则消息会被丢弃         */        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());        System.out.println(" [生产者] Sent '" + message + "'");        //关闭通道和连接        channel.close();        connection.close();    }}

Consumer类:

package com.rabbitmq.test.T_helloworld;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.QueueingConsumer;import com.rabbitmq.test.util.ConnectionUtil;public class Consumer {private final static String QUEUE_NAME = "test_queue";    public static void main(String[] argv) throws Exception {    //定义一个链接工厂ConnectionFactory factory=new ConnectionFactory();//设置服务地址,IP,端口,账号密码信息factory.setHost("192.168.1.103");factory.setPort(5673);factory.setUsername("admin");factory.setPassword("admin");//vhost:虚拟主机,一个broker里可以开设多个vhost,用作不同用户的权限分离。//factory.setVirtualHost("/testrabbit");// 获取到连接以及mq通道Connection connection = factory.newConnection();        // 从连接中创建通道        Channel channel = connection.createChannel();        // 声明队列(如果你已经明确的知道有这个队列,那么下面这句代码可以注释掉,如果不注释掉的话,也可以理解为消费者必须监听一个队列,如果没有就创建一个)        channel.queueDeclare(QUEUE_NAME, false, false, false, null);        // 定义队列的消费者        QueueingConsumer consumer = new QueueingConsumer(channel);       /*         * 监听队列         * 参数1:队列名称         * 参数2:是否发送ack包,不发送ack消息会持续在服务端保存,直到收到ack。  可以通过channel.basicAck手动回复ack         * 参数3:消费者         */         channel.basicConsume(QUEUE_NAME, true, consumer);        // 获取消息        while (true) {            QueueingConsumer.Delivery delivery = consumer.nextDelivery();            String message = new String(delivery.getBody());            System.out.println(" [消费者] Received '" + message + "'");        }    }}


原创粉丝点击