Artemis HA和群集无法正常工作

时间:2018-08-21 11:29:13

标签: activemq-artemis

下面是broker.xml中artemis集群(3个服务器)的设置

     <!-- Clustering configuration -->

      <broadcast-groups>
         <broadcast-group name="my-broadcast-group">
            <broadcast-period>5000</broadcast-period>
            <jgroups-file>test-jgroups-file_ping.xml</jgroups-file>
            <jgroups-channel>active_broadcast_channel</jgroups-channel>
            <connector-ref>netty-connector</connector-ref>
         </broadcast-group>
      </broadcast-groups>

      <discovery-groups>
         <discovery-group name="my-discovery-group">
            <jgroups-file>test-jgroups-file_ping.xml</jgroups-file>
            <jgroups-channel>active_broadcast_channel</jgroups-channel>
            <refresh-timeout>10000</refresh-timeout>
         </discovery-group>
      </discovery-groups>

      <cluster-connections>
         <cluster-connection name="my-cluster">
            <connector-ref>netty-connector</connector-ref>
            <retry-interval>500</retry-interval>
            <use-duplicate-detection>true</use-duplicate-detection>
            <message-load-balancing>STRICT</message-load-balancing>
            <max-hops>1</max-hops>
            <discovery-group-ref discovery-group-name="my-discovery-group"/>
         </cluster-connection>
      </cluster-connections>

  <ha-policy>
         <shared-store>
            <colocated>
               <backup-port-offset>100</backup-port-offset>
               <backup-request-retries>-1</backup-request-retries>
               <backup-request-retry-interval>2000</backup-request-retry-interval>
               <max-backups>2</max-backups>
               <request-backup>true</request-backup>
               <master>
                  <failover-on-shutdown>true</failover-on-shutdown>
               </master>
               <slave>
                  <scale-down/>
               </slave>
            </colocated>
         </shared-store>
      </ha-policy>

所有服务器中的群集和ha配置相同。我试图理解和执行的故障转移方案如下。

  1. 按上述顺序启动broker1,broker2,broker3。在这里我可以 从管理用户界面中可以看到broker1已备份broker2和broker3。 broker2具有broker1的支持。 broker3没有任何备份。
  2. 我在下面编写了连接服务器的程序

    public static void main(final String[] args) throws Exception {
        Connection connection = null;
        InitialContext initialContext = null;
        try {
            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY,
                    "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
            properties.put("connectionFactory.ConnectionFactory",
                    "(tcp://localhost:61616,tcp://localhost:61617,tcp://localhost:61618)?ha=true&retryInterval=1000&retryIntervalMultiplier=1.0&reconnectAttempts=-1");
            properties.put("queue.queue/exampleQueue", "exampleQueue");
            // Step 1. Create an initial context to perform the JNDI lookup.
            initialContext = new InitialContext(properties);
    
            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
    
            // Step 2. Look-up the JMS Queue object from JNDI
            Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
            // Step 3. Create a JMS Connection
            connection = cf.createConnection("admin", "admin");
    
            // Step 4. Start the connection
            connection.start();
    
            // Step 5. Create a JMS session with AUTO_ACKNOWLEDGE mode
    
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            // Step 8. Create a text message
            BytesMessage message = session.createBytesMessage();
            message.setStringProperty(InfoSearchEngine.QUERY_ID_HEADER_PARAM, "123");
            MessageConsumer consumer0 = session.createConsumer(queue);
            // Step 9. Send the text message to the queue
            while (true) {
                try {
                    Thread.sleep(500);
                    // Step 7. Create a JMS message producer
                    MessageProducer messageProducer = session.createProducer(queue);
                    messageProducer.send(message);
                    System.out.println("Sent message: " + message.getBodyLength());
                } catch (Exception e) {
                    System.out.println("Exception - " + e.getLocalizedMessage());
                }
            }
        } finally {
            if (connection != null) {
                // Step 20. Be sure to close our JMS resources!
                connection.close();
            }
            if (initialContext != null) {
                // Step 21. Also close the initialContext!
                initialContext.close();
            }
        }
    }
    
  3. 如果我关闭broker1,程序将转移到broker2并正常运行。如果 我关闭了broker2,则程序未连接到broker3。

我希望broker3应该在群集中就已经开始处理该请求。

1 个答案:

答案 0 :(得分:0)

  

我从管理UI中可以看到broker1已备份broker2和broker3。 broker2具有broker1的支持。 broker3没有任何备份。

Artemis中的故障转移仅在实时备份之间进行。在您的方案中,broker1正在备份broker2,因此当您关闭broker1时,这意味着broker2不再具有备份,因此当您关闭broker2时不会发生故障转移。您应该在<group-name>master配置中指定slave,以便以更有条理的方式形成备份,以免发生这种情况。