在一个网络内运行多个brokers或者stand alone brokers时存在一个问题,这就是消息在物理上只被一个broker持有,因此当某个broker失效,那么你只能等待直到它重启后,这个broker上的消息才能够被继续发送(如果没有设置持久化,那么在这种情况下,消息将会丢失)。Master Slave 背后的想法是,消息被复制到slave broker,因此即使master broker遇到了像硬件故障之类的错误,你也可以立即切换到slave broker而不丢失任何消息。
接下来介如何配置
解压两份activemq于不同的目录,其中一个broker命名为master,一个为slave
1. master端不做处理
2. slave: activemq.xml中配置如下
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- START SNIPPET: example --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <!-- Allows us to use system properties as variables in this configuration file --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>file:${activemq.base}/conf/credentials.properties</value> </property> </bean> <broker brokerName="slave" useJmx="false" deleteAllMessagesOnStartup="true" xmlns="http://activemq.apache.org/schema/core"> <services> <masterConnector remoteURI= "tcp://0.0.0.0:61616" userName="user" password="password"/> </services> <transportConnectors> <transportConnector uri="tcp://0.0.0.0:61617"/> </transportConnectors> </broker> <!-- Enable web consoles, REST and Ajax APIs and demos It also includes Camel (with its web console), see ${ACTIVEMQ_HOME}/conf/camel.xml for more info Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details --> <import resource="jetty.xml"/> </beans> <!-- END SNIPPET: example -->
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"system",
"manager", "failover:(tcp://0.0.0.0:61616,tcp://0.0.0.0:61617)");
// JMS 客户端到JMS Provider 的连接
Connection connection = connectionFactory.createConnection();
connection.start();
启动slave后,master 会打印 active slave broker slave is attached
当关闭master 后,slave控制台会打印: Connector tcp://0.0.0.0:61617 Started
OK!
注:
1. slave将会在启动的时候去连接master,所以要先启动master
2. 除非master失效, 否则slave不会进行任何的传输或者是连接。
3. slave端 的broker 可以配置 一个额外的参数 shutdownOnMasterFailure, 设为true的话会在master失效的时候自动关闭
4.master端的参数: waitForslave: 设为true的意义 当master失效的时候,在slave工作之前,不允许有任何客户端或网络的连接
shutdownOnSlaveFailure : if ture ,the master will shutdown if a slave becomes detached. This ensures that a slave is only ever in sync with the master