ActiveMQ基于zookeeper+leveldb主从配置

以下为官网:http://activemq.apache.org/clustering.html 页面,主从复制就是根据当前页面中的Master Slave配置的。


ZooKeeper 怎么看主从 zookeeper主从配置_zookeeper

官网主从配置的方式以下三种 
1. 通过共享文件系统,众多brokers争夺文件系统的使用权,谁争夺到了,谁就是Master。 
2. JDBC的主从,跟文件共享系统差不多,只不过持久化为数据库。 
3. zookeeper+leveldb的主从,用它只是因为官网上的那句Very fast。


稍微说一下此时的环境,我是在一台公网机器上配置了三个zookeeper和三个activerMQ,所以内网IP都是一样的,利用不同的端口区分它们。
三台MQ要用到的端口分别为:
    61616  8161 
    61617  8162 
    61618  8163
三台ZK用到的端口分别为2181,2182,2183

部署步骤如下

  1. 在home下mkdir文件夹,在其内分别创建三个server文件夹予以区分。 
    zookeeper下载地址:http://zookeeper.apache.org/releases.html  activeMQ 下载地址:http://activemq.apache.org/download.html  在每个server文件夹内,分别上传压缩包并解压。而且还创建了data和dataLog文件夹准备存放zookeeper的数据和日志文件。
[root@localhost ZooKeeper]# ll
总用量 12
drwxr-xr-x. 7 root root 4096 12月  7 16:28 server1
drwxr-xr-x. 7 root root 4096 12月  7 15:29 server2
drwxr-xr-x. 7 root root 4096 12月  7 15:29 server3
[root@localhost ZooKeeper]# cd server1/
[root@localhost server1]# ll
总用量 11568
drwxr-xr-x. 11 root root     4096 12月  7 16:28 apache-activemq-5.12.1
drwxr-xr-x.  3 root root       60 12月  7 17:33 data
drwxr-xr-x.  3 root root       22 12月  8 14:22 dataLog
drwxr-xr-x.  2 root root        6 12月  7 14:51 logs
drwxr-xr-x. 10 1000 1000     4096 7月  29 2012 zookeeper-3.3.6
-rw-r--r--.  1 root root 11833706 12月  7 14:52 zookeeper-3.3.6.tar.gz

进入data目录,创建一个myid的文件,里面写入一个数字,比如我这个是server1,那么就写一个1,server2对应myid文件就写入2,server3对应myid文件就写个3

[root@localhost server1]# cd data
[root@localhost data]# ll
总用量 8
-rw-r--r--. 1 root root  2 12月  7 17:33 myid
drwxr-xr-x. 2 root root 73 12月  8 14:20 version-2
-rw-r--r--. 1 root root  4 12月  8 14:41 zookeeper_server.pid
[root@localhost data]# cat myid 
1

进入zookeeper-3.3.6的conf文件夹下,mv zoo_sample.cfg 为zoo.cfg.看一下我server1中的zoo.cfg的配置

1 # The number of milliseconds of each tick
  2 tickTime=2000
  3 # The number of ticks that the initial 
  4 # synchronization phase can take
  5 initLimit=10
  6 # The number of ticks that can pass between 
  7 # sending a request and getting an acknowledgement
  8 syncLimit=5
  9 # the directory where the snapshot is stored.

 10 dataDir=/home/ZooKeeper/server1/data
 11 dataLogDir=/home/ZooKeeper/server1/dataLog
 12 # the port at which the clients will connect
 13 clientPort=2181
 14 server.1=192.168.0.209:2881:3881
 15 server.2=192.168.0.209:2882:3882
 16 server.3=192.168.0.209:2883:3883

dataDir中,dataLogDir的路径分别是自己文件夹的路径。 
还有就是clientPort也要改成不同的端口。三台ZK用到的端口分别为2181,2182,2183 
server.X中的X对应着就是上边data/myid中的数字。

其他两台跟此大体相同,只不过注意dataDir,dataLogDir的路径及clientProt的端口号,还有data/myid中的值。

2.启动zookeeper并观察日志。 
分别进入三个zookeeper中的bin进行启动

[root@localhost bin]# ./zkServer.sh start
[root@localhost bin]# tail -f zookeeper.out

一定要去看日志,避免启动不正常。

3.配置三个service文件夹中的ActiveMQ

首先分别更改三个MQ的端口,涉及到的配置文件为apache-activemq-5.12.1/conf 
中的activemq.xml,jetty.xml,因为我这是在一台机器上,如果你是三台,那么久不用改了,通过Ip就可以区分了。

vim activemq.xml 
server2中将61616改为61617,server3改为61618
<transportConnectors>
119             <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
120             <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
121             <transportConnector name="amqp" uri="amqp://0.0.0.0:5472?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
122             <transportConnector name="stomp" uri="stomp://0.0.0.0:60613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
123             <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1783?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>

124             <transportConnector name="ws" uri="ws://0.0.0.0:60614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
125         </transportConnectors>
vim jetty.xml 
将web界面端口同理分别改为8162,8163
<property name="port" value="8161"/>

4.更改activema.xml文件,与zookeeper关联上。

<persistenceAdapter>
 82          <replicatedLevelDB
 83       directory="${activemq.data}/levelDB"
 84       replicas="3"
 85       bind="tcp://0.0.0.0:0"
 86       zkAddress="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"
 87       zkPath="/activemq/leveldb-stores"
 88       hostname="127.0.0.1"
 89       />
 90         </persistenceAdapter>

注意修改persistenceAdapter节点

  • replicas:集群中节点的数量,这里配置了三台就是3
  • bind:当这个节点成为主节点后,就会默认绑定当前IP和端口
  • zkAddress:三台zookeeper的服务ip和端口,用逗号隔开
  • zkPath:默认不用改
  • hostname:这里应该填写三台zookeeper可以互相访问的地址,我因为用的是一台机器,所以写的127.0.0.1,如果你部署的是三台内网,这里就写相应的那台机器的内网IP。

三台mq都配置完之后###,

  1. 进入/home/ZooKeeper/server1/apache-activemq-5.12.1/bin目录

启动三台mq,一台一台启动的时候也要查active的log

/home/ZooKeeper/server2/apache-activemq-5.12.1/data
[root@localhost data]# tail -fn 100 activemq.log

如果到这里启动没什么问题的话,当你启动第一台mq的时候,zookeeper的日志就会打出英文的日志 
告诉你一台activemq不够,至少两台才能形成Master/Slave。

之后可以在浏览器上分别访问三台activeMq的web管理后台8161,8162,8163 这种主从的方式成功的话,只会有一台是master,三个端口也只能有一个可以访问,其他两个都是等待状态。
[root@localhost data]# netstat -an |grep 61617
tcp6       0      0 :::61617                :::*                    LISTEN     
tcp6       0      0 10.10.0.186:61617       58.132.171.233:55133    ESTABLISHED
tcp6       0      0 127.0.0.1:61617         127.0.0.1:58332         ESTABLISHED
tcp6       0      0 127.0.0.1:58332         127.0.0.1:61617         ESTABLISHED
[root@localhost data]# netstat -an |grep 8162
tcp6       0      0 :::8162                 :::*                    LISTEN     
[root@localhost data]#

同时通过查看端口也可以发现,我三台中,server2的MQ是MASTER,也可以通过访问 
http://IP:8161/admin/queues.jsp 
http://IP:8162/admin/queues.jsp 
http://IP:8163/admin/queues.jsp 
进行访问。注意:发现三个网站中同时只能其中被选举为master的才能访问。

最后一步

你需要将客户端的链接改为failover的方式,主备援救的方式,采用此种方式后,如果其中一台broker 死了,客户点会自动切换到新生成的那台主broker上。

mqUrl=failover:(tcp://IP:61616,tcp://IP:61617,tcp://IP:61618)

这个时候,就可以手动kill Master broker ,看看客户端会不会自动切换。

最后提醒的就是上述方式的集群可以自动切换,但是必须同时至少有两个ActiveMQ正常执行,集群方式才可以有效。