ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等。Zookeeper是hadoop的一个子项目,在分布式应用中,由于工程师不能很好地使用锁机制,以及基于消息的协调机制不适合在某些应用中使用,因此需要有一种可靠的、可扩展的、分布式的、可配置的协调机制来统一系统的状态
运行原理:Zookeeper的核心是原子广播,这个机制保证了各个Server之间的同步。实现这个机制的协议叫做Zab协议。Zab协议有两种模式,它们分别是恢复模式(选主)和广播模式(同步)。当服务启动或者在领导者崩溃后,Zab就进入了恢复模式,当领导者被选举出来,且大多数Server完成了和leader的状态同步以后,恢复模式就结束了。状态同步保证了leader和Server具有相同的系统状态。为了保证事务的顺序一致性,zookeeper采用了递增的事务id号(zxid)来标识事务。所有的提议(proposal)都在被提出的时候加上了zxid。实现中zxid是一个64位的数字,它高32位是epoch用来标识leader关系是否改变,每次一个leader被选出来,它都会有一个新的epoch,标识当前属于那个leader的统治时期。低32位用于递增计数。
每个Server在工作过程中有三种状态:
LOOKING:当前Server不知道leader是谁,正在搜寻
LEADING:当前Server即为选举出来的leader
FOLLOWING:leader已经选举出来,当前Server与之同步
一、前期准备
环境需要6台机器:hosts文件要保持一致
编辑/etc/hosts文件:
127.0.0.1 localhost localhost.localdomain localhost4localhost4.localdomain4
::1 localhost localhost.localdomainlocalhost6 localhost6.localdomain6
192.168.1.105 node105
192.168.1.106 node106
192.168.1.107 node107
192.168.1.108 node108
192.168.1.109 node109
192.168.1.110 node110
主机名定义为:node105依次排列
开始安装:
root身份登录系统 将jdk-7u76-linux-x64.tar.gz拷贝到/opt下面
cd /opt
tar zxvf jdk-7u76-linux-x64.tar.gz
解压后生成的文件名字叫做jdk1.7.0_76
vim /etc/profile
把如下代码放到文件的最后面
export JAVA_HOME=/opt/jdk1.7.0_76
export JAVA_BIN=/opt/jdk1.7.0_76/bin
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JAVA_BIN PATH CLASSPATH
source/etc/profile 使变量生效
二、zookeeper的部署 (三个节点上部署)
192.168.1.105,192.168.1.106,192.168.1.107三个节点上要部署zookeeper
将zookeeper-3.4.6.tar.gz 上传到/opt下面
cd /opt
tar -zxzf zookeeper-3.4.6.tar.gz
ln -s zookeeper-3.4.6 zookeeper
mkdir /opt/zookeeper/data
这是zk数据存储目录
cd /opt/zookeeper/data
vi myid
如果是node105则内容为1,如果是node106则内容为2,如果是node107则内容为3
mkdir /opt/zookeeper/logs
这是zk日志存储目录
cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg
--zookeeper启动时会读取zookeeper/conf/zoo.cfg文件内容,zookeeper/conf/ 下面默认是没有zoo.cfg文件的, 因此我们可以根据zookeeper/conf/zoo_sample.cfg来生成zookeeper/conf/zoo.cfg
vi /opt/zookeeper/conf/zoo.cfg
添加如下内容
dataDir=/opt/zookeeper/data
dataLogDir=/opt/zookeeper/logs
server.1=node105:2888:3888
server.2=node106:2888:3888
server.3=node107:2888:3888
dataDir=/data/app/zookeeper/data
dataLogDir=/data/app/zookeeper/logs
各个节点启服务:
/opt/zookeeper/bin/zkServer.sh start
/opt/zookeeper/bin/zkServer.sh status
node1
/opt/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Mode: follower
node2
/opt/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Mode: follower
node3
/opt/zookeeper/bin/zkServer.sh status
JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Mode: leader
可见node3为zookeepr leader
三、单集群activeMQ的部署
规划
192.168.1.105,192.168.1.106,192.168.1.107组成的是第一个集群,假设名字叫做cluster001 zookeeper+mq
如果有第二个集群
192.168.1.108,192.168.1.109,192.168.1.110组成的是第二个集群,假设名字叫做cluster002 mq
本文档只演示cluster001的部署,cluster002的部署类似于cluster001。
2.cluster001集群的部署
1)分别在三台主机中创建/opt/activemq/cluster001目录
$ mkdir -p /opt/activemq/cluster001
上传apache-activemq-5.11.1-bin.tar.gz到/opt/activemq/cluster001目录
2)解压并按节点命名
$ cd /opt/activemq/cluster001
$ tar -xvf apache-activemq-5.11.1-bin.tar.gz
$ mv apache-activemq-5.11.1 node-0X
(X代表节点号, 1表示node105、2表示node106、3表示node107,下同)
3)集群配置:
在 3 个 ActiveMQ 节点中配置 conf/activemq.xml 中的持久化适配器。修改其中 bind、zkAddress、
hostname和zkPath。注意:每个ActiveMQ的BrokerName必须相同,否则不能加入集群。
vim /opt/activemq/cluster001/node-01/conf/activemq.xml
vim /opt/activemq/cluster001/node-02/conf/activemq.xml
vim /opt/activemq/cluster001/node-03/conf/activemq.xml
Node-01中的配置:
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="cluster001" 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.1.105:2181,192.168.1.106:2181,192.168.1.107:2181"
hostname="node105"
zkPath="/activemq/leveldb-stores"
/>
</persistenceAdapter>
</broker>
<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>
<!-- 绿色标记的是要添加的代码 -->
<policyEntry queue=">" enableAudit="false">
<networkBridgeFilterFactory>
<conditionalNetworkBridgeFilterFactory replayWhenNoConsumers="true"/>
</networkBridgeFilterFactory>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
Node-02中的配置:
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="cluster001" 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.1.105:2181,192.168.1.106:2181,192.168.1.107:2181"
hostname="node106"
zkPath="/activemq/leveldb-stores"
/>
</persistenceAdapter>
</broker>
<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>
<!-- 绿色标记的是要添加的代码 -->
<policyEntry queue=">" enableAudit="false">
<networkBridgeFilterFactory>
<conditionalNetworkBridgeFilterFactory replayWhenNoConsumers="true"/>
</networkBridgeFilterFactory>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
Node-03中的配置:
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="cluster001" 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.1.105:2181,192.168.1.106:2181,192.168.1.107:2181"
hostname="node107"
zkPath="/activemq/leveldb-stores"
/>
</persistenceAdapter>
</broker>
<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>
<!-- 绿色标记的是要添加的代码 -->
<policyEntry queue=">" enableAudit="false">
<networkBridgeFilterFactory>
<conditionalNetworkBridgeFilterFactory replayWhenNoConsumers="true"/>
</networkBridgeFilterFactory>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
4)按顺序启动3个ActiveMQ节点:
$nohup /opt/activemq/cluster001/node-01/bin/activemq start &
$nohup /opt/activemq/cluster001/node-02/bin/activemq start &
$nohup /opt/activemq/cluster001/node-03/bin/activemq start &
四、多集群activeMQ的部署
这里只演示双集群的部署,三集群或n集群的部署都是类似的。
修改所有主机的activemq.xml文件
在<persistenceAdapter> 前面添加:
<networkConnectors>
<networkConnector uri="multicast://default"/>
</networkConnectors>
在<transportConnector name="openwire"> 标签的末尾添加:
discoveryUri="multicast://default"
使用及验证:
#jps
1522 QuorumPeerMain
2661 Jps
2139 activemq.jar
#ps -ef |grep ac 查看进程
找到起端口:8161
图示:
访问:http://192.168.1.150:8161/admin/ 账户及密码: admin admin
本文转自卫庄的痛 51CTO博客,原文链接:http://blog.51cto.com/crfsz/1872778,如需转载请自行联系原作者