Several cluster configurations of ActiveMQ and several clusters of activemq

Source: Internet
Author: User
Tags amq

Several cluster configurations of ActiveMQ and several clusters of activemq

ActiveMQ is a powerful Messaging Server that supports many development languages, such as Java, C, C ++, and C. Enterprise-level message servers have high requirements on server stability and speed, while ActiveMQ's distributed cluster can meet this requirement. The following describes several cluster configurations of ActiveMQ.

Queue consumer clusters

This cluster allows multiple consumers to consume a queue at the same time. If a consumer fails to consume information, messages that have not been consumed will be sent to other normal consumers. The structure diagram is as follows:

Broker clusters

This configuration is a broker in which a consumer connects to multiple broker clusters. When a broker fails, the consumer automatically connects to another normal broker. The consumer uses the failover: // protocol to connect to the broker.

failover:(tcp://localhost:61616,tcp://localhost:61617)

Failover official website introduction Co., http://activemq.apache.org/failover-transport-reference.html.

Brokers maintain mutual discovery through static discovery and dynamic discovery. The following describes the mechanisms of static and dynamic discovery:

Static discovery:

Static discovery discovers each other by configuring a fixed broker uri. The configuration syntax is as follows:

static:(uri1,uri2,uri3,...)?options

For example:

static:(tcp://localhost:61616,tcp://remotehost:61617?trace=false,vm://localbroker)?initialReconnectDelay=100

For more information about static discovery, see ActiveMQ official website http://activemq.apache.org/static-transport-reference.html

Dynamic discovery:

The dynamic discovery mechanism discovers each other through Fanout transport when each broker is started. The configuration example is as follows:

1 <broker name="foo">2   <transportConnectors>3     <transportConnector uri="tcp://localhost:0" discoveryUri="multicast://default"/>4   </transportConnectors>5   ...6 </broker>

 

More dynamic discovery mechanism Introduction, see the official website http://activemq.apache.org/discovery-transport-reference.html

 

Networks of brokers

Multiple brokers form a cluster. When a consumer of One broker fails to consume messages, the Network of Broker solution supported by ActiveMQ can forward accumulated messages from the broker to other brokers with consumers. This solution has the following two configuration methods:

1. Configure the networkConnector element for the broker configuration file

2. Use discovery mechanisms to detect Brokers

Here is an example of using the fixed list of URIs:

 1 <?xml version="1.0" encoding="UTF-8"?> 2   3 <beans xmlns="http://activemq.org/config/1.0"> 4   5   <broker brokerName="receiver" persistent="false" useJmx="false">  6     <networkConnectors> 7       <!-- Static discovery --> 8       <networkConnector uri="static:(tcp://localhost:62001)"/> 9       <!-- MasterSlave Discovery -->10       <!--<networkConnector uri="masterslave:(tcp://host1:61616,tcp://host2:61616,tcp://..)"/> -->11     </networkConnectors>12  13     <persistenceAdapter>14       <memoryPersistenceAdapter/>15     </persistenceAdapter>16  17    <transportConnectors>18       <transportConnector uri="tcp://localhost:62002"/>19     </transportConnectors>20   </broker>21  22 </beans>

 

This example uses multicast discovery:

 1 <?xml version="1.0" encoding="UTF-8"?> 2   3 <beans xmlns="http://activemq.org/config/1.0"> 4   5   <broker name="sender" persistent="false" useJmx="false">  6     <networkConnectors> 7       <networkConnector uri="multicast://default"/> 8     </networkConnectors> 9  10     <persistenceAdapter>11       <memoryPersistenceAdapter/>12     </persistenceAdapter>13  14   <transportConnectors>15       <transportConnector uri="tcp://localhost:0" discoveryUri="multicast://default"/>16     </transportConnectors>17   </broker>18  19 </beans>

 

Master Slave

By deploying multiple broker instances, one master node and multiple slave brokers to achieve high availability, there are three solutions:

1. Master-Slave
2. SharedFile System Master Slave
3. JDBCMaster Slave

The first solution is not widely used because only two AMQ instance components can be used;
The third solution supports networking n amq instances, but its performance is limited by the database;
The second solution also supports the networking of n amq instances. Based on the kahadb storage policy, the solution can also be deployed on a distributed file system with flexible, efficient, and secure applications.

The Master Slave scheme automatically becomes the master when one of the brokers starts and obtains the exclusive lock. Other brokers will remain waiting for the lock, when the master is down and the lock is released, other slave will automatically become the master when the exclusive lock is obtained. The deployment structure is as follows:

To configure the second scheme, you only need to modify the activemq. xml file in the config folder and modify the scheme for message Persistence:

1 <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="D:/Platform/mq_share_file">2   ...3     <persistenceAdapter>4             <kahaDB directory="D:/Platform/mq_share_file/kahadb" enableIndexWriteAsync="true" enableJournalDiskSyncs="false"/>5     </persistenceAdapter>6     ...7 </broker>

 

Message producer code:

1 public class P2PSender {2 private static final String QUEUE = "client1-to-client2"; 3 4 public static void main (String [] args) {5 // ConnectionFactory: Connection factory, JMS uses it to create a Connection 6 ConnectionFactory connectionFactory; 7 // Connection: the connection from the JMS client to the JMS Provider 8 Connection connection = null; 9 // Session: 10 Session session of a thread that sends or receives a message; 11 // Destination: the Destination of the message and to whom the message is sent. 12 Destination destination; 13 // MessagePr Oducer: message sender 14 MessageProducer producer; 15 // TextMessage message; 16 // construct the ConnectionFactory instance object. The ActiveMq implementation 17 connectionFactory = new ActiveMQConnectionFactory (18 "failover :( tcp: // localhost: 61616? WireFormat. maxInactivityDuration = 0, tcp :/// localhost: 61617? WireFormat. maxInactivityDuration = 0) "); 19 try {20 // construct the connection object 21 connection = connectionFactory from the factory. createConnection (); 22 // start 23 connection. start (); 24 // get operation connection 25 session = connection. createSession (false, Session. AUTO_ACKNOWLEDGE); 26 destination = session. createQueue (QUEUE); 27 // get the session. FirstQueue is a server's queue destination = session. createQueue ("FirstQueue"); 28 // message producer [Sender] 29 producer = session. c ReateProducer (destination); 30 // set 31 producer not persistent. setDeliveryMode (DeliveryMode. NON_PERSISTENT); 32 // construct the message 33 sendMessage (session, producer); 34 // session. commit (); 35 connection. close (); 36} catch (Exception e) {37 e. printStackTrace (); 38} finally {39 if (null! = Connection) {40 try {41 connection. close (); 42} catch (JMSException e) {43 e. printStackTrace (); 44} 45} 46} 47} 48 49 public static void sendMessage (Session session, MessageProducer producer) throws Exception {50 for (int I = 1; I <= 1; I ++) {51 Date d = new Date (); 52 TextMessage message = session. createTextMessage ("ActiveMQ sends messages" + I + "" + new Date (); 53 System. out. println ("Send message: Message sent by ActiveMQ" + I + "" + new Date (); 54 producer. send (message); 55} 56} 57}

Message Consumer Code:

1 public class P2PReceiver {2 private static final String QUEUE = "client1-to-client2"; 3 4 public static void main (String [] args) {5 // ConnectionFactory: Connection factory, JMS uses it to create a Connection 6 ConnectionFactory connectionFactory; 7 // Connection: the connection from the JMS client to the JMS Provider 8 Connection connection = null; 9 // Session: 10 Session session of a thread that sends or receives a message; 11 // Destination: the Destination of the message and to whom the message is sent. 12 Destination destination; 13 // consumer, connected to the message Recipient 14 MessageConsumer consumer; 15 connectionFactory = new ActiveMQConnectionFactory ("failover :( tcp: // localhost: 61616? WireFormat. maxInactivityDuration = 0, tcp :/// localhost: 61617? WireFormat. maxInactivityDuration = 0) "); 16 try {17 // get the connection object 18 connection = connectionFactory. createConnection (); 19 // start 20 connection. start (); 21 // obtain the operation connection 22 session = connection. createSession (false, Session. AUTO_ACKNOWLEDGE); 23 // create Queue24 destination = session. createQueue (QUEUE); 25 consumer = session. createConsumer (destination); 26 while (true) {27 TextMessage message = (TextMessage) consumer. recei Ve (); 28 if (null! = Message) {29 System. out. println ("Receive message" + message. getText (); 30} 31} 32} catch (Exception e) {33 e. printStackTrace (); 34} finally {35 try {36 if (null! = Connection) 37 connection. close (); 38} catch (Throwable ignore) {39} 40} 41} 42}

 

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.