一、集群配置方式类型

master-slave

<networkConnectors>
  <networkConnector uri="masterslave:(tcp://host1:61616,tcp://host2:61616,tcp://..)"/>
</networkConnectors>
  • uri列表中对应的顺序就是MASTER,SLAVE1,SLAVE2...SLAVE;
  • master-slave的作用就是brokers之间的主从,同一时刻只能连接一个broker。通常先连接master,如果master连接不上,则连接slave;
  • brokers之间的消息同步机制是通过<persistenceAdapter>共享的存储方式来共享数据的,如:jdbc/kahaDB/replicaLevelDB(zookeeper)。同时,brokers之间也是通过获取数据存储的访问权来获取broker的主导权(即哪个broker激活)。
  • zookeeper做masterslave时,brokers的名字必须一致,否则zookeeper会认为不符合2n+1的规则。
  • masterslave这种配置既可以在activeMQ.xml中配置,也可以在client端使用。通常建议在client使用。虽然master-slave可以提高可用性,但是负载均衡的功能减弱,并且很多时候会浪费slave broker服务器。

broker static discover

<networkConnectors>
  <networkConnector uri="static:(tcp://host1:61616,tcp://host2:61617,tcp://..)"/>
</networkConnectors>

表示当前local broker与remote broker(61616/61617)建立的单向网络连接。即local broker可以将消息传递给remote broker,但反过来就不行。duplex="true" 属性配置可以开启双向通信。

broker multicast discover

<networkConnectors>
   <networkConnector uri="multicast://default"/>
</networkConnectors>

<transportConnector uri="tcp://localhost:0" discoveryUri="multicast://default"/>

组播发现会自动发现同网络中的broker并进行连接通信。注意:必须是同网络中。
在实际的配置很少这样配置,因为无法保证哪些brokers建立了连接。对于master-slave配置也不是很好。

dynamicallyIncludedDestinations

<dynamicallyIncludedDestinations>
     <queue physicalName="include.test.foo"/>
     <topic physicalName="include.test.bar"/>
</dynamicallyIncludedDestinations>

当networkConnector中连接的broker有customers时,那么对应的destinations才会传递到对应的broker。否则,不传递。这中方式对于point-to-point的方式就比较合适。

staticallyIncludedDestinations

<staticallyIncludedDestinations>
     <queue physicalName="always.include.queue"/>
     <topic physicalName="always.include.topic"/>
</staticallyIncludedDestinations>

不管networkConnector中连接的broker有无customers,都会讲消息发送到每个broker去。这种方式比较适合topic的消息方式。

excludedDestinations

<excludedDestinations>
     <queue physicalName="exclude.test.foo"/>
     <topic physicalName="exclude.test.bar"/>
</excludedDestinations>

networkConnector中指定某些detination不会发送到remote brokers.

networkConnector中uri的参数

uri="static:(tcp://host1:61616,tcp://host2:61616)?maxReconnectDelay=5000&useExponentialBackOff=false"

initialReconnectDelay:重连的时间间隔。
maxReconnectDelay:最大重连时间。即initialReconnectDelay次数
useExponentialBackOff:一次连接失败,后面的重连时间是否依次递增。
backOffMultiplier:当useExponentialBackOff=true,重连时间的间隔的倍数,即initialReconnectDelay
backOffMultiplier

networkConnector属性配置

name:连接名称,多个时必须唯一。
dynamicOnly:
decreaseNetworkConsumerPriority:
networkTTL:
messageTTL:
consumerTTL:
conduitSubscriptions:管道订阅。即一个broker有多个customers,也只会认为是一个customer.
excludedDestinations:
dynamicallyIncludedDestinations:
useVirtualDestSubs:
staticallyIncludedDestinations:
duplex:是否开启双工通信
prefetchSize:
suppressDuplicateQueueSubscriptions:
bridgeTempDestinations:
alwaysSyncSend:
staticBridge:静态连接桥。与staticallyIncludedDestinations联合使用
userName:networkConnector连接用户名
password:networkConnector连接密码

二、集群实例

ActiveMQ之brokers network集群

broker-master

<networkConnectors>
    <networkConnector name="connector_master_slave" uri="static:(tcp://localhost:61618,tcp://localhost:61619)" 
                           userName="admin" password="admin123"/>
   <networkConnector name="connector_brokers" uri="static:(tcp://localhost:61620,tcp://localhost:61621)"                         userName="admin" password="admin123">
     <dynamicallyIncludedDestinations>
          <queue physicalName=">"/>
     </dynamicallyIncludedDestinations>
     <staticallyIncludedDestinations>
           <topic physicalName=">"/>
     </staticallyIncludedDestinations>
   </networkConnector>
</networkConnectors>

<transportConnector name="openwire" uri="tcp://0.0.0.0:61617?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>

broker-slave01

<networkConnectors>
    <networkConnector name="connector_master_slave" uri="static:(tcp://localhost:61617,tcp://localhost:61619)"              userName="admin" password="admin123"/>
    <networkConnector name="connector_brokers" uri="static:(tcp://localhost:61620,tcp://localhost:61621)"                            userName="admin" password="admin123">
       <dynamicallyIncludedDestinations>
             <queue physicalName=">"/>
       </dynamicallyIncludedDestinations>
       <staticallyIncludedDestinations>
             <topic physicalName=">"/>
       </staticallyIncludedDestinations>
    </networkConnector>
</networkConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61618?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>

broker-slave02

<networkConnectors>
   <networkConnector name="connector_master_slave" 
                           uri="static:(tcp://localhost:61617,tcp://localhost:61618)" userName="admin" password="admin123"/>
   <networkConnector name="connector_brokers" uri="static:(tcp://localhost:61620,tcp://localhost:61621)"                          userName="admin" password="admin123">
          <dynamicallyIncludedDestinations>
                   <queue physicalName=">"/>
          </dynamicallyIncludedDestinations>
          <staticallyIncludedDestinations>
                   <topic physicalName=">"/>
          </staticallyIncludedDestinations>
     </networkConnector>
</networkConnectors>

broker-cluster01

<networkConnectors>
     <networkConnector uri="static:(tcp://localhost:61621)" duplex="false" name="connector_broker_normal"                            userName="admin" password="admin123">
         <dynamicallyIncludedDestinations>
                   <queue physicalName=">" />
                   <topic physicalName=">" />
         </dynamicallyIncludedDestinations>
     </networkConnector>
</networkConnectors>

<transportConnector name="openwire" uri="tcp://0.0.0.0:61620?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>

broker-cluster02

<networkConnectors>
     <networkConnector uri="static:(tcp://localhost:61620)" duplex="false" name="connector_broker_normal"                           userName="admin" password="admin123">
         <dynamicallyIncludedDestinations>
                 <queue physicalName=">" />
                 <topic physicalName=">" />
         </dynamicallyIncludedDestinations>
     </networkConnector>
</networkConnectors>

<transportConnector name="openwire"uri="tcp://0.0.0.0:61621?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>

brokers的安全配置

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="locations">
         <value>file:${activemq.conf}/credentials.properties</value>
    </property>
</bean>

<plugins> 
   <simpleAuthenticationPlugin> 
       <users> 
         <authenticationUser username="${activemq.username}" password="${activemq.password}"  groups="admins"/>
        </users> 
   </simpleAuthenticationPlugin> 

        <authorizationPlugin> 
          <map> 
           <authorizationMap> 
            <authorizationEntries>
             <authorizationEntry queue=">" read="admins" write="admins" admin="admins" />
             <authorizationEntry topic=">" read="admins" write="admins" admin="admins"/>
            </authorizationEntries>
            <tempDestinationAuthorizationEntry>
             <tempDestinationAuthorizationEntry read="admins" write="admins" admin="admins"/>
            </tempDestinationAuthorizationEntry>
           </authorizationMap> 
          </map> 
   </authorizationPlugin>
</plugins>

参考文档:
http://activemq.apache.org/networks-of-brokers.html
http://shift-alt-ctrl.iteye.com/blog/2070531
https://www.cnblogs.com/shihaiming/p/6018916.html