ACTIVEMQ is a powerful messaging server that supports a variety of development languages such as Java, C, C + +, C #, and more. Enterprise-level messaging servers, regardless of server stability or speed, requirements are very high, and ACTIVEMQ distributed cluster can be very good to meet this demand, the following is a few ACTIVEMQ cluster configuration.
Queue Consumer Clusters
This cluster allows multiple consumers to consume a queue at the same time, if a consumer is unable to consume information, the message will be sent to other normal consumers, the structure chart is as follows:
Broker clusters
This configuration is a broker in which a consumer connects to multiple broker clusters, and when the broker goes wrong, 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 http://activemq.apache.org/failover-transport-reference.html
The mechanisms of static discovery and dynamic discovery are described below in the discovery between broker, through static discovery, and dynamic discovery , to maintain discovery of each other:
Static discovery:
Static discovery discovers each other by configuring a fixed broker URI, with the following configuration syntax:
Static: (Uri1,uri2,uri3,...)? Options
For example:
Static: (tcp://localhost:61616,tcp://remotehost:61617?trace=false,vm://localbroker)? initialreconnectdelay=100
More static find introduction, 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, with the following configuration examples:
<broker name= "foo" > <transportConnectors> <transportconnector uri= "tcp://localhost:0" discoveryuri= "Multicast://default"/> </transportConnectors> ... </broker>
More dynamic discovery mechanism introduction, see official website http://activemq.apache.org/discovery-transport-reference.html
Networks of brokers
Multiple brokers make up a cluster, and when one of the broker's consumer problems causes the message stack to not be consumed, the network of the ACTIVEMQ support The broker scheme forwards the messages stacked by the broker to other brokers with consumers.
The program mainly has the following two configuration methods:
1. Configure the Networkconnector element for the broker configuration file
2, using discovery mechanism to detect each other broker
Here's an example of using the fixed list of URIs:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "http://activemq.org/config/1.0" > <broker Brokername= "receiver" persistent= "false" usejmx= "false" > <networkConnectors> <!--Static Discovery --<networkconnector uri= "static: (tcp://localhost:62001)"/> <!--masterslave Discovery -<!--<networkconnector uri= "Masterslave: (tcp://host1:61616,tcp://host2:61616,tcp://...)" />- </networkConnectors> <persistenceAdapter> <memorypersistenceadapter/ > </persistenceAdapter> <transportConnectors> <transportconnector uri= "tcp:// localhost:62002 "/> </transportConnectors> </broker> </beans>
This example uses multicast discovery:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "http://activemq.org/config/1.0" > <broker Name= "Sender" persistent= "false" usejmx= "false" > <networkConnectors> <networkconnector uri= " Multicast://default "/> </networkConnectors> <persistenceAdapter> < memorypersistenceadapter/> </persistenceAdapter> <transportConnectors> < Transportconnector uri= "tcp://localhost:0" discoveryuri= "Multicast://default"/> </transportConnectors > </broker> </beans>
Master Slave
By deploying multiple broker instances, one master and multiple slave relationships broker to achieve high availability, there are three scenarios:
1, Master-slave
2. Sharedfile System Master Slave
3, Jdbcmaster Slave
The first scheme is not widely used because it can only be composed of two amq instance components;
The third scenario supports N AMQ instance networking, but his performance will be limited to the database;
The second scenario also supports N AMQ instance networking, based on the KAHADB storage strategy, and can be deployed on distributed file systems for flexible, efficient and secure applications.
Master slave scenario when one of the brokers starts and gets an exclusive lock, the other subsequent broker waits for the lock, and the other slave gets the exclusive lock when the master fails to release the lock automatically becomes master, and the deployment structure is as follows:
The second scenario is configured simply by modifying the Config folder under the Activemq.xml file, modifying the scenario that the message persists:
<broker xmlns= "http://activemq.apache.org/schema/core" brokername= "localhost" datadirectory= "d:/platform/mq_ Share_file "> ... <persistenceAdapter> <kahadb directory= "d:/platform/mq_share_file/kahadb" enableindexwriteasync= " True "enablejournaldisksyncs=" false "/> </persistenceAdapter> ...</broker>
Message Producer Code:
public class P2psender {private static final String QUEUE = "Client1-to-client2"; public static void Main (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connection connectionfactory CONNECTIONFA Ctory; CONNECTION:JMS client-to-JMS provider connection Connection Connection = null; Session: A thread that sends or receives a message session session; Destination: The destination of the message, to whom the message is sent. Destination Destination; MessageProducer: Message sender MessageProducer producer; TextMessage message; Constructs the ConnectionFactory instance object, which is implemented here with activemq connectionfactory = new Activemqconnectionfactory ("Failo Ver: (tcp://localhost:61616?wireformat.maxinactivityduration=0,tcp://localhost:61617?) wireformat.maxinactivityduration=0) "); try {//Construct get Connection object from factory connection = Connectionfactory.createconnection (); Start Connection.start (); Get operation Connection session = Connection.createsession (False, SessIon. Auto_acknowledge); Destination = Session.createqueue (QUEUE); Get Session,firstqueue is a server of queue destination = Session.createqueue ("Firstqueue"); Get the message Generator "sender" producer = Session.createproducer (destination); Set non-persistent producer.setdeliverymode (deliverymode.non_persistent); Construct Message SendMessage (session, producer); Session.commit (); Connection.close (); } catch (Exception e) {e.printstacktrace (); } finally {if (null! = connection) {try {connection.close (); } catch (JMSException e) {e.printstacktrace (); }}}} public static void SendMessage (Session session, MessageProducer producer) throws Exception {for (int i = 1; I <= 1; i++) {Date d = new Date (); TextMessage message = Session.createtextmessaGE ("ACTIVEMQ send Message" + i + "" + New Date ()); SYSTEM.OUT.PRINTLN ("Send message: Message sent by ACTIVEMQ" + i + "" + New Date ()); Producer.send (message); } }}
message Consumer code:
public class P2preceiver {private static final String QUEUE = "Client1-to-client2"; public static void Main (string[] args) {//ConnectionFactory: Connection Factory, JMS uses it to create a connection connectionfactory CONNECTIONFA Ctory; CONNECTION:JMS client-to-JMS provider connection Connection Connection = null; Session: A thread that sends or receives a message session session; Destination: The destination of the message, to whom the message is sent. Destination Destination; Consumer, message recipient Messageconsumer consumer; ConnectionFactory = new Activemqconnectionfactory ("Failover: (tcp://localhost:61616? wireformat.maxinactivityduration=0,tcp://localhost:61617?wireformat.maxinactivityduration=0) "); try {//Get Connection object connection = Connectionfactory.createconnection (); Start Connection.start (); Get operation Connection session = Connection.createsession (false, Session.auto_acknowledge); Create Queue Destination = Session.createqueue (queue); Consumer = session.createconsumer (destination); while (true) {TextMessage message = (textmessage) consumer.receive (); if (null! = message) {SYSTEM.OUT.PRINTLN ("Receive Message" + Message.gettext ()); }}} catch (Exception e) {e.printstacktrace (); } finally {try {if (null! = connection) connection.close (); } catch (Throwable ignore) {}}}}
Several cluster configurations transferred from ACTIVEMQ
Go Several cluster configurations of ACTIVEMQ