今天就跟大家聊聊有關(guān)ActiveMQ中的主從怎么利用zookeeper搭建,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
十年建站經(jīng)驗(yàn), 做網(wǎng)站、網(wǎng)站制作客戶的見(jiàn)證與正確選擇。創(chuàng)新互聯(lián)提供完善的營(yíng)銷型網(wǎng)頁(yè)建站明細(xì)報(bào)價(jià)表。后期開(kāi)發(fā)更加便捷高效,我們致力于追求更美、更快、更規(guī)范。1、環(huán)境準(zhǔn)備
IP
192.168.3.10 server1
192.168.3.11 server2
192.168.3.12 server3
安裝軟件信息:
apache-activemq-5.13.0-bin.tar.gz
zookeeper-3.5.2-alpha.tar.gz
ZooInspector.zip
2、搭建Zookeeper集群
(1)將zookeeper-3.5.2-alpha.tar.gz文件解壓到/home/wzh/zk目錄;
(2)將zoo_sample.cfg復(fù)制一份為 zoo.cfg,并修改其配置信息
wzh@hd-master:~/zk/zookeeper-3.5.2-alpha/conf$ cp zoo_sample.cfg zoo.cfg
wzh@hd-master:~/zk/zookeeper-3.5.2-alpha/conf$vim zoo.cfg
tickTime=2000 initLimit=10 syncLimit=5 dataDir=/tmp/zookeeper clientPort=2181 server.1=192.168.3.10:2888:3888 server.2=192.168.3.11:2888:3888 server.3=192.168.3.11:2888:3888
(3)創(chuàng)建/tmp/zookeeper目錄
在該目錄下創(chuàng)建名為myid的文件,內(nèi)容為1(這個(gè)值隨server而改變)
(4)將server1上的/home/wzh/zk/zookeeper-3.5.2-alpha文件夾復(fù)制到server2,server3,然后創(chuàng)建/tmp/zookeeper目錄
在該目錄下創(chuàng)建名為myid的文件,內(nèi)容為2
(5)啟動(dòng)zookeeper
[192.168.3.10]
wzh@hd-master:~/zk/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /home/wzh/zk/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
[192.168.3.11]
wzh@hd-slave1:~/zk/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /home/wzh/zk/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
[192.168.3.12]
wzh@hd-slave2:~/zk/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /home/wzh/zk/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
3、搭建ActiveMQ集群
(1)將apache-activemq-5.13.0-bin.tar.gz解壓到/home/wzh/amq
(2)修改activemq.xml配置文件
【1】將broker節(jié)點(diǎn)的brokerName設(shè)置為wzhamq
復(fù)制代碼 代碼如下:
【2】將persistenceAdapter的持久化方式選用replicatedLevelDB,將kahaDB方式注釋掉
將apache-activemq-5.13.復(fù)制到11,12機(jī)器
wzh@hd-master:~/amq$ scp -r apache-activemq-5.13.0/ wzh@192.168.3.11:/tmp
修改配置文件中的hostname="192.168.3.11"
修改配置文件中的hostname="192.168.3.12"
(3)啟動(dòng)ActiveMQ
wzh@hd-master:~/amq$ ./apache-activemq-5.13.0/bin/activemq status INFO: Loading '/home/wzh/amq/apache-activemq-5.13.0//bin/env' INFO: Using java '/opt/java/jdk1.8.0_91/bin/java' ActiveMQ is running (pid '2031') wzh@hd-master:~/amq$
依次啟動(dòng)192.168.3.11,192.168.3.12機(jī)器
4:集群管理
(1)通過(guò)使用ZooInspector工具查看zookeeper集群情況
(2)http://192.168.3.10:8161/admin/ 默認(rèn)用戶名與口令為admin登錄ActiveMQ管理端
5、通過(guò)Spring-boot操作ActiveMQ JMS
(1)通過(guò)gradle構(gòu)建Spring-boot應(yīng)用,在 gradle文件中增加
dependencies { compile('org.springframework.boot:spring-boot-starter-activemq') compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') }
(2)application中增加以下配置
spring.activemq.broker-url=failover:(tcp://192.168.3.10:61616,tcp://192.168.3.11:61616,tcp://192.168.3.12:61616) spring.activemq.in-memory=true spring.activemq.pool.enabled=false spring.activemq.user=admin spring.activemq.password=admin
(3)JMS消息發(fā)送
@Service public class Producer { @Autowired private JmsMessagingTemplate jmsTemplate; public void sendMessage(Destination destination, final String message){ jmsTemplate.convertAndSend(destination, message); } }
(4)JMS消息接收
@Component public class Consumer { @JmsListener(destination = "test.queue") public void receiveQueue(String text){ System.out.println("Consumer收到的報(bào)文為:"+text); } }
(5)測(cè)試
@RestController @RequestMapping( value = "/test", headers = "Accept=application/json", produces = "application/json;charset=utf-8" ) public class TestCtrl { @Autowired Producer producer; Destination destination = new ActiveMQQueue("test.queue"); @RequestMapping( value = "/say/{msg}/to/{name}", method = RequestMethod.GET ) public Mapsay(@PathVariable String msg, @PathVariable String name){ Map map = new HashMap<>(); map.put("msg", msg); map.put("name", name); producer.sendMessage(destination, msg); return map; } }
(6)進(jìn)入ActiveMQ管理控制臺(tái)創(chuàng)建一個(gè)消息隊(duì)列
test.queue
(7)通過(guò)POSTMAN進(jìn)行測(cè)試
2017-08-03 08:10:44.928 INFO 12820 --- [ActiveMQ Task-3] o.a.a.t.failover.FailoverTransport : Successfully reconnected to tcp://192.168.3.10:61616
2017-08-03 08:11:08.854 INFO 12820 --- [ActiveMQ Task-1] o.a.a.t.failover.FailoverTransport : Successfully connected to tcp://192.168.3.10:61616
Consumer收到的報(bào)文為:hello
2017-08-03 08:43:39.464 INFO 12820 --- [ActiveMQ Task-1] o.a.a.t.failover.FailoverTransport : Successfully connected to tcp://192.168.3.10:61616
Consumer收到的報(bào)文為:hello
(8)目前系統(tǒng)連接的是10,如果此時(shí)將10集群Down掉,系統(tǒng)會(huì)理解選擇一臺(tái)slave作為master提供服務(wù),從而啟動(dòng)案例主備的效果。
看完上述內(nèi)容,你們對(duì)ActiveMQ中的主從怎么利用zookeeper搭建有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。