分享

rabbitmq单机多实例集群搭建

 曾淼Mark 2019-04-30

这里展示的是单机集群的部署,如果机器足够多,可以选择多机集群部署,详细可以参考《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 -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的节点,可以看到以下场景,证明多实例启动成功



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类:
  1. package com.rabbitmq.test.T_helloworld;
  2. import com.rabbitmq.client.Channel;
  3. import com.rabbitmq.client.Connection;
  4. import com.rabbitmq.client.ConnectionFactory;
  5. import com.rabbitmq.test.util.ConnectionUtil;
  6. /**
  7. * helloworld
  8. * @author lenovo
  9. *
  10. */
  11. public class Producer {
  12. private final static String QUEUE_NAME = "test_queue";
  13. public static void main(String[] argv) throws Exception {
  14. //定义一个链接工厂
  15. ConnectionFactory factory=new ConnectionFactory();
  16. //设置服务地址,IP,端口,账号密码信息
  17. factory.setHost("192.168.1.103");
  18. factory.setPort(5672);
  19. factory.setUsername("admin");
  20. factory.setPassword("admin");
  21. //vhost:虚拟主机,一个broker里可以开设多个vhost,用作不同用户的权限分离。
  22. //factory.setVirtualHost("/testrabbit");
  23. // 获取到连接以及mq通道
  24. Connection connection = factory.newConnection();
  25. //Connection connection = ConnectionUtil.getConnection();
  26. // 从连接中创建通道
  27. Channel channel = connection.createChannel();
  28. /*
  29. * 声明(创建)队列
  30. * 参数1:队列名称
  31. * 参数2:为true时server重启队列不会消失
  32. * 参数3:队列是否是独占的,如果为true只能被一个connection使用,其他连接建立时会抛出异常
  33. * 参数4:队列不再使用时是否自动删除(没有连接,并且没有未处理的消息)
  34. * 参数5:建立队列时的其他参数
  35. */
  36. channel.queueDeclare(QUEUE_NAME, false, false, false, null);
  37. // 消息内容
  38. String message = "Hello World!";
  39. /*
  40. * 向server发布一条消息
  41. * 参数1:exchange名字,若为空则使用默认的exchange
  42. * 参数2:routing key
  43. * 参数3:其他的属性
  44. * 参数4:消息体
  45. * RabbitMQ默认有一个exchange,叫default exchange,它用一个空字符串表示,它是direct exchange类型,
  46. * 任何发往这个exchange的消息都会被路由到routing key的名字对应的队列上,如果没有对应的队列,则消息会被丢弃
  47. */
  48. channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
  49. System.out.println(" [生产者] Sent '" + message + "'");
  50. //关闭通道和连接
  51. channel.close();
  52. connection.close();
  53. }
  54. }

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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多