linux activmemq 集群安装,配置和高可用测试

 

 

 

从 ActiveMQ 5.9 开始,ActiveMQ 的集群实现方式取消了传统的Master-Slave 方式,增加了基于ZooKeeper + LevelDB的 Master-Slave实现方式,其他两种方式目录共享和数据库共享依然存在。 

 

 

本文主要讲解基于 ZooKeeper 和LevelDB 搭建ActiveMQ 集群。集群仅提供主备方式的高可用集 群功能,避免单点故障,没有负载均衡功能。 

官方文档:http://activemq.apache.org/replicated-leveldb-store.html 

 

高可用的原理:使用ZooKeeper(集群)注册所有的ActiveMQ Broker。只有其中的一个Broker 可以提供 服务,被视为Master,其他的Broker 处于待机状态,被视为Slave。如果Master 因故障而不能提供服务,

 ZooKeeper 会从 Slave中选举出一个 Broker充当 Master。 

Slave 连接 Master并同步他们的存储状态,Slave不接受客户端连接。所有的存储操作都将被复制到 

连接至 Master 的Slaves。如果 Master 宕了,得到了最新更新的 Slave 会成为 Master。故障节点在恢复后 

会重新加入到集群中并连接 Master 进入Slave 模式。 

所有需要同步的 disk 的消息操作都将等待存储状态被复制到其他法定节点的操作完成才能完成。所以,如果你配置了replicas=3,那么法定大小是(3/2)+1=2。Master 将会存储并更新然后等待 (2-1)=1 个Slave存储和更新完成,才汇报 success。至于为什么是 2-1,熟悉 Zookeeper 的应该知道,有一个 node要作为观擦者存在。当一个新的Master 被选中,你需要至少保障一个法定node 在线以能够找到拥有最新 状态的node。这个node 可以成为新的Master。因此,推荐运行至少3 个replica nodes,以防止一个node失败了,服务中断。(原理与 ZooKeeper 集群的高可用实现方式类似)

 

1、ActiveMQ集群部署规划:

 环境:CentOS 6.6 x64 、JDK7 

版本:ActiveMQ 5.11.1 

ZooKeeper 集群环境:192.168.1.81:2181,192.168.1.82:2182,192.168.1.83:2183

 

 

2、分别在三台主机中创建/home/yxq/activemq目录

# mkdir /home/yxq/activemq

上传 apache-activemq-5.11.1-bin.tar.gz 到/home/yxq/activemq 目录\

 

3、解压并按节点命名

# tar -xvf apache-activemq-5.11.1-bin.tar.gz

# cd /home/yxq/activemq 

# tar -xvf apache-activemq-5.11.1-bin.tar.gz

# mv apache-activemq-5.11.1 node-0X#(X代表节点号 1、2、3,下同)

 

4、修改管理控制台端口(默认为 8161)可在 conf/jetty.xml 中修改,如下:

 node-01 管控台端口:

 <bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">

             <!-- the default port number for the web console -->

        <property name="host" value="0.0.0.0"/>

        <property name="port" value="8161"/>

  </bean>

 node-02管控台端口:

 <bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">

             <!-- the default port number for the web console -->

        <property name="host" value="0.0.0.0"/>

        <property name="port" value="8162"/>

  </bean>

 

 node-03管控台端口:

<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">

             <!-- the default port number for the web console -->

        <property name="host" value="0.0.0.0"/>

        <property name="port" value="8163"/>

  </bean>

 

5、集群配置: 

在 3 个ActiveMQ 节点中配置conf/activemq.xml 中的持久化适配器。修改其中bind、zkAddress、hostname和 zkPath。注意:每个 ActiveMQ 的 BrokerName 必须相同,否则不能加入集群。

所有节点中activemq.xml配置

   

node-01 中的持久化配置:

activemq docker 集群 activemq集群原理_网络

node-02 中的持久化配置:

activemq docker 集群 activemq集群原理_java_02

node-03 中的持久化配置:

activemq docker 集群 activemq集群原理_activemq docker 集群_03

 

修改各节点的消息端口(注意,避免端口冲突):

node-01 中的消息端口配置:

activemq docker 集群 activemq集群原理_apache_04

node-02 中的消息端口配置:

 

activemq docker 集群 activemq集群原理_apache_05

 node-03 中的消息端口配置:

activemq docker 集群 activemq集群原理_网络_06

 

6、按顺序启动 3个 ActiveMQ节点:(zookeeper集群已经启动)

#/home/yxq/zookeeper/node-01/bin/zkSeerver.sh start

#/home/yxq/zookeeper/node-02/bin/zkSeerver.sh start

#/home/yxq/zookeeper/node-03/bin/zkSeerver.sh start

 

# /home/yxq/activemq/node-01/bin/activemq start

# /home/yxq/activemq/node-02/bin/activemq start

# /home/yxq/activemq/node-03/bin/activemq start

 

监听日志:

#  tail -f /home/yxq/activemq/node-01/data/activemq.log 

#  tail -f /home/yxq/activemq/node-02/data/activemq.log 

#  tail -f /home/yxq/activemq/node-03/data/activemq.log 

 

 

 7.集群可用性测试

ActiveMQ的客户端只能访问Master的Broker,其他处于Slave的Broker不能访问,所以客户端连接的Broker应该使用failover协议(失败转移)

failover:(tcp://192.168.1.81:51511,tcp://192.168.1.82:51512,tcp://192.168.1.83:51513)?randomize=false

activemq docker 集群 activemq集群原理_apache_07

 

8.集群高可用测试

   当一个ActiveMQ节点挂掉,或者一个Zookeeper节点挂掉,ActiveMQ服务依然正常运转,如果仅剩一个ActiveMQ节点,因为不能选举Master,ActiveMQ不能正常运行:同样的,如果Zookeeper仅剩一个节点活动,不管ActiveMQ各节点存活,ActiveMQ也不能正常提供服务。(ActiveMQ集群的高可用,依赖于Zookeeper集群的高可用)

 

 

9.设置开机启动

#vi /etc/rc.local

su - yxq -c '/home/yxq/activemq/node-01/bin/activemq start'

su - yxq -c '/home/yxq/activemq/node-02/bin/activemq start'

su - yxq -c '/home/yxq/activemq/node-03/bin/activemq start'

 

10.ActiveMQ集群测试。至此activemq集群搭建完毕。

activemq docker 集群 activemq集群原理_activemq docker 集群_08

 

11、负载均衡集群

配置放在

集群 1 链接集群 2:

 

activemq docker 集群 activemq集群原理_apache_09

 

集群 2 链接集群 1: 

集群2(伪集群)连接集群1配置 

activemq docker 集群 activemq集群原理_网络_10

 

activemq docker 集群 activemq集群原理_apache_11

集群2(伪集群)端口放开

activemq docker 集群 activemq集群原理_java_12

启动集群1(伪集群)启动

activemq docker 集群 activemq集群原理_java_13

activemq docker 集群 activemq集群原理_网络_14

集群1连接集群2配置

activemq docker 集群 activemq集群原理_java_15

集群2连接集群1配置

activemq docker 集群 activemq集群原理_activemq docker 集群_16