方案描述
该方案使用Zookeeper选择出ActiveMQ的Master。Master会将每次的消息接收和处理同步到Salve之后再返回给客户端。如果Master崩溃,会重新选择Slave升级为Master,然后继续提供工作。
该方案使用LevelDB持久化消息,每个MQ节点单独存储数据。
配置步骤
Zookeeper集群配置
版本:zookeeper-3.4.9(需要安装JDK)
重命名/zookeeper-3.4.9/conf/zoo_sample.cfg为zoo.cfg
内容如下:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/app/data/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=0.0.0.0:2888:3888
server.2=10.130.9.123:2888:3888
server.3=10.130.9.124:2888:3888
需要创建一个文件夹存放数据dataDir=/app/data/zookeeper
配置各个server的ip地址,这里有三台server。本机ip地址可以写成0.0.0.0代替。
在/app/data/zookeeper下创建一个文件并命名为myid,并写入与ip地址相称的服务器编号,比如本机对应的server编号为1,就在myid文件中写入数字1。
启动服务:
切换到zookeeper的bin目录
./zkServer.sh start
执行后查看bin目录下面的zookeeper.out日志,查看集群状况。
其他几台依次类推
ActiveMQ集群配置
版本:apache-activemq-5.14.2
配置conf目录下面的activemq.xml
<broker xmlns="http://activemq.apache.org/schema/core"
brokerName="localhost" dataDirectory="${activemq.data}">
保证brokerName相同
主要是配置持久化:
<persistenceAdapter>
<replicatedLevelDB
directory="${activemq.data}/leveldb"
replicas="3"
bind="tcp://0.0.0.0:0"
zkAddress="10.130.9.127:2181,10.130.9.123:2181,10.130.9.124:2181"
zkSessionTimeout="2s"
hostname="10.130.9.127"
sync="local_disk"
zkPath="/activemq/leveldb-stores"/>
</persistenceAdapter>
directory:数据文件存储的路径,如果不存在会自己创建
replicas:集群中存在的节点的数量,一般以上的节点存活,才能保证系统继续工作
bind:当ActiveMQ成为master时为复制协议提供服务。可以配置成动态的tcp://0.0.0.0:0,也可以配置成固定的端口号。
zkAddress:zookeeper集群的所有集群ip加端口号地址。
zkSessionTimeout:zookeeper侦测节点失效的时间
hostname:当前主机信息
sync:同步策略
zkPath:zookeeper内部数据交换路径
启动服务:
进入bin目录,运行脚本
./activemq start
依次配置其他节点
都运行完成后,可以通过netstat -lntp命令查看集群中每台机器的端口,只会有一台机器打开8161等服务端口提供服务,其他服务器因为不是Master,服务都会被挂起,只能进行数据同步,如果停掉当前master(运行脚本./activemq stop),其他Slave中会有一台变成master,便打开8161等端口提供服务。
客户端配置
由原来的单台配置
failover:(tcp://10.130.9.123:61616)
变成多台
failover:(tcp://10.130.9.123:61616,tcp://10.130.9.124:61616,tcp://10.130.9.127:61616)
其他不变。