网上的教程真的是凤毛麟角,就不想说啥了,一次一次把我带入坑。
好了关于Zookeeper的搭建已经说好了,本文说说基于Zookeeper的MQ集群。
第一步、将mq安装包上传到CentOS7,并解压
我这里有个 /mysoft 的路径,用来存放安装包的。执行命令:
[root@localhost mysoft]# ll total 272132 -rw-r--r--. 1 root root 58075730 Jul 21 17:35 apache-activemq-5.15.0-bin.tar.gz -rw-r--r--. 1 root root 185540433 Jul 21 18:30 jdk-8u131-linux-x64.gz -rw-r--r--. 1 root root 35042811 Jul 21 18:03 zookeeper-3.4.10.tar.gz [root@localhost mysoft]# tar -zxvf apache-activemq-5.15.0-bin.tar.gz -C /usr/local
我这里解压到了/usr/local 目录下。
第二步、修改配置文件
小插曲: 当我没有配置ActiveMQ的环境变量时,会出现(两个/) [root@localhost conf]# cd ../ [root@localhost apache-activemq-5.15.0]# cd bin [root@localhost bin]# ./activemq start INFO: Loading '/usr/local/apache-activemq-5.15.0//bin/env' INFO: Using java '/usr/local/java/jdk/jdk1.8.0_131/bin/java' INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details INFO: pidfile created : '/usr/local/apache-activemq-5.15.0//data/activemq.pid' (pid '7127') 所以我就配了个环境变量, vi /etc/profile export ACTIVEMQ_HOME=/usr/local/apache-activemq-5.15.0
之后就没有两个/了
找到配置文件
cd /usr/local/apache-activemq-5.15.0/conf
vi activemq.xml
1. 先修改节点名称,这里三台机器一致
2. 修改persistenceAdapter
节点一:
节点二:
节点三:
3. 提个醒
第三步、开放端口
2. 端口说明
Zookeeper 2181 – the port that clients will use to connect to the ZK ensemble 2888 – port used by ZK for quorum election 3888 – port used by ZK for leader election
ActiveMQ 61616 – 消息端口(服务端口) # 默认的 8161 – 控制台端口 # 默认的 62621 – 集群通信端口
centos7命令开放端口--重启防火墙--查看端口:
[root@localhost conf]# firewall-cmd --zone=public --add-port=62621/tcp --permanent success [root@localhost conf]# firewall-cmd --zone=public --add-port=61616/tcp --permanent success [root@localhost conf]# firewall-cmd --zone=public --add-port=8161/tcp --permanent success [root@localhost conf]# firewall-cmd --zone=public --add-port=2888/tcp --permanent success [root@localhost conf]# firewall-cmd --zone=public --add-port=3888/tcp --permanent success [root@localhost conf]# firewall-cmd --zone=public --add-port=2181/tcp --permanent success
[root@localhost conf]# systemctl restart firewalld
[root@localhost conf]# firewall-cmd --zone=public --list-ports
61616/tcp 3888/tcp 2181/tcp 2888/tcp 8161/tcp 62621/tcp
第四步、启动Zookeeper,启动MQ
Zookeeper的启动我就不说了,我写的那篇文章有。
先定位到ActiveMQ的bin目录。然后
./activemq start
节点一:
节点二:
节点三:
那么有没有启动成功呢?
先看看ActiveMQ的启动日志,有没有异常的东西
[root@localhost conf]# cd ../ [root@localhost apache-activemq-5.15.0]# cd data/ [root@localhost data]# tail activemq.log 2017-07-23 18:37:55,804 | WARN | unprocessed event state: AuthFailed | org.apache.activemq.leveldb.replicated.groups.ZKClient | main-EventThread 2017-07-23 18:37:55,906 | INFO | Socket connection established to 192.168.127.131/192.168.127.131:2181, initiating session | org.apache.zookeeper.ClientCnxn | main-SendThread(192.168.127.131:2181) 2017-07-23 18:37:56,012 | INFO | Session establishment complete on server 192.168.127.131/192.168.127.131:2181, sessionid = 0x25d71f9f3ed0000, negotiated timeout = 4000 | org.apache.zookeeper.ClientCnxn | main-SendThread(192.168.127.131:2181) 2017-07-23 18:37:58,115 | WARN | listeners are taking too long to process the events | org.apache.activemq.leveldb.replicated.groups.ChangeListenerSupport | main-EventThread 2017-07-23 18:37:58,117 | WARN | listeners are taking too long to process the events | org.apache.activemq.leveldb.replicated.groups.ChangeListenerSupport | main-EventThread 2017-07-23 18:37:58,299 | INFO | Using the pure java LevelDB implementation. | org.apache.activemq.leveldb.LevelDBClient | ActiveMQ BrokerService[activemqCluster] Task-1 2017-07-23 18:37:58,456 | INFO | Attaching to master: tcp://192.168.127.129:62621 | org.apache.activemq.leveldb.replicated.SlaveLevelDBStore | ActiveMQ BrokerService[activemqCluster] Task-1 2017-07-23 18:37:58,489 | INFO | Slave started | org.apache.activemq.leveldb.replicated.MasterElector | ActiveMQ BrokerService[activemqCluster] Task-1 2017-07-23 18:37:59,698 | INFO | Attaching... Downloaded 0.00/0.00 kb and 1/1 files | org.apache.activemq.leveldb.replicated.SlaveLevelDBStore | hawtdispatch-DEFAULT-1 2017-07-23 18:37:59,700 | INFO | Attached | org.apache.activemq.leveldb.replicated.SlaveLevelDBStore | hawtdispatch-DEFAULT-1
可以看出并没有。
再执行一下zkCli.sh ,看看有没有我们配的节点。
[root@localhost data]# cd /usr/local [root@localhost local]# zkCli.sh Connecting to localhost:2181 2017-07-23 19:16:38,378 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT 2017-07-23 19:16:38,513 [myid:] - INFO [main:Environment@100] - Client environment:host.name=localhost 2017-07-23 19:16:38,513 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_131 2017-07-23 19:16:38,561 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2017-07-23 19:16:38,561 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/java/jdk/jdk1.8.0_131/jre 2017-07-23 19:16:38,562 [myid:] - INFO ...省略 2017-07-23 19:16:38,562 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2017-07-23 19:16:38,562 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 2017-07-23 19:16:38,562 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2017-07-23 19:16:38,562 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 2017-07-23 19:16:38,562 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-327.el7.x86_64 2017-07-23 19:16:38,562 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root 2017-07-23 19:16:38,562 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root 2017-07-23 19:16:38,562 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local 2017-07-23 19:16:38,563 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@506c589e 2017-07-23 19:16:38,726 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) Welcome to ZooKeeper! JLine support is enabled 2017-07-23 19:16:39,013 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session 2017-07-23 19:16:39,077 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x15d71f9f3e00000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0] ls / [activemq, zookeeper] [zk: localhost:2181(CONNECTED) 1] ls /activemq [leveldb-stores] [zk: localhost:2181(CONNECTED) 2] ls /activemq/leveldb-stores [00000000006, 00000000007, 00000000005] [zk: localhost:2181(CONNECTED) 3] quit Quitting... 2017-07-23 19:17:24,754 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x15d71f9f3e00000 closed 2017-07-23 19:17:24,757 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x15d71f9f3e00000 [root@localhost local]#
可以看到
[00000000006, 00000000007, 00000000005]
,之所以不是1,2,3,可能是我前几次没成功导致的吧,毕竟掉了几次坑。。。
你在这三台机器上都可以看到这三个,就证明ok了。
最后直观一点:打开浏览器。分别访问
http://192.168.127.129:8161/admin/
http://192.168.127.130:8161/admin/
http://192.168.127.131:8161/admin/
会发现只有一个提供服务。
------------------------------
-----------------------------------
现在我们停掉129的MQ
[root@localhost local]# cd apache-activemq-5.15.0/bin/ [root@localhost bin]# ./activemq stop INFO: Loading '/usr/local/apache-activemq-5.15.0/bin/env' INFO: Using java '/usr/local/java/jdk/jdk1.8.0_131/bin/java' INFO: Waiting at least 30 seconds for regular process termination of pid '7141' : Java Runtime: Oracle Corporation 1.8.0_131 /usr/local/java/jdk/jdk1.8.0_131/jre Heap sizes: current=63360k free=62311k max=1013632k JVM args: -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=/usr/local/apache-activemq-5.15.0/conf/login.config -Dactivemq.classpath=/usr/local/apache-activemq-5.15.0/conf:/usr/local/apache-activemq-5.15.0/../lib/: -Dactivemq.home=/usr/local/apache-activemq-5.15.0 -Dactivemq.base=/usr/local/apache-activemq-5.15.0 -Dactivemq.conf=/usr/local/apache-activemq-5.15.0/conf -Dactivemq.data=/usr/local/apache-activemq-5.15.0/data Extensions classpath: [/usr/local/apache-activemq-5.15.0/lib,/usr/local/apache-activemq-5.15.0/lib/camel,/usr/local/apache-activemq-5.15.0/lib/optional,/usr/local/apache-activemq-5.15.0/lib/web,/usr/local/apache-activemq-5.15.0/lib/extra] ACTIVEMQ_HOME: /usr/local/apache-activemq-5.15.0 ACTIVEMQ_BASE: /usr/local/apache-activemq-5.15.0 ACTIVEMQ_CONF: /usr/local/apache-activemq-5.15.0/conf ACTIVEMQ_DATA: /usr/local/apache-activemq-5.15.0/data ...Connecting to pid: 7141 ..Stopping broker: activemqCluster ... TERMINATED [root@localhost bin]#
发现刚才不可以访问的130可以访问了
到这里Zookeeper+ActiveMQ集群搭建好了,关于负载均衡以后更新