ACTIVEMQ Master-Slave configuration

Source: Internet
Author: User
Tags failover message queue redis trim apache camel



1 Overview

ACTIVEMQ is an open-source, JMS1.1-compliant, message-oriented (MOM) middleware that provides efficient, scalable, stable, and secure enterprise-class messaging for applications. ACTIVEMQ uses the authorization provided by Apache, and anyone can modify its implementation code.

ACTIVEMQ's design goal is to provide standard, message-oriented, integrated messaging middleware across multiple language and multi-system applications. ACTIVEMQ implements the JMS standard and provides a number of additional features. These additional features include JMX management (Java Management Extensions, Java Management Extensions), master-Slave management (Master/salve, which is one of the cluster patterns, mainly in terms of reliability, when the primary intermediary (agent) fails, Then the agent replaces the primary proxy's location, does not cause the message system to be paralyzed, the message group communicates (the same set of messages is submitted only to one customer for processing), and the ordered message management (ensures that the message is received by the recipient in the order in which it was sent). Message priority (high priority messages are first delivered and processed), deferred subscription messages are received (when a subscription message is published, if the Subscriber does not open the connection, the message mediation submits the previous, unhandled message to the Subscriber when the connection is opened), the receiver is processed too slowly (can use dynamic load balancing, The majority of messages are submitted to the fast-processing receivers, mainly the PTP messages, the virtual receivers (reducing the number of connections to the mediation), the mature message persistence technology (some messages need to be persisted to the database or file system, the information is not lost when the mediation crashes), cursor operations are supported (can handle large messages), Support for message conversion, using Apache Camel can support EIP, using the form of mirror queue to easily monitor the message queue.

< /c13>2 Custom Installation 2.1 Download Address

Download website:

Http://activemq.apache.org/download-archives.html



2.2 Installation Steps

The installation steps are as follows:

1, decompression;

TAR-ZXVF apache-activemq-5.9.0-bin.tar.gz
 


2, related configuration, edit/conf/activemq.xml configuration file:

Master and slave Add the following same configuration:

<brokerxmlns= "Http://activemq.apache.org/schema/core" brokername= "localhost" datadirectory= "${activemq.data}" > <!--configuremessage persistence for the broker.
            The default persistence mechanism isthe KAHADB Store (identified by the kahadb tag). For moreinformation, see:http://activemq.apache.org/persistence.html--<persistencead
            apter> <!--<kahadbdirectory= "${activemq.data}/kahadb"/>-- <jdbcpersistenceadapterdatadirectory= "${activemq.data}/mysqldb" datasource= "#mySql" useDatabaseLock= "true"/ > </persistenceAdapter> </broker> <!--MySQL datasource--> <beanid= "MYSQL" class= "Org.apache.commons.dbcp.BasicDataSource" > <propertyname= "driverclassname" value= " Com.mysql.jdbc.Driver "/> <propertyname=" url "value=" Jdbc:mysql://192.168.1.101:3306/activemq?relaxaUtocommit=true "/> <propertyname=" username "value=" root "/> <propertyname=" password "value=" root "/> <propertyname=" poolpreparedstatements "value=" true "/> </bean>


2. Start:

Execute/bin directory under ACTIVEMQ

./activemq Start &


3. View the log after startup:

Master:


Slave;

The previous log indicates a successful start.

2.3 Master-Slave testing

1. Send queue data using Java client,

Original connection string: tcp://192.168.1.104:61616

Primary and Standby connection string: Failover: (tcp://192.168.1.104:61616,tcp://192.168.1.105:61616)

 

Product Code:

public class Producer {private static final String Broker_url = "Failover: (tcp://192.168.1.104:61616,tcp://192.168.
 
    1.105:61,616) ";
    private static final Boolean non_transacted = false;
    private static final int num_messages_to_send = 100;
 
    Private static final long DELAY = 100;
        public static void Main (string[] args) {String url = broker_url;
        if (Args.length > 0) {url = Args[0].trim ();
        } activemqconnectionfactoryconnectionfactory = new Activemqconnectionfactory ("admin", "admin", url);
        Connection Connection = null;
            try {connection =connectionfactory.createconnection ();
            Connection.start ();
            Session session =connection.createsession (non_transacted, Session.auto_acknowledge);
            Destination Destination =session.createqueue ("Test-queue");
            MessageProducer producer =session.createproducer (destination); for (int i = 0; i < num_messages_tO_send;
                i++) {textmessage message =session.createtextmessage ("message#" + i);
                SYSTEM.OUT.PRINTLN ("Sending message #" + i);
                Producer.send (message);
            Thread.Sleep (DELAY);
            } producer.close ();
 
        Session.close ();
        } catch (Exception e) {System.out.println ("caught exception!");
                } finally {if (connection! = null) {try {connection.close (); 
                } catch (JMSException e) {System.out.println ("Could not close an open connection ..."); }
            }
        }
    }
}


Consumer Code:

public class Consumer {private static final String Broker_url = "Failover: (tcp://192.168.1.104:61616,tcp://192.168.1.
 
    105:61,616) ";
    private static final Boolean non_transacted = false;
 
    Private static final long TIMEOUT = 20000;
        public static void Main (string[] args) {String url = broker_url;
        if (Args.length > 0) {url = Args[0].trim ();
        } System.out.println ("\nwaiting to receive messages ... would timeout after" + timeout/1000 + "s");
        Activemqconnectionfactoryconnectionfactory = new Activemqconnectionfactory ("admin", "admin", url);
        Connection Connection = null;
            try {connection =connectionfactory.createconnection ();
            Connection.start ();
            Session session =connection.createsession (non_transacted, Session.auto_acknowledge);
            Destination Destination =session.createqueue ("Test-queue"); Messageconsumer consumer =session.createconsumer (destination);
            int i = 0;
 
                while (true) {message message =consumer.receive (TIMEOUT); if (message = null) {if (message instanceof textmessage) {String Text = (Te
                        xtmessage) message). GetText (); System.out.println ("Got" + i++ + ".
                    Message: "+ text");
                }} else {break;
            }} consumer.close ();
        Session.close ();
        } catch (Exception e) {System.out.println ("caught exception!");
                } finally {if (connection! = null) {try {connection.close ();
                } catch (JMSException e) {System.out.println ("Could not close an open connection ..."); }
            }
        }
    }
}


2. After sending the queue message, view the ACTIVEMQ Web page information:


To see if the corresponding database has persisted data:

Log Output information:

3 master-Slave fail-over test 3.1 Test slave auto Switch

1) Stop Master:

[root@centosbin]#./activemq stop


You can view the slave print log:




You can view the slave from the previous not acquired lock into master to get the current ACTIVEMQ lock

2) Client consumers continue to access the data:

Failover: (tcp://192.168.1.104:61616,tcp://192.168.1.105:61616)



You can see that consumer data can still be read by client consumers in the case of master interruption.

3) After setting the authentication password, the Redis service shutdown requires password verification before it can be closed, so the command is modified to:

$ redis-cli–a 123456 shutdown

3.2 Testing the old master to switch after rebooting

1) Restart the master that was previously stopped;

[Root@centos bin]#./activemqstart &


As follows:

Can see the old master at this time to slave wait for lock;

2) Stop the current new master:

[root@centosbin]#./activemq stop

To view the old master log:


You can see that the old Master is re-promoted to the current master

Using the client consumer continues to consume data, you can see that the master-slave switching process still does not affect business operations.

Summary: Master-slave in the process of switching, is the master-slave role in turn to act.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.