修改 activemq.xml 配置文件java
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="zk_cluster_nodes" dataDirectory="${activemq.data}" schedulerSupport="true">
該配置的 brokerName 必須一致。node
將如下配置spring
<persistenceAdapter> <kahaDB directory="${activemq.base}/data/kahadb" /> </persistenceAdapter>
改成:apache
<persistenceAdapter> <replicatedLevelDB directory="${activemq.data}/leveldb" replicas="3" bind="tcp://0.0.0.0:0" zkAddress="192.168.1.2:2181,192.168.1.3:2181,192.168.1.4:2181" zkPassword="" hostname="192.168.10.2" sync="local_disk" zkPath="/activemq/leveldb-stores" /> </persistenceAdapter>
依次啓動3個節點,若出現錯誤:瀏覽器
INFO | No IOExceptionHandler registered, ignoring IO exception java.io.IOException: com.google.common.base.Objects.firstNonNull(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; at org.apache.activemq.util.IOExceptionSupport.create(IOExceptionSupport.java:39)[activemq-client-5.10.0.jar:5.10.0] at org.apache.activemq.leveldb.LevelDBClient.might_fail(LevelDBClient.scala:552)[activemq-leveldb-store-5.10.0.jar:5.10.0] at org.apache.activemq.leveldb.LevelDBClient.replay_init(LevelDBClient.scala:657)[activemq-leveldb-store-5.10.0.jar:5.10.0] at org.apache.activemq.leveldb.LevelDBClient.start(LevelDBClient.scala:558)[activemq-leveldb-store-5.10.0.jar:5.10.0] at org.apache.activemq.leveldb.DBManager.start(DBManager.scala:648)[activemq-leveldb-store-5.10.0.jar:5.10.0] at org.apache.activemq.leveldb.LevelDBStore.doStart(LevelDBStore.scala:235)[activemq-leveldb-store-5.10.0.jar:5.10.0] at org.apache.activemq.leveldb.replicated.MasterLevelDBStore.doStart(MasterLevelDBStore.scala:110)[activemq-leveldb-store-5.10.0.jar:5.10.0] at org.apache.activemq.util.ServiceSupport.start(ServiceSupport.java:55)[activemq-client-5.10.0.jar:5.10.0] at org.apache.activemq.leveldb.replicated.ElectingLevelDBStore$$anonfun$start_master$1.apply$mcV$sp(ElectingLevelDBStore.scala:226)[activemq-leveldb-store-5.10.0.jar:5.10.0] at org.fusesource.hawtdispatch.package$$anon$4.run(hawtdispatch.scala:330)[hawtdispatch-scala-2.11-1.21.jar:1.21] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[:1.7.0_79] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[:1.7.0_79] at java.lang.Thread.run(Thread.java:745)[:1.7.0_79] INFO | Stopped LevelDB[/home/local/activemq-5.10.0-3/data/leveldb]
先刪除 activemq 中 lib 目錄下 pax-url-aether-1.5.2.jar 文件,並將 activemq.xml 中該配置註釋服務器
<bean id="logQuery" class="org.fusesource.insight.log.log4j.Log4jLogQuery" lazy-init="false" scope="singleton" init-method="start" destroy-method="stop"> </bean>
3. 配置 springsession
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="failover:(tcp://192.168.1.2:61616,tcp://192.168.1.3:61626,tcp://192.168.1.4:61636)"></property> <property name="redeliveryPolicy"> <bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy"> <property name="maximumRedeliveries" value="5"/> <property name="initialRedeliveryDelay" value="100"/> </bean> </property> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="pubSubDomain" value="false"/> </bean>
4. 測試發送消息app
ApplicationContext app = new ClassPathXmlApplicationContext("broker.xml"); String queueName = "UUU9"; final String message = "bbbb"; JmsTemplate jmsTemplate = app.getBean(JmsTemplate.class); jmsTemplate.setDeliveryMode(1); for (int i = 0; i < 10; i++) { jmsTemplate.send(queueName, new MessageCreator() { public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage(message); textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 6000L); return textMessage; } }); Thread.sleep(200L); }
而後,在瀏覽器地址欄裏輸入:負載均衡
http://192.168.1.2:8161/admin/queues.jsp
jsp
http://192.168.1.3:8161/admin/queues.jsp
http://192.168.1.4:8161/admin/queues.jsp
由於使用zookeeper作負載均衡,三臺只有一臺是master,其餘兩臺處於等待狀態,因此只有其中一臺提供服務,但一旦這臺服務器宕機之後,會有另一臺頂替上來,因此其餘幾個ip地址是打不開的,只有一臺能打開