(三)RabbitMQ集群(Ⅰ)

RabbitMQ集群(Ⅰ)

安装RabbitMQ:https://blog.csdn.net/ycj_xiyang/article/details/79926586

RabbitMQ集群(Ⅱ):https://blog.csdn.net/YCJ_xiyang/article/details/79958507

什么是集群?什么是分布式?

    分布式:一个业务分拆多个子业务,部署在不同的服务器上
    集群:同一个业务,部署在多个服务器上 

RabbitMQ基于erlang语言开发,而erlang是一门分布式语言开发,适用于集群开发;rabbitmq自身也提供了多种集群方案:http://www.rabbitmq.com/ha.html 

  1.集群管理:没有明显的主从,主要的是disk和ram节点的区别
  2.做集群的需求:不支持跨网段(erlang限制)
  3.集群类型:普通集群、镜像集群
    普通集群:结构同步,消息实体只存在一个节点中,但consumer在非消息节点获取是,节点间存在消息拉取,易产生性能瓶颈。
    镜像集群:集群中一个master,负责调度,处理消息实体,其他节点保存一份数据到本地;性能主要靠master承载。
  4.持久化,分两部分:
    Rabbitmq服务器配置持久化:默认的就是持久化(disc类型);
    代码持久化:默认情况下,代码创建的消息队列和存放在队列里的消息都是非持久化的,需要在建立队列时指定。

启动节点

    一共三种启动方式

        一般启动:service rabbitmq-server satrt

[root@192 rabbitmq]# service rabbitmq-server start
Starting rabbitmq-server: SUCCESS
rabbitmq-server.

        前台启动:rabbitmq-server

[root@192 rabbitmq]# rabbitmq-server

              RabbitMQ 3.5.6. Copyright (C) 2007-2015 Pivotal Software, Inc.
  ##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
  ##  ##
  ##########  Logs: /var/log/rabbitmq/rabbit@localhost.log
  ######  ##        /var/log/rabbitmq/rabbit@localhost-sasl.log
  ##########
              Starting broker... completed with 0 plugins.

                         如果当前的窗口终结,则这个服务会退出

        后台启动:rabbitmq-server -detached

[root@192 rabbitmq]# rabbitmq-server  -detached  
Warning: PID file not written; -detached was passed.
[root@192 rabbitmq]# echo $?
0
[root@192 rabbitmq]# 

同步Erlang cookie

    集群中node必须使用相同的cookie才能相互通讯

        cookie的位置一般在 /var/lib/rabbitmq/.erlang.cookie(内容任意)

查看集群的状态

    rabbitmqctl cluster_status    

[root@h101 ~]# rabbitmqctl  cluster_status
Cluster status of node rabbit@h101 ...
[{nodes,[{disc,[rabbit@h101]}]},
 {running_nodes,[rabbit@h101]},
 {cluster_name,<<"rabbit@h101.temp">>},
 {partitions,[]}]
[root@h101 ~]# 
[root@h102 ~]# rabbitmqctl  cluster_status
Cluster status of node rabbit@h102 ...
[{nodes,[{disc,[rabbit@h102]}]},
 {running_nodes,[rabbit@h102]},
 {cluster_name,<<"rabbit@h102.temp">>},
 {partitions,[]}]
[root@h102 ~]# 

创建集群

    集群中node是平等的,新node可以选择任意一个节点加入

    加入集群分三步(更多的节点加入也是一样的方法)

        首先关闭本地应用 rabbitmqctl stop_app

[root@h101 ~]# rabbitmqctl  stop_app 
Stopping node rabbit@h101 ...

        加入集群 rabbitmqctl join_cluster 节点名

[root@h102 ~]# rabbitmqctl join_cluster  rabbit@h101
Clustering node rabbit@h102 with rabbit@h101 ...
[root@h102 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@h102 ...
[{nodes,[{disc,[rabbit@h101,rabbit@h102]}]}]
[root@h101 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@h101 ...
[{nodes,[{disc,[rabbit@h101,rabbit@h102]}]},
 {running_nodes,[rabbit@h101]},
 {cluster_name,<<"rabbit@h101.temp">>},
 {partitions,[]}]
[root@h101 ~]# 

        启动应用 rabbitmqctl start_app

[root@h102 ~]# rabbitmqctl  start_app
Starting node rabbit@h102 ...
[root@h102 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@h102 ...
[{nodes,[{disc,[rabbit@h101,rabbit@h102]}]},
 {running_nodes,[rabbit@h101,rabbit@h102]},
 {cluster_name,<<"rabbit@h101.temp">>},
 {partitions,[]}]
[root@h102 ~]# 
[root@h101 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@h101 ...
[{nodes,[{disc,[rabbit@h101,rabbit@h102]}]},
 {running_nodes,[rabbit@h102,rabbit@h101]},
 {cluster_name,<<"rabbit@h101.temp">>},
 {partitions,[]}]
[root@h101 ~]# 

重启集群node

    加入集群的节点可以任意关停、下线或宕机

关闭h101

[root@h101 ~]# rabbitmqctl  stop 
Stopping and halting node rabbit@h101 ...
[root@h101 ~]# 

h101上的日志

=INFO REPORT==== 23-Mar-2018::22:19:33 ===
Stopping RabbitMQ

=INFO REPORT==== 23-Mar-2018::22:19:33 ===
stopped TCP Listener on [::]:5672

=INFO REPORT==== 23-Mar-2018::22:19:33 ===
Stopped RabbitMQ application

=INFO REPORT==== 23-Mar-2018::22:19:33 ===
Halting Erlang VM
h102上的日志
=INFO REPORT==== 23-Mar-2018::22:19:33 ===
rabbit on node rabbit@h101 down

=INFO REPORT==== 23-Mar-2018::22:19:33 ===
Keep rabbit@h101 listeners: the node is already back

=INFO REPORT==== 23-Mar-2018::22:19:34 ===
node rabbit@h101 down: connection_closed

h102上的集群状态

[root@h102 ~]# rabbitmqctl cluster_status 
Cluster status of node rabbit@h102 ...
[{nodes,[{disc,[rabbit@h101,rabbit@h102]}]},
 {running_nodes,[rabbit@h102]},
 {cluster_name,<<"rabbit@h101.temp">>},
 {partitions,[]}]
[root@h102 ~]# 

启动h101

[root@h101 ~]# /etc/init.d/rabbitmq-server start 
Starting rabbitmq-server: SUCCESS
rabbitmq-server.
[root@h101 ~]#

h102上的日志

=INFO REPORT==== 23-Mar-2018::22:26:04 ===
node rabbit@h101 up

=INFO REPORT==== 23-Mar-2018::22:26:05 ===
rabbit on node rabbit@h101 up

h102上的集群状态

[root@h102 ~]# rabbitmqctl cluster_status 
Cluster status of node rabbit@h102 ...
[{nodes,[{disc,[rabbit@h101,rabbit@h102]}]},
 {running_nodes,[rabbit@h101,rabbit@h102]},
 {cluster_name,<<"rabbit@h101.temp">>},
 {partitions,[]}]
[root@h102 ~]# 

h101上的集群状态

[root@h101 ~]#  rabbitmqctl cluster_status 
Cluster status of node rabbit@h101 ...
[{nodes,[{disc,[rabbit@h101,rabbit@h102]}]},
 {running_nodes,[rabbit@h102,rabbit@h101]},
 {cluster_name,<<"rabbit@h101.temp">>},
 {partitions,[]}]
[root@h101 ~]# 

拆分集群

    本地退出

        移除某个node分三步

            停止本地应用 rabbitmqctl stop_app

[root@h102 ~]# rabbitmqctl cluster_status 
Cluster status of node rabbit@h102 ...
[{nodes,[{disc,[rabbit@h101,rabbit@h102]}]},
 {running_nodes,[rabbit@h101,rabbit@h102]},
 {cluster_name,<<"rabbit@h101.temp">>},
 {partitions,[]}]
[root@h102 ~]# rabbitmqctl stop_app 
Stopping node rabbit@h102 ...

            重启node rabbitmqctl reset

[root@h102 ~]# rabbitmqctl reset
Resetting node rabbit@h102 ...

            启动应用 rabbitmqctl start_app

[root@h102 ~]# rabbitmqctl start_app
Starting node rabbit@h102 ...
[root@h102 ~]# rabbitmqctl cluster_status 
Cluster status of node rabbit@h102 ...
[{nodes,[{disc,[rabbit@h102]}]},
 {running_nodes,[rabbit@h102]},
 {cluster_name,<<"rabbit@h102.temp">>},
 {partitions,[]}]
[root@h102 ~]# 

    远程剔除

            无法响应的node,一段时间里无法交互,可以从集群中剔除

                首先停掉本地应用 rabbitmqctl stop_app

[root@h101 ~]# rabbitmqctl stop_app
Stopping node rabbit@h101 ...
[root@h101 ~]# 

                在另一个服务上进行剔除 rabbitmqctl forget_cluster_node 无法响应的节点名

[root@h102 ~]# rabbitmqctl forget_cluster_node rabbit@h101
Removing node rabbit@h101 from cluster ...
[root@h102 ~]# 

               此时在另一个服务上无法响应的节点已经剔除,但是无法响应的节点还认为自己是集群的一部分,一旦它的应用恢复,它会尝试与集群联络,从而报错

[root@h101 ~]# rabbitmqctl start_app
Starting node rabbit@h101 ...


BOOT FAILED
===========

Error description:
   {error,{inconsistent_cluster,"Node rabbit@h101 thinks it's clustered with node rabbit@h102, but rabbit@h102 disagrees"}}

Log files (may contain more information):
   /var/log/rabbitmq/rabbit@h101.log
   /var/log/rabbitmq/rabbit@h101-sasl.log

Stack trace:
   [{rabbit_mnesia,check_cluster_consistency,0},
    {rabbit,'-start/0-fun-0-',0},
    {rabbit,start_it,1},
    {rpc,'-handle_call_call/6-fun-0-',5}]

Error: {error,{inconsistent_cluster,"Node rabbit@h101 thinks it's clustered with node rabbit@h102, but rabbit@h102 disagrees"}}
[root@h101 ~]#

     解决的办法是  一旦有机会连上无法响应的节点,就对它进行重置

[root@h101 ~]# rabbitmqctl reset
Resetting node rabbit@h101 ...
[root@h101 ~]# rabbitmqctl start_app 
Starting node rabbit@h101 ...
[root@h101 ~]# rabbitmqctl cluster_status  
Cluster status of node rabbit@h101 ...
[{nodes,[{disc,[rabbit@h101]}]},
 {running_nodes,[rabbit@h101]},
 {cluster_name,<<"rabbit@h101.temp">>},
 {partitions,[]}]
[root@h101 ~]# 

                  

                 

阅读更多
文章标签: cluster
个人分类: RabbitMQ
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭