rabbitmq单机多实例集群搭建

1.安装单机版的java


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


3.启动3个实例
#由于我配置了web管理插件,因此还要指定其web插件占用的端口号,若是不指定,将不能启动多个节点,由于端口号被占用web

RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit rabbitmq-server -detached
RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}]" RABBITMQ_NODENAME=rabbit2 rabbitmq-server -detached
RABBITMQ_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的节点,能够看到如下场景,证实多实例启动成功app

6.若是想加入一个新的节点到集群,只须要执行第五步相同的命令便可!测试

删除节点:rabbitmqctl forget_cluster_node rabbit3@hostname.net

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类:code

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类:server

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 + "'");
        }
    }
}
相关文章
相关标签/搜索