采用ZooKeeper 和 LevelDB 搭建 ActiveMQ 集群。集群仅提供主备方式的高可用集群功能,避免单点故障,没有负载均衡功能。
官方文档:http://activemq.apache.org/replicated-leveldb-store.html
1. ZooKeeper集群安装
准备3台服务器 192.168.43.129,192.168.43.130,192.168.43.131 ,参考 http://blog.csdn.net/zsg88/article/details/76286225
2. 在 192.168.43.129,192.168.43.130,192.168.43.131 分贝安装activemq,参考:
linux下ActiveMQ的安装配置一 http://blog.csdn.net/zsg88/article/details/76153246
linux下ActiveMQ的安装配置二 http://blog.csdn.net/zsg88/article/details/76158126
安装在 /home/zsg/program 目录下
cd /home/zsg/program
tar -zxvf apache-activemq-5.15.0-bin.tar.gz
mv apache-activemq-5.15.0 activemq
2. activemq 集群配置
在 3 个 ActiveMQ 节点中配置 conf/activemq.xml 中的持久化适配器。修改其中 bind、zkAddress、hostname 和 zkPath。注意:每个 ActiveMQ 的 BrokerName 必须相同,否则不能加入集群。
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="mq-dubbo" dataDirectory="${activemq.data}">
......
<persistenceAdapter>
<!--
<kahaDB directory="${activemq.data}/kahadb"/>
-->
<replicatedLevelDB
directory="${activemq.data}/leveldb"
replicas="3"
bind="tcp://0.0.0.0:62621"
zkAddress="192.168.43.129:2181,192.168.43.130:2181,192.168.43.131:2181"
zkPassword=""
hostname="zk-01"
zkPath="/activemq/leveldb-stores" />
</persistenceAdapter>
</broker>
bind: 集群的通讯端口
zkAddress : zookeeper集群的ip和对客户端的监听端口
hostname : 主机名或ip
zkPath: 代表zookeeper内的“路径”,即你运行在2181端口的zk内的“寻址节点”,类似于JNDI。如果你没有这个zkPath,默认它在zk内的寻址节点为“/default”。加入到某一组master slave中的mq的实例中的zkPath必须完全匹配。不用改。
replicas:这边的数字指的就是ActiveMQ实例的节点数,它需要满足2N+1,因此你也可以5台(1拖4)。
在<policyEntries>节点中添加
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry topic=">" >
<!-- The constantPendingMessageLimitStrategy is used to prevent
slow topic consumers to block producers and affect other consumers
by limiting the number of messages that are retained
For more information, see:
http://activemq.apache.org/slow-consumer-handling.html
-->
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
<!-- 新添加的代码 -->
<policyEntry queue=">" enableAudit="false">
<networkBridgeFilterFactory>
<conditionalNetworkBridgeFilterFactory replayWhenNoConsumers="true"/>
</networkBridgeFilterFactory>
</policyEntry>
</policyMap>
</destinationPolicy>
4.端口
activemq.xml 看到这么多通讯端口
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
activemq的jetty访问的默认端口是8161
如果你的3个activemq都在1台机器里,那么每个mq的的端口都不能一样。
如果是3太不同的机器,那么就可以都是同样的端口。
5. 先启动zookeeper集群
6. 启动3个activema
/home/zsg/program/activemq/bin/activemq start
一台一台启动的时候也要查active的log
cd data
tail -fn 100 activemq.log
如果到这里启动没什么问题的话,当你启动第一台mq的时候,zookeeper的日志就会打出英文的日志告诉你一台activemq不够,至少两台才能形成Master/Slave。
之后可以在浏览器上分别访问三台activeMq的web管理后台 http://192.168.43.129:8161/ ,http://192.168.43.130:8161/ ,http://192.168.43.131:8161/ ,这种主从的方式成功的话,只会有一台是master,三个端口也只能有一个可以访问,其他两个都是等待状态。
经测试,后台也只能有一个可以访问。
7. 通过分析activemq进程所占用端口的情况来确定集群的主从
(1)首先根据名称用ps命令查看进程的id:
$ ps -ef | grep activemq
找到activemq的进程编号 61573
(2)查到进程id之后,使用netstat命令查看其占用的端口:
$ netstat -nap | grep 61573
图一
图二
我们能看到2种端口占用的情况,很明显图二是主服务器,图一是从服务器
那么很明显,admin后台的访问地址是 http://192.168.43.130.8161
8. 客户端测试
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name=“brokerURL” value=“failover:(tcp://192.168.43.129:61616,tcp://192.168.43.130:61616, tcp://192.168.43.131:61616)" />
<property name="useAsyncSend" value="true" />
<property name="alwaysSessionAsync" value="true" />
<property name="useDedicatedTaskRunner" value="false" />
</bean>
生产和消费分成两个程序。
生产发一条消息,注意看console会输出如“[INFO] Successfully reconnected to tcp://192.168.43.129:61616”
手工停止61616这台实例,然后你会发觉另2个mq中有一个被promoted to master
然后运行consumer,consumer会连上被promoted的实例并继续消费刚才那几条消息
8.手动控制台方式启动,方便查看错误
/home/zsg/program/activemq/bin/activemq console