环境准备:192.168.1.2,192.168.1.3,192.168.1.4.系统均使用Redhat 6,防火墙关闭,并且jdk已安装.
之所以选三台,是因为zookeeper推荐最低三台配置,这样可以保持最大的可用服务器数。
安装配置zookeeper(以下操作三台机器都要):
cd /home/app
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
tar -zxvf zookeeper-3.4.11
cd zookeeper-3.4.11/conf
mv zoo_sample.cfg zoo.cfg
以下操作在第一台机器,即192.168.1.2:
vi zoo.cfg,内容如下:
# The number of milliseconds of each tick
tickTime=2000 //ZK中的时间单元,ZK中所有时间都是以这个时间单元为基础,进行整数倍配置的,单位毫秒.
# The number of ticks that the initial
# synchronization phase can take
initLimit=10 //默认值是10,即tickTime属性值的10倍.用于配置允许followers连接并同步到leader的最大时间.如果ZooKeeper管理的数据量很大的话可以增加这个值
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5 //默认值是5,即tickTime属性值的5倍.用于配置leader和followers间进行心跳检测的最大延迟时间.如果在设置的时间内followers无法与leader进行通信,,那么followers将会被丢弃.
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/home/app/data //数据目录
dataLogDir=/home/app/log //日志目录
# 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
# servers
server.1=192.168.1.2:2888:3888 //指定整个zookeeper集群的server编号、地址和端口
server.2=192.168.1.3:2888:3888
server.3=192.168.1.4:2888:3888
完成后我们将其中配置文件拷贝到另外两台机器上(在第一台机器上操作,即192.168.1.2):
scp /home/app/zookeeper-3.4.11/conf/zoo.cfg root@192.168.1.3:/home/app/zookeeper-3.4.11/conf/
scp /home/app/zookeeper-3.4.11/conf/zoo.cfg root@192.168.1.4:/home/app/zookeeper-3.4.11/conf/
现在最重要的一个步骤到了。还记得我们在配置文件中给出的server列表都有一个编号吗?我们需要为这三个节点创建对应的编号文件,在/home/app/data/myid文件中。如下:
server.1=192.168.1.2:2888:3888,所以在192.168.1.2这台机器上执行:
echo 1 > /home/app/data/myid
server.2=192.168.1.3:2888:3888,所以在192.168.1.3这台机器上执行:
echo 2 > /home/app/data/myid
server.3=192.168.1.4:2888:3888,所以在192.168.1.4这台机器上执行:
echo 3 > /home/app/data/myid
至此,大功告成,我们准备开始启动了。分别在三台机器上执行(注意,执行不分先后):
/home/app/zookeeper-3.4.11/bin/zkServer.sh start
出现的结果如下(可能您的结果状态和我的会不一样)
192.168.1.2:
zkServer.sh status
JMX enabled by default
Using config: /home/app/zookeeper-3.4.11/conf/zoo.cfg
Mode: follower
192.168.1.3:
zkServer.sh status
JMX enabled by default
Using config: /home/app/zookeeper-3.4.11/conf/zoo.cfg
Mode: leader
192.168.1.4:
zkServer.sh status
JMX enabled by default
Using config: /home/app/zookeeper-3.4.11/conf/zoo.cfg
Mode: follower
在一个zookeeper集群集群中,始终有一个节点会通过集群所有节点参与的选举被推举为“leader”节点。其他节点就是“follower”节点。
安装配置activemq-5.13.5(以下操作三台机器都要):
cd /home/app
wget http://archive.apache.org/dist/activemq/5.13.5/apache-activemq-5.13.5-bin.tar.gz
tar -zxvf apache-activemq-5.13.5-bin.tar.gz
cd apache-activemq-5.13.5
以下操作在第一台机器,即192.168.1.2:
vi conf/activemq.xml
1、找到 <broker xmlns="http://activemq.apache.org/schema/core" brokerName="test" dataDirectory="${activemq.data}">
将brokerName的值改为自己需要的字符串(我这里是test);要特别注意这里,这个brokerName的值必须三台activemq一样(这样zookeeper才会认为是一个集群的)
2、找到<persistenceAdapter>
<kahaDB directory="${activemq.data}/kahadb"/>
</persistenceAdapter>
修改为:
<persistenceAdapter>
<replicatedLevelDB
directory="${activemq.data}/leveldb"
replicas="3" <!--这里有三台所以就3了-->
bind="tcp://0.0.0.0:0"
zkAddress="192.168.1.2:2181,192.168.1.3:2181,192.168.1.4:2181"
hostname="192.168.1.2" <!--当前机器的ip-->
zkPath="/activemq/leveldb-stores"
sync="local_disk"/>
</persistenceAdapter>
另外两台服务器的配置与第一台配置相似,唯一不同就是hostname="192.168.1.2"这里,需要写入各自的ip地址.
启动activemq服务器(三台都要)
cd /home/app/apache-activemq-5.13.5/bin
./activemq start
三台服务器按照上面方法一次启动.
验证:
然后,在浏览器地址栏里输入:
http://192.168.1.2:8161/admin/queues.jsp
http://192.168.1.3:8161/admin/queues.jsp
http://192.168.1.4:8161/admin/queues.jsp
因为使用zookeeper做负载均衡,三台只有一台是master,其他两台处于等待状态,所以只有其中一台提供服务,但一旦这台服务器宕机以后,会有另外一台顶替上来,所以其他几个ip地址是打不开的,只有一台能打开.
Be First to Comment