1. 程式人生 > >ActiveMQ之brokers network集群

ActiveMQ之brokers network集群

自動 聯合 pla src 才會 ref disco -c dap

一、集群配置方式類型

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連接密碼

二、集群實例

技術分享圖片

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

ActiveMQ之brokers network集群