今天就跟大家聊聊有关ActiveMQ中的主从怎么利用zookeeper搭建,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
1、环境准备
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复制一份为 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=2000initLimit=10syncLimit=5dataDir=/tmp/zookeeperclientPort=2181 server.1=192.168.3.10:2888:3888server.2=192.168.3.11:2888:3888server.3=192.168.3.11:2888:3888
(3)创建/tmp/zookeeper目录
在该目录下创建名为myid的文件,内容为1(这个值随server而改变)
(4)将server1上的/home/wzh/zk/zookeeper-3.5.2-alpha文件夹复制到server2,server3,然后创建/tmp/zookeeper目录
在该目录下创建名为myid的文件,内容为2
(5)启动zookeeper
[192.168.3.10]
wzh@hd-master:~/zk/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh startZooKeeper JMX enabled by defaultUsing config: /home/wzh/zk/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfgStarting zookeeper ... STARTED
[192.168.3.11]
wzh@hd-slave1:~/zk/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh startZooKeeper JMX enabled by defaultUsing config: /home/wzh/zk/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfgStarting zookeeper ... STARTED
[192.168.3.12]
wzh@hd-slave2:~/zk/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh startZooKeeper JMX enabled by defaultUsing config: /home/wzh/zk/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfgStarting zookeeper ... STARTED
搭建ActiveMQ集群
(1)将apache-activemq-5.13.0-bin.tar.gz解压到/home/wzh/amq
(2)修改activemq.xml配置文件
【1】将broker节点的brokerName设置为wzhamq
复制代码 代码如下:
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="wzhamq" dataDirectory="${activemq.data}">
【2】将persistenceAdapter的持久化方式选用replicatedLevelDB,将kahaDB方式注释掉
<persistenceAdapter> <!-- <kahaDB directory="${activemq.data}/kahadb"/> --> <replicatedLevelDB directory="${activemq.data}/leveldb" replicas="3" bind="tcp://0.0.0.0:0" zkAddress="192.168.3.10:2181,192.168.3.11:2181" hostname="192.168.3.10" sync="local_disk" zkPath="/activemq/leveldb-stores"/> </persistenceAdapter>
将apache-activemq-5.13.复制到11,12机器
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)启动ActiveMQ
wzh@hd-master:~/amq$ ./apache-activemq-5.13.0/bin/activemq statusINFO: 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$
依次启动192.168.3.11,192.168.3.12机器
集群管理
(1)通过使用ZooInspector工具查看zookeeper集群情况
(2)http://192.168.3.10:8161/admin/ 默认用户名与口令为admin登录ActiveMQ管理端
通过Spring-boot操作ActiveMQ JMS
(1)通过gradle构建Spring-boot应用,在 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=truespring.activemq.pool.enabled=falsespring.activemq.user=adminspring.activemq.password=admin
(3)JMS消息发送
@Servicepublic class Producer { @Autowired private JmsMessagingTemplate jmsTemplate; public void sendMessage(Destination destination, final String message){ jmsTemplate.convertAndSend(destination, message); }}
(4)JMS消息接收
@Componentpublic class Consumer { @JmsListener(destination = "test.queue") public void receiveQueue(String text){ System.out.println("Consumer收到的报文为:"+text); }}
(5)测试
@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 Map<String, Object> say(@PathVariable String msg, @PathVariable String name){ Map<String, Object> map = new HashMap<>(); map.put("msg", msg); map.put("name", name); producer.sendMessage(destination, msg); return map; }}
(6)进入ActiveMQ管理控制台创建一个消息队列
test.queue
(7)通过POSTMAN进行测试
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收到的报文为: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收到的报文为:hello
(8)目前系统连接的是10,如果此时将10集群Down掉,系统会理解选择一台slave作为master提供服务,从而启动案例主备的效果。
看完上述内容,你们对ActiveMQ中的主从怎么利用zookeeper搭建有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。